7
7
8
8
"""
9
9
10
- # Injected modules are '_warnings', 'imp ', 'sys', 'marshal', '_io',
10
+ # Injected modules are '_warnings', '_imp ', 'sys', 'marshal', '_io',
11
11
# and '_os' (a.k.a. 'posix', 'nt' or 'os2').
12
12
# Injected attribute is path_sep.
13
13
# Most injection is handled by _setup().
@@ -158,6 +158,16 @@ def _wrap(new, old):
158
158
159
159
code_type = type (_wrap .__code__ )
160
160
161
+
162
+ def _new_module (name ):
163
+ """Create a new module.
164
+
165
+ The module is not entered into sys.modules.
166
+
167
+ """
168
+ return type (sys )(name )
169
+
170
+
161
171
# Finder/loader utility code ##################################################
162
172
163
173
def verbose_message (message , * args ):
@@ -212,7 +222,7 @@ def module_for_loader_wrapper(self, fullname, *args, **kwargs):
212
222
# This must be done before open() is called as the 'io' module
213
223
# implicitly imports 'locale' and would otherwise trigger an
214
224
# infinite loop.
215
- module = imp . new_module (fullname )
225
+ module = _new_module (fullname )
216
226
sys .modules [fullname ] = module
217
227
try :
218
228
return fxn (self , module , * args , ** kwargs )
@@ -254,7 +264,7 @@ def _requires_builtin_wrapper(self, fullname):
254
264
def _requires_frozen (fxn ):
255
265
"""Decorator to verify the named module is frozen."""
256
266
def _requires_frozen_wrapper (self , fullname ):
257
- if not imp .is_frozen (fullname ):
267
+ if not _imp .is_frozen (fullname ):
258
268
raise ImportError ("{0} is not a frozen module" .format (fullname ),
259
269
name = fullname )
260
270
return fxn (self , fullname )
@@ -264,7 +274,7 @@ def _requires_frozen_wrapper(self, fullname):
264
274
265
275
def _suffix_list (suffix_type ):
266
276
"""Return a list of file suffixes based on the imp file type."""
267
- return [suffix [0 ] for suffix in imp .get_suffixes ()
277
+ return [suffix [0 ] for suffix in _imp .get_suffixes ()
268
278
if suffix [2 ] == suffix_type ]
269
279
270
280
@@ -288,7 +298,7 @@ def find_module(cls, fullname, path=None):
288
298
"""
289
299
if path is not None :
290
300
return None
291
- return cls if imp .is_builtin (fullname ) else None
301
+ return cls if _imp .is_builtin (fullname ) else None
292
302
293
303
@classmethod
294
304
@set_package
@@ -298,7 +308,7 @@ def load_module(cls, fullname):
298
308
"""Load a built-in module."""
299
309
is_reload = fullname in sys .modules
300
310
try :
301
- return imp .init_builtin (fullname )
311
+ return _imp .init_builtin (fullname )
302
312
except :
303
313
if not is_reload and fullname in sys .modules :
304
314
del sys .modules [fullname ]
@@ -335,7 +345,7 @@ class FrozenImporter:
335
345
@classmethod
336
346
def find_module (cls , fullname , path = None ):
337
347
"""Find a frozen module."""
338
- return cls if imp .is_frozen (fullname ) else None
348
+ return cls if _imp .is_frozen (fullname ) else None
339
349
340
350
@classmethod
341
351
@set_package
@@ -345,7 +355,7 @@ def load_module(cls, fullname):
345
355
"""Load a frozen module."""
346
356
is_reload = fullname in sys .modules
347
357
try :
348
- return imp .init_frozen (fullname )
358
+ return _imp .init_frozen (fullname )
349
359
except :
350
360
if not is_reload and fullname in sys .modules :
351
361
del sys .modules [fullname ]
@@ -355,7 +365,7 @@ def load_module(cls, fullname):
355
365
@_requires_frozen
356
366
def get_code (cls , fullname ):
357
367
"""Return the code object for the frozen module."""
358
- return imp .get_frozen_object (fullname )
368
+ return _imp .get_frozen_object (fullname )
359
369
360
370
@classmethod
361
371
@_requires_frozen
@@ -367,7 +377,7 @@ def get_source(cls, fullname):
367
377
@_requires_frozen
368
378
def is_package (cls , fullname ):
369
379
"""Return if the frozen module is a package."""
370
- return imp .is_frozen_package (fullname )
380
+ return _imp .is_frozen_package (fullname )
371
381
372
382
373
383
class _LoaderBasics :
@@ -391,7 +401,7 @@ def _bytes_from_bytecode(self, fullname, data, bytecode_path, source_stats):
391
401
magic = data [:4 ]
392
402
raw_timestamp = data [4 :8 ]
393
403
raw_size = data [8 :12 ]
394
- if len (magic ) != 4 or magic != imp .get_magic ():
404
+ if len (magic ) != 4 or magic != _imp .get_magic ():
395
405
raise ImportError ("bad magic number in {}" .format (fullname ),
396
406
name = fullname , path = bytecode_path )
397
407
elif len (raw_timestamp ) != 4 :
@@ -434,7 +444,7 @@ def _load_module(self, module, *, sourceless=False):
434
444
code_object = self .get_code (name )
435
445
module .__file__ = self .get_filename (name )
436
446
if not sourceless :
437
- module .__cached__ = imp .cache_from_source (module .__file__ )
447
+ module .__cached__ = _imp .cache_from_source (module .__file__ )
438
448
else :
439
449
module .__cached__ = module .__file__
440
450
module .__package__ = name
@@ -497,7 +507,7 @@ def get_code(self, fullname):
497
507
498
508
"""
499
509
source_path = self .get_filename (fullname )
500
- bytecode_path = imp .cache_from_source (source_path )
510
+ bytecode_path = _imp .cache_from_source (source_path )
501
511
source_mtime = None
502
512
if bytecode_path is not None :
503
513
try :
@@ -522,7 +532,7 @@ def get_code(self, fullname):
522
532
source_path )
523
533
found = marshal .loads (bytes_data )
524
534
if isinstance (found , code_type ):
525
- imp ._fix_co_filename (found , source_path )
535
+ _imp ._fix_co_filename (found , source_path )
526
536
verbose_message ('code object from {}' ,
527
537
bytecode_path )
528
538
return found
@@ -539,7 +549,7 @@ def get_code(self, fullname):
539
549
# If e.g. Jython ever implements imp.cache_from_source to have
540
550
# their own cached file format, this block of code will most likely
541
551
# throw an exception.
542
- data = bytearray (imp .get_magic ())
552
+ data = bytearray (_imp .get_magic ())
543
553
data .extend (_w_long (source_mtime ))
544
554
data .extend (_w_long (len (source_bytes )))
545
555
data .extend (marshal .dumps (code_object ))
@@ -664,7 +674,7 @@ def load_module(self, fullname):
664
674
"""Load an extension module."""
665
675
is_reload = fullname in sys .modules
666
676
try :
667
- module = imp .load_dynamic (fullname , self ._path )
677
+ module = _imp .load_dynamic (fullname , self ._path )
668
678
verbose_message ('extension module loaded from {!r}' , self ._path )
669
679
return module
670
680
except :
@@ -841,15 +851,15 @@ class _SourceFinderDetails:
841
851
supports_packages = True
842
852
843
853
def __init__ (self ):
844
- self .suffixes = _suffix_list (imp .PY_SOURCE )
854
+ self .suffixes = _suffix_list (_imp .PY_SOURCE )
845
855
846
856
class _SourcelessFinderDetails :
847
857
848
858
loader = _SourcelessFileLoader
849
859
supports_packages = True
850
860
851
861
def __init__ (self ):
852
- self .suffixes = _suffix_list (imp .PY_COMPILED )
862
+ self .suffixes = _suffix_list (_imp .PY_COMPILED )
853
863
854
864
855
865
class _ExtensionFinderDetails :
@@ -858,7 +868,7 @@ class _ExtensionFinderDetails:
858
868
supports_packages = False
859
869
860
870
def __init__ (self ):
861
- self .suffixes = _suffix_list (imp .C_EXTENSION )
871
+ self .suffixes = _suffix_list (_imp .C_EXTENSION )
862
872
863
873
864
874
# Import itself ###############################################################
@@ -886,7 +896,7 @@ def _path_hooks(cls, path):
886
896
try :
887
897
return super ()._path_hooks (path )
888
898
except ImportError :
889
- implicit_hooks = [_DEFAULT_PATH_HOOK , imp .NullImporter ]
899
+ implicit_hooks = [_DEFAULT_PATH_HOOK , _imp .NullImporter ]
890
900
return super ()._path_hooks (path , implicit_hooks )
891
901
892
902
@classmethod
@@ -902,11 +912,11 @@ class _ImportLockContext:
902
912
903
913
def __enter__ (self ):
904
914
"""Acquire the import lock."""
905
- imp .acquire_lock ()
915
+ _imp .acquire_lock ()
906
916
907
917
def __exit__ (self , exc_type , exc_value , exc_traceback ):
908
918
"""Release the import lock regardless of any raised exceptions."""
909
- imp .release_lock ()
919
+ _imp .release_lock ()
910
920
911
921
912
922
def _resolve_name (name , package , level ):
@@ -1092,19 +1102,19 @@ def __import__(name, globals={}, locals={}, fromlist=[], level=0):
1092
1102
return _handle_fromlist (module , fromlist , _gcd_import )
1093
1103
1094
1104
1095
- def _setup (sys_module , imp_module ):
1105
+ def _setup (sys_module , _imp_module ):
1096
1106
"""Setup importlib by importing needed built-in modules and injecting them
1097
1107
into the global namespace.
1098
1108
1099
- As sys is needed for sys.modules access and imp is needed to load built-in
1109
+ As sys is needed for sys.modules access and _imp is needed to load built-in
1100
1110
modules, those two modules must be explicitly passed in.
1101
1111
1102
1112
"""
1103
- global imp , sys
1104
- imp = imp_module
1113
+ global _imp , sys
1114
+ _imp = _imp_module
1105
1115
sys = sys_module
1106
1116
1107
- for module in (imp , sys ):
1117
+ for module in (_imp , sys ):
1108
1118
if not hasattr (module , '__loader__' ):
1109
1119
module .__loader__ = BuiltinImporter
1110
1120
@@ -1137,14 +1147,14 @@ def _setup(sys_module, imp_module):
1137
1147
setattr (self_module , '_relax_case' , _make_relax_case ())
1138
1148
1139
1149
1140
- def _install (sys_module , imp_module ):
1150
+ def _install (sys_module , _imp_module ):
1141
1151
"""Install importlib as the implementation of import.
1142
1152
1143
- It is assumed that imp and sys have been imported and injected into the
1153
+ It is assumed that _imp and sys have been imported and injected into the
1144
1154
global namespace for the module prior to calling this function.
1145
1155
1146
1156
"""
1147
- _setup (sys_module , imp_module )
1157
+ _setup (sys_module , _imp_module )
1148
1158
orig_import = builtins .__import__
1149
1159
builtins .__import__ = __import__
1150
1160
builtins .__original_import__ = orig_import
0 commit comments