Home | History | Annotate | Download | only in importlib
      1 """Core implementation of import.
      2 
      3 This module is NOT meant to be directly imported! It has been designed such
      4 that it can be bootstrapped into Python as the implementation of import. As
      5 such it requires the injection of specific modules and attributes in order to
      6 work. One should use importlib as the public-facing version of this module.
      7 
      8 """
      9 #
     10 # IMPORTANT: Whenever making changes to this module, be sure to run
     11 # a top-level make in order to get the frozen version of the module
     12 # updated. Not doing so will result in the Makefile to fail for
     13 # all others who don't have a ./python around to freeze the module
     14 # in the early stages of compilation.
     15 #
     16 
     17 # See importlib._setup() for what is injected into the global namespace.
     18 
     19 # When editing this code be aware that code executed at import time CANNOT
     20 # reference any injected objects! This includes not only global code but also
     21 # anything specified at the class level.
     22 
     23 # Bootstrap-related code ######################################################
     24 
     25 _bootstrap_external = None
     26 
     27 def _wrap(new, old):
     28     """Simple substitute for functools.update_wrapper."""
     29     for replace in ['__module__', '__name__', '__qualname__', '__doc__']:
     30         if hasattr(old, replace):
     31             setattr(new, replace, getattr(old, replace))
     32     new.__dict__.update(old.__dict__)
     33 
     34 
     35 def _new_module(name):
     36     return type(sys)(name)
     37 
     38 
     39 # Module-level locking ########################################################
     40 
     41 # A dict mapping module names to weakrefs of _ModuleLock instances
     42 _module_locks = {}
     43 # A dict mapping thread ids to _ModuleLock instances
     44 _blocking_on = {}
     45 
     46 
     47 class _DeadlockError(RuntimeError):
     48     pass
     49 
     50 
     51 class _ModuleLock:
     52     """A recursive lock implementation which is able to detect deadlocks
     53     (e.g. thread 1 trying to take locks A then B, and thread 2 trying to
     54     take locks B then A).
     55     """
     56 
     57     def __init__(self, name):
     58         self.lock = _thread.allocate_lock()
     59         self.wakeup = _thread.allocate_lock()
     60         self.name = name
     61         self.owner = None
     62         self.count = 0
     63         self.waiters = 0
     64 
     65     def has_deadlock(self):
     66         # Deadlock avoidance for concurrent circular imports.
     67         me = _thread.get_ident()
     68         tid = self.owner
     69         while True:
     70             lock = _blocking_on.get(tid)
     71             if lock is None:
     72                 return False
     73             tid = lock.owner
     74             if tid == me:
     75                 return True
     76 
     77     def acquire(self):
     78         """
     79         Acquire the module lock.  If a potential deadlock is detected,
     80         a _DeadlockError is raised.
     81         Otherwise, the lock is always acquired and True is returned.
     82         """
     83         tid = _thread.get_ident()
     84         _blocking_on[tid] = self
     85         try:
     86             while True:
     87                 with self.lock:
     88                     if self.count == 0 or self.owner == tid:
     89                         self.owner = tid
     90                         self.count += 1
     91                         return True
     92                     if self.has_deadlock():
     93                         raise _DeadlockError('deadlock detected by %r' % self)
     94                     if self.wakeup.acquire(False):
     95                         self.waiters += 1
     96                 # Wait for a release() call
     97                 self.wakeup.acquire()
     98                 self.wakeup.release()
     99         finally:
    100             del _blocking_on[tid]
    101 
    102     def release(self):
    103         tid = _thread.get_ident()
    104         with self.lock:
    105             if self.owner != tid:
    106                 raise RuntimeError('cannot release un-acquired lock')
    107             assert self.count > 0
    108             self.count -= 1
    109             if self.count == 0:
    110                 self.owner = None
    111                 if self.waiters:
    112                     self.waiters -= 1
    113                     self.wakeup.release()
    114 
    115     def __repr__(self):
    116         return '_ModuleLock({!r}) at {}'.format(self.name, id(self))
    117 
    118 
    119 class _DummyModuleLock:
    120     """A simple _ModuleLock equivalent for Python builds without
    121     multi-threading support."""
    122 
    123     def __init__(self, name):
    124         self.name = name
    125         self.count = 0
    126 
    127     def acquire(self):
    128         self.count += 1
    129         return True
    130 
    131     def release(self):
    132         if self.count == 0:
    133             raise RuntimeError('cannot release un-acquired lock')
    134         self.count -= 1
    135 
    136     def __repr__(self):
    137         return '_DummyModuleLock({!r}) at {}'.format(self.name, id(self))
    138 
    139 
    140 class _ModuleLockManager:
    141 
    142     def __init__(self, name):
    143         self._name = name
    144         self._lock = None
    145 
    146     def __enter__(self):
    147         try:
    148             self._lock = _get_module_lock(self._name)
    149         finally:
    150             _imp.release_lock()
    151         self._lock.acquire()
    152 
    153     def __exit__(self, *args, **kwargs):
    154         self._lock.release()
    155 
    156 
    157 # The following two functions are for consumption by Python/import.c.
    158 
    159 def _get_module_lock(name):
    160     """Get or create the module lock for a given module name.
    161 
    162     Should only be called with the import lock taken."""
    163     lock = None
    164     try:
    165         lock = _module_locks[name]()
    166     except KeyError:
    167         pass
    168     if lock is None:
    169         if _thread is None:
    170             lock = _DummyModuleLock(name)
    171         else:
    172             lock = _ModuleLock(name)
    173         def cb(_):
    174             del _module_locks[name]
    175         _module_locks[name] = _weakref.ref(lock, cb)
    176     return lock
    177 
    178 def _lock_unlock_module(name):
    179     """Release the global import lock, and acquires then release the
    180     module lock for a given module name.
    181     This is used to ensure a module is completely initialized, in the
    182     event it is being imported by another thread.
    183 
    184     Should only be called with the import lock taken."""
    185     lock = _get_module_lock(name)
    186     _imp.release_lock()
    187     try:
    188         lock.acquire()
    189     except _DeadlockError:
    190         # Concurrent circular import, we'll accept a partially initialized
    191         # module object.
    192         pass
    193     else:
    194         lock.release()
    195 
    196 # Frame stripping magic ###############################################
    197 def _call_with_frames_removed(f, *args, **kwds):
    198     """remove_importlib_frames in import.c will always remove sequences
    199     of importlib frames that end with a call to this function
    200 
    201     Use it instead of a normal call in places where including the importlib
    202     frames introduces unwanted noise into the traceback (e.g. when executing
    203     module code)
    204     """
    205     return f(*args, **kwds)
    206 
    207 
    208 def _verbose_message(message, *args, verbosity=1):
    209     """Print the message to stderr if -v/PYTHONVERBOSE is turned on."""
    210     if sys.flags.verbose >= verbosity:
    211         if not message.startswith(('#', 'import ')):
    212             message = '# ' + message
    213         print(message.format(*args), file=sys.stderr)
    214 
    215 
    216 def _requires_builtin(fxn):
    217     """Decorator to verify the named module is built-in."""
    218     def _requires_builtin_wrapper(self, fullname):
    219         if fullname not in sys.builtin_module_names:
    220             raise ImportError('{!r} is not a built-in module'.format(fullname),
    221                               name=fullname)
    222         return fxn(self, fullname)
    223     _wrap(_requires_builtin_wrapper, fxn)
    224     return _requires_builtin_wrapper
    225 
    226 
    227 def _requires_frozen(fxn):
    228     """Decorator to verify the named module is frozen."""
    229     def _requires_frozen_wrapper(self, fullname):
    230         if not _imp.is_frozen(fullname):
    231             raise ImportError('{!r} is not a frozen module'.format(fullname),
    232                               name=fullname)
    233         return fxn(self, fullname)
    234     _wrap(_requires_frozen_wrapper, fxn)
    235     return _requires_frozen_wrapper
    236 
    237 
    238 # Typically used by loader classes as a method replacement.
    239 def _load_module_shim(self, fullname):
    240     """Load the specified module into sys.modules and return it.
    241 
    242     This method is deprecated.  Use loader.exec_module instead.
    243 
    244     """
    245     spec = spec_from_loader(fullname, self)
    246     if fullname in sys.modules:
    247         module = sys.modules[fullname]
    248         _exec(spec, module)
    249         return sys.modules[fullname]
    250     else:
    251         return _load(spec)
    252 
    253 # Module specifications #######################################################
    254 
    255 def _module_repr(module):
    256     # The implementation of ModuleType.__repr__().
    257     loader = getattr(module, '__loader__', None)
    258     if hasattr(loader, 'module_repr'):
    259         # As soon as BuiltinImporter, FrozenImporter, and NamespaceLoader
    260         # drop their implementations for module_repr. we can add a
    261         # deprecation warning here.
    262         try:
    263             return loader.module_repr(module)
    264         except Exception:
    265             pass
    266     try:
    267         spec = module.__spec__
    268     except AttributeError:
    269         pass
    270     else:
    271         if spec is not None:
    272             return _module_repr_from_spec(spec)
    273 
    274     # We could use module.__class__.__name__ instead of 'module' in the
    275     # various repr permutations.
    276     try:
    277         name = module.__name__
    278     except AttributeError:
    279         name = '?'
    280     try:
    281         filename = module.__file__
    282     except AttributeError:
    283         if loader is None:
    284             return '<module {!r}>'.format(name)
    285         else:
    286             return '<module {!r} ({!r})>'.format(name, loader)
    287     else:
    288         return '<module {!r} from {!r}>'.format(name, filename)
    289 
    290 
    291 class _installed_safely:
    292 
    293     def __init__(self, module):
    294         self._module = module
    295         self._spec = module.__spec__
    296 
    297     def __enter__(self):
    298         # This must be done before putting the module in sys.modules
    299         # (otherwise an optimization shortcut in import.c becomes
    300         # wrong)
    301         self._spec._initializing = True
    302         sys.modules[self._spec.name] = self._module
    303 
    304     def __exit__(self, *args):
    305         try:
    306             spec = self._spec
    307             if any(arg is not None for arg in args):
    308                 try:
    309                     del sys.modules[spec.name]
    310                 except KeyError:
    311                     pass
    312             else:
    313                 _verbose_message('import {!r} # {!r}', spec.name, spec.loader)
    314         finally:
    315             self._spec._initializing = False
    316 
    317 
    318 class ModuleSpec:
    319     """The specification for a module, used for loading.
    320 
    321     A module's spec is the source for information about the module.  For
    322     data associated with the module, including source, use the spec's
    323     loader.
    324 
    325     `name` is the absolute name of the module.  `loader` is the loader
    326     to use when loading the module.  `parent` is the name of the
    327     package the module is in.  The parent is derived from the name.
    328 
    329     `is_package` determines if the module is considered a package or
    330     not.  On modules this is reflected by the `__path__` attribute.
    331 
    332     `origin` is the specific location used by the loader from which to
    333     load the module, if that information is available.  When filename is
    334     set, origin will match.
    335 
    336     `has_location` indicates that a spec's "origin" reflects a location.
    337     When this is True, `__file__` attribute of the module is set.
    338 
    339     `cached` is the location of the cached bytecode file, if any.  It
    340     corresponds to the `__cached__` attribute.
    341 
    342     `submodule_search_locations` is the sequence of path entries to
    343     search when importing submodules.  If set, is_package should be
    344     True--and False otherwise.
    345 
    346     Packages are simply modules that (may) have submodules.  If a spec
    347     has a non-None value in `submodule_search_locations`, the import
    348     system will consider modules loaded from the spec as packages.
    349 
    350     Only finders (see importlib.abc.MetaPathFinder and
    351     importlib.abc.PathEntryFinder) should modify ModuleSpec instances.
    352 
    353     """
    354 
    355     def __init__(self, name, loader, *, origin=None, loader_state=None,
    356                  is_package=None):
    357         self.name = name
    358         self.loader = loader
    359         self.origin = origin
    360         self.loader_state = loader_state
    361         self.submodule_search_locations = [] if is_package else None
    362 
    363         # file-location attributes
    364         self._set_fileattr = False
    365         self._cached = None
    366 
    367     def __repr__(self):
    368         args = ['name={!r}'.format(self.name),
    369                 'loader={!r}'.format(self.loader)]
    370         if self.origin is not None:
    371             args.append('origin={!r}'.format(self.origin))
    372         if self.submodule_search_locations is not None:
    373             args.append('submodule_search_locations={}'
    374                         .format(self.submodule_search_locations))
    375         return '{}({})'.format(self.__class__.__name__, ', '.join(args))
    376 
    377     def __eq__(self, other):
    378         smsl = self.submodule_search_locations
    379         try:
    380             return (self.name == other.name and
    381                     self.loader == other.loader and
    382                     self.origin == other.origin and
    383                     smsl == other.submodule_search_locations and
    384                     self.cached == other.cached and
    385                     self.has_location == other.has_location)
    386         except AttributeError:
    387             return False
    388 
    389     @property
    390     def cached(self):
    391         if self._cached is None:
    392             if self.origin is not None and self._set_fileattr:
    393                 if _bootstrap_external is None:
    394                     raise NotImplementedError
    395                 self._cached = _bootstrap_external._get_cached(self.origin)
    396         return self._cached
    397 
    398     @cached.setter
    399     def cached(self, cached):
    400         self._cached = cached
    401 
    402     @property
    403     def parent(self):
    404         """The name of the module's parent."""
    405         if self.submodule_search_locations is None:
    406             return self.name.rpartition('.')[0]
    407         else:
    408             return self.name
    409 
    410     @property
    411     def has_location(self):
    412         return self._set_fileattr
    413 
    414     @has_location.setter
    415     def has_location(self, value):
    416         self._set_fileattr = bool(value)
    417 
    418 
    419 def spec_from_loader(name, loader, *, origin=None, is_package=None):
    420     """Return a module spec based on various loader methods."""
    421     if hasattr(loader, 'get_filename'):
    422         if _bootstrap_external is None:
    423             raise NotImplementedError
    424         spec_from_file_location = _bootstrap_external.spec_from_file_location
    425 
    426         if is_package is None:
    427             return spec_from_file_location(name, loader=loader)
    428         search = [] if is_package else None
    429         return spec_from_file_location(name, loader=loader,
    430                                        submodule_search_locations=search)
    431 
    432     if is_package is None:
    433         if hasattr(loader, 'is_package'):
    434             try:
    435                 is_package = loader.is_package(name)
    436             except ImportError:
    437                 is_package = None  # aka, undefined
    438         else:
    439             # the default
    440             is_package = False
    441 
    442     return ModuleSpec(name, loader, origin=origin, is_package=is_package)
    443 
    444 
    445 _POPULATE = object()
    446 
    447 
    448 def _spec_from_module(module, loader=None, origin=None):
    449     # This function is meant for use in _setup().
    450     try:
    451         spec = module.__spec__
    452     except AttributeError:
    453         pass
    454     else:
    455         if spec is not None:
    456             return spec
    457 
    458     name = module.__name__
    459     if loader is None:
    460         try:
    461             loader = module.__loader__
    462         except AttributeError:
    463             # loader will stay None.
    464             pass
    465     try:
    466         location = module.__file__
    467     except AttributeError:
    468         location = None
    469     if origin is None:
    470         if location is None:
    471             try:
    472                 origin = loader._ORIGIN
    473             except AttributeError:
    474                 origin = None
    475         else:
    476             origin = location
    477     try:
    478         cached = module.__cached__
    479     except AttributeError:
    480         cached = None
    481     try:
    482         submodule_search_locations = list(module.__path__)
    483     except AttributeError:
    484         submodule_search_locations = None
    485 
    486     spec = ModuleSpec(name, loader, origin=origin)
    487     spec._set_fileattr = False if location is None else True
    488     spec.cached = cached
    489     spec.submodule_search_locations = submodule_search_locations
    490     return spec
    491 
    492 
    493 def _init_module_attrs(spec, module, *, override=False):
    494     # The passed-in module may be not support attribute assignment,
    495     # in which case we simply don't set the attributes.
    496     # __name__
    497     if (override or getattr(module, '__name__', None) is None):
    498         try:
    499             module.__name__ = spec.name
    500         except AttributeError:
    501             pass
    502     # __loader__
    503     if override or getattr(module, '__loader__', None) is None:
    504         loader = spec.loader
    505         if loader is None:
    506             # A backward compatibility hack.
    507             if spec.submodule_search_locations is not None:
    508                 if _bootstrap_external is None:
    509                     raise NotImplementedError
    510                 _NamespaceLoader = _bootstrap_external._NamespaceLoader
    511 
    512                 loader = _NamespaceLoader.__new__(_NamespaceLoader)
    513                 loader._path = spec.submodule_search_locations
    514         try:
    515             module.__loader__ = loader
    516         except AttributeError:
    517             pass
    518     # __package__
    519     if override or getattr(module, '__package__', None) is None:
    520         try:
    521             module.__package__ = spec.parent
    522         except AttributeError:
    523             pass
    524     # __spec__
    525     try:
    526         module.__spec__ = spec
    527     except AttributeError:
    528         pass
    529     # __path__
    530     if override or getattr(module, '__path__', None) is None:
    531         if spec.submodule_search_locations is not None:
    532             try:
    533                 module.__path__ = spec.submodule_search_locations
    534             except AttributeError:
    535                 pass
    536     # __file__/__cached__
    537     if spec.has_location:
    538         if override or getattr(module, '__file__', None) is None:
    539             try:
    540                 module.__file__ = spec.origin
    541             except AttributeError:
    542                 pass
    543 
    544         if override or getattr(module, '__cached__', None) is None:
    545             if spec.cached is not None:
    546                 try:
    547                     module.__cached__ = spec.cached
    548                 except AttributeError:
    549                     pass
    550     return module
    551 
    552 
    553 def module_from_spec(spec):
    554     """Create a module based on the provided spec."""
    555     # Typically loaders will not implement create_module().
    556     module = None
    557     if hasattr(spec.loader, 'create_module'):
    558         # If create_module() returns `None` then it means default
    559         # module creation should be used.
    560         module = spec.loader.create_module(spec)
    561     elif hasattr(spec.loader, 'exec_module'):
    562         raise ImportError('loaders that define exec_module() '
    563                           'must also define create_module()')
    564     if module is None:
    565         module = _new_module(spec.name)
    566     _init_module_attrs(spec, module)
    567     return module
    568 
    569 
    570 def _module_repr_from_spec(spec):
    571     """Return the repr to use for the module."""
    572     # We mostly replicate _module_repr() using the spec attributes.
    573     name = '?' if spec.name is None else spec.name
    574     if spec.origin is None:
    575         if spec.loader is None:
    576             return '<module {!r}>'.format(name)
    577         else:
    578             return '<module {!r} ({!r})>'.format(name, spec.loader)
    579     else:
    580         if spec.has_location:
    581             return '<module {!r} from {!r}>'.format(name, spec.origin)
    582         else:
    583             return '<module {!r} ({})>'.format(spec.name, spec.origin)
    584 
    585 
    586 # Used by importlib.reload() and _load_module_shim().
    587 def _exec(spec, module):
    588     """Execute the spec's specified module in an existing module's namespace."""
    589     name = spec.name
    590     _imp.acquire_lock()
    591     with _ModuleLockManager(name):
    592         if sys.modules.get(name) is not module:
    593             msg = 'module {!r} not in sys.modules'.format(name)
    594             raise ImportError(msg, name=name)
    595         if spec.loader is None:
    596             if spec.submodule_search_locations is None:
    597                 raise ImportError('missing loader', name=spec.name)
    598             # namespace package
    599             _init_module_attrs(spec, module, override=True)
    600             return module
    601         _init_module_attrs(spec, module, override=True)
    602         if not hasattr(spec.loader, 'exec_module'):
    603             # (issue19713) Once BuiltinImporter and ExtensionFileLoader
    604             # have exec_module() implemented, we can add a deprecation
    605             # warning here.
    606             spec.loader.load_module(name)
    607         else:
    608             spec.loader.exec_module(module)
    609     return sys.modules[name]
    610 
    611 
    612 def _load_backward_compatible(spec):
    613     # (issue19713) Once BuiltinImporter and ExtensionFileLoader
    614     # have exec_module() implemented, we can add a deprecation
    615     # warning here.
    616     spec.loader.load_module(spec.name)
    617     # The module must be in sys.modules at this point!
    618     module = sys.modules[spec.name]
    619     if getattr(module, '__loader__', None) is None:
    620         try:
    621             module.__loader__ = spec.loader
    622         except AttributeError:
    623             pass
    624     if getattr(module, '__package__', None) is None:
    625         try:
    626             # Since module.__path__ may not line up with
    627             # spec.submodule_search_paths, we can't necessarily rely
    628             # on spec.parent here.
    629             module.__package__ = module.__name__
    630             if not hasattr(module, '__path__'):
    631                 module.__package__ = spec.name.rpartition('.')[0]
    632         except AttributeError:
    633             pass
    634     if getattr(module, '__spec__', None) is None:
    635         try:
    636             module.__spec__ = spec
    637         except AttributeError:
    638             pass
    639     return module
    640 
    641 def _load_unlocked(spec):
    642     # A helper for direct use by the import system.
    643     if spec.loader is not None:
    644         # not a namespace package
    645         if not hasattr(spec.loader, 'exec_module'):
    646             return _load_backward_compatible(spec)
    647 
    648     module = module_from_spec(spec)
    649     with _installed_safely(module):
    650         if spec.loader is None:
    651             if spec.submodule_search_locations is None:
    652                 raise ImportError('missing loader', name=spec.name)
    653             # A namespace package so do nothing.
    654         else:
    655             spec.loader.exec_module(module)
    656 
    657     # We don't ensure that the import-related module attributes get
    658     # set in the sys.modules replacement case.  Such modules are on
    659     # their own.
    660     return sys.modules[spec.name]
    661 
    662 # A method used during testing of _load_unlocked() and by
    663 # _load_module_shim().
    664 def _load(spec):
    665     """Return a new module object, loaded by the spec's loader.
    666 
    667     The module is not added to its parent.
    668 
    669     If a module is already in sys.modules, that existing module gets
    670     clobbered.
    671 
    672     """
    673     _imp.acquire_lock()
    674     with _ModuleLockManager(spec.name):
    675         return _load_unlocked(spec)
    676 
    677 
    678 # Loaders #####################################################################
    679 
    680 class BuiltinImporter:
    681 
    682     """Meta path import for built-in modules.
    683 
    684     All methods are either class or static methods to avoid the need to
    685     instantiate the class.
    686 
    687     """
    688 
    689     @staticmethod
    690     def module_repr(module):
    691         """Return repr for the module.
    692 
    693         The method is deprecated.  The import machinery does the job itself.
    694 
    695         """
    696         return '<module {!r} (built-in)>'.format(module.__name__)
    697 
    698     @classmethod
    699     def find_spec(cls, fullname, path=None, target=None):
    700         if path is not None:
    701             return None
    702         if _imp.is_builtin(fullname):
    703             return spec_from_loader(fullname, cls, origin='built-in')
    704         else:
    705             return None
    706 
    707     @classmethod
    708     def find_module(cls, fullname, path=None):
    709         """Find the built-in module.
    710 
    711         If 'path' is ever specified then the search is considered a failure.
    712 
    713         This method is deprecated.  Use find_spec() instead.
    714 
    715         """
    716         spec = cls.find_spec(fullname, path)
    717         return spec.loader if spec is not None else None
    718 
    719     @classmethod
    720     def create_module(self, spec):
    721         """Create a built-in module"""
    722         if spec.name not in sys.builtin_module_names:
    723             raise ImportError('{!r} is not a built-in module'.format(spec.name),
    724                               name=spec.name)
    725         return _call_with_frames_removed(_imp.create_builtin, spec)
    726 
    727     @classmethod
    728     def exec_module(self, module):
    729         """Exec a built-in module"""
    730         _call_with_frames_removed(_imp.exec_builtin, module)
    731 
    732     @classmethod
    733     @_requires_builtin
    734     def get_code(cls, fullname):
    735         """Return None as built-in modules do not have code objects."""
    736         return None
    737 
    738     @classmethod
    739     @_requires_builtin
    740     def get_source(cls, fullname):
    741         """Return None as built-in modules do not have source code."""
    742         return None
    743 
    744     @classmethod
    745     @_requires_builtin
    746     def is_package(cls, fullname):
    747         """Return False as built-in modules are never packages."""
    748         return False
    749 
    750     load_module = classmethod(_load_module_shim)
    751 
    752 
    753 class FrozenImporter:
    754 
    755     """Meta path import for frozen modules.
    756 
    757     All methods are either class or static methods to avoid the need to
    758     instantiate the class.
    759 
    760     """
    761 
    762     @staticmethod
    763     def module_repr(m):
    764         """Return repr for the module.
    765 
    766         The method is deprecated.  The import machinery does the job itself.
    767 
    768         """
    769         return '<module {!r} (frozen)>'.format(m.__name__)
    770 
    771     @classmethod
    772     def find_spec(cls, fullname, path=None, target=None):
    773         if _imp.is_frozen(fullname):
    774             return spec_from_loader(fullname, cls, origin='frozen')
    775         else:
    776             return None
    777 
    778     @classmethod
    779     def find_module(cls, fullname, path=None):
    780         """Find a frozen module.
    781 
    782         This method is deprecated.  Use find_spec() instead.
    783 
    784         """
    785         return cls if _imp.is_frozen(fullname) else None
    786 
    787     @classmethod
    788     def create_module(cls, spec):
    789         """Use default semantics for module creation."""
    790 
    791     @staticmethod
    792     def exec_module(module):
    793         name = module.__spec__.name
    794         if not _imp.is_frozen(name):
    795             raise ImportError('{!r} is not a frozen module'.format(name),
    796                               name=name)
    797         code = _call_with_frames_removed(_imp.get_frozen_object, name)
    798         exec(code, module.__dict__)
    799 
    800     @classmethod
    801     def load_module(cls, fullname):
    802         """Load a frozen module.
    803 
    804         This method is deprecated.  Use exec_module() instead.
    805 
    806         """
    807         return _load_module_shim(cls, fullname)
    808 
    809     @classmethod
    810     @_requires_frozen
    811     def get_code(cls, fullname):
    812         """Return the code object for the frozen module."""
    813         return _imp.get_frozen_object(fullname)
    814 
    815     @classmethod
    816     @_requires_frozen
    817     def get_source(cls, fullname):
    818         """Return None as frozen modules do not have source code."""
    819         return None
    820 
    821     @classmethod
    822     @_requires_frozen
    823     def is_package(cls, fullname):
    824         """Return True if the frozen module is a package."""
    825         return _imp.is_frozen_package(fullname)
    826 
    827 
    828 # Import itself ###############################################################
    829 
    830 class _ImportLockContext:
    831 
    832     """Context manager for the import lock."""
    833 
    834     def __enter__(self):
    835         """Acquire the import lock."""
    836         _imp.acquire_lock()
    837 
    838     def __exit__(self, exc_type, exc_value, exc_traceback):
    839         """Release the import lock regardless of any raised exceptions."""
    840         _imp.release_lock()
    841 
    842 
    843 def _resolve_name(name, package, level):
    844     """Resolve a relative module name to an absolute one."""
    845     bits = package.rsplit('.', level - 1)
    846     if len(bits) < level:
    847         raise ValueError('attempted relative import beyond top-level package')
    848     base = bits[0]
    849     return '{}.{}'.format(base, name) if name else base
    850 
    851 
    852 def _find_spec_legacy(finder, name, path):
    853     # This would be a good place for a DeprecationWarning if
    854     # we ended up going that route.
    855     loader = finder.find_module(name, path)
    856     if loader is None:
    857         return None
    858     return spec_from_loader(name, loader)
    859 
    860 
    861 def _find_spec(name, path, target=None):
    862     """Find a module's spec."""
    863     meta_path = sys.meta_path
    864     if meta_path is None:
    865         # PyImport_Cleanup() is running or has been called.
    866         raise ImportError("sys.meta_path is None, Python is likely "
    867                           "shutting down")
    868 
    869     if not meta_path:
    870         _warnings.warn('sys.meta_path is empty', ImportWarning)
    871 
    872     # We check sys.modules here for the reload case.  While a passed-in
    873     # target will usually indicate a reload there is no guarantee, whereas
    874     # sys.modules provides one.
    875     is_reload = name in sys.modules
    876     for finder in meta_path:
    877         with _ImportLockContext():
    878             try:
    879                 find_spec = finder.find_spec
    880             except AttributeError:
    881                 spec = _find_spec_legacy(finder, name, path)
    882                 if spec is None:
    883                     continue
    884             else:
    885                 spec = find_spec(name, path, target)
    886         if spec is not None:
    887             # The parent import may have already imported this module.
    888             if not is_reload and name in sys.modules:
    889                 module = sys.modules[name]
    890                 try:
    891                     __spec__ = module.__spec__
    892                 except AttributeError:
    893                     # We use the found spec since that is the one that
    894                     # we would have used if the parent module hadn't
    895                     # beaten us to the punch.
    896                     return spec
    897                 else:
    898                     if __spec__ is None:
    899                         return spec
    900                     else:
    901                         return __spec__
    902             else:
    903                 return spec
    904     else:
    905         return None
    906 
    907 
    908 def _sanity_check(name, package, level):
    909     """Verify arguments are "sane"."""
    910     if not isinstance(name, str):
    911         raise TypeError('module name must be str, not {}'.format(type(name)))
    912     if level < 0:
    913         raise ValueError('level must be >= 0')
    914     if level > 0:
    915         if not isinstance(package, str):
    916             raise TypeError('__package__ not set to a string')
    917         elif not package:
    918             raise ImportError('attempted relative import with no known parent '
    919                               'package')
    920         elif package not in sys.modules:
    921             msg = ('Parent module {!r} not loaded, cannot perform relative '
    922                    'import')
    923             raise SystemError(msg.format(package))
    924     if not name and level == 0:
    925         raise ValueError('Empty module name')
    926 
    927 
    928 _ERR_MSG_PREFIX = 'No module named '
    929 _ERR_MSG = _ERR_MSG_PREFIX + '{!r}'
    930 
    931 def _find_and_load_unlocked(name, import_):
    932     path = None
    933     parent = name.rpartition('.')[0]
    934     if parent:
    935         if parent not in sys.modules:
    936             _call_with_frames_removed(import_, parent)
    937         # Crazy side-effects!
    938         if name in sys.modules:
    939             return sys.modules[name]
    940         parent_module = sys.modules[parent]
    941         try:
    942             path = parent_module.__path__
    943         except AttributeError:
    944             msg = (_ERR_MSG + '; {!r} is not a package').format(name, parent)
    945             raise ModuleNotFoundError(msg, name=name) from None
    946     spec = _find_spec(name, path)
    947     if spec is None:
    948         raise ModuleNotFoundError(_ERR_MSG.format(name), name=name)
    949     else:
    950         module = _load_unlocked(spec)
    951     if parent:
    952         # Set the module as an attribute on its parent.
    953         parent_module = sys.modules[parent]
    954         setattr(parent_module, name.rpartition('.')[2], module)
    955     return module
    956 
    957 
    958 def _find_and_load(name, import_):
    959     """Find and load the module, and release the import lock."""
    960     with _ModuleLockManager(name):
    961         return _find_and_load_unlocked(name, import_)
    962 
    963 
    964 def _gcd_import(name, package=None, level=0):
    965     """Import and return the module based on its name, the package the call is
    966     being made from, and the level adjustment.
    967 
    968     This function represents the greatest common denominator of functionality
    969     between import_module and __import__. This includes setting __package__ if
    970     the loader did not.
    971 
    972     """
    973     _sanity_check(name, package, level)
    974     if level > 0:
    975         name = _resolve_name(name, package, level)
    976     _imp.acquire_lock()
    977     if name not in sys.modules:
    978         return _find_and_load(name, _gcd_import)
    979     module = sys.modules[name]
    980     if module is None:
    981         _imp.release_lock()
    982         message = ('import of {} halted; '
    983                    'None in sys.modules'.format(name))
    984         raise ModuleNotFoundError(message, name=name)
    985     _lock_unlock_module(name)
    986     return module
    987 
    988 
    989 def _handle_fromlist(module, fromlist, import_):
    990     """Figure out what __import__ should return.
    991 
    992     The import_ parameter is a callable which takes the name of module to
    993     import. It is required to decouple the function from assuming importlib's
    994     import implementation is desired.
    995 
    996     """
    997     # The hell that is fromlist ...
    998     # If a package was imported, try to import stuff from fromlist.
    999     if hasattr(module, '__path__'):
   1000         if '*' in fromlist:
   1001             fromlist = list(fromlist)
   1002             fromlist.remove('*')
   1003             if hasattr(module, '__all__'):
   1004                 fromlist.extend(module.__all__)
   1005         for x in fromlist:
   1006             if not hasattr(module, x):
   1007                 from_name = '{}.{}'.format(module.__name__, x)
   1008                 try:
   1009                     _call_with_frames_removed(import_, from_name)
   1010                 except ModuleNotFoundError as exc:
   1011                     # Backwards-compatibility dictates we ignore failed
   1012                     # imports triggered by fromlist for modules that don't
   1013                     # exist.
   1014                     if exc.name == from_name:
   1015                         continue
   1016                     raise
   1017     return module
   1018 
   1019 
   1020 def _calc___package__(globals):
   1021     """Calculate what __package__ should be.
   1022 
   1023     __package__ is not guaranteed to be defined or could be set to None
   1024     to represent that its proper value is unknown.
   1025 
   1026     """
   1027     package = globals.get('__package__')
   1028     spec = globals.get('__spec__')
   1029     if package is not None:
   1030         if spec is not None and package != spec.parent:
   1031             _warnings.warn("__package__ != __spec__.parent "
   1032                            f"({package!r} != {spec.parent!r})",
   1033                            ImportWarning, stacklevel=3)
   1034         return package
   1035     elif spec is not None:
   1036         return spec.parent
   1037     else:
   1038         _warnings.warn("can't resolve package from __spec__ or __package__, "
   1039                        "falling back on __name__ and __path__",
   1040                        ImportWarning, stacklevel=3)
   1041         package = globals['__name__']
   1042         if '__path__' not in globals:
   1043             package = package.rpartition('.')[0]
   1044     return package
   1045 
   1046 
   1047 def __import__(name, globals=None, locals=None, fromlist=(), level=0):
   1048     """Import a module.
   1049 
   1050     The 'globals' argument is used to infer where the import is occurring from
   1051     to handle relative imports. The 'locals' argument is ignored. The
   1052     'fromlist' argument specifies what should exist as attributes on the module
   1053     being imported (e.g. ``from module import <fromlist>``).  The 'level'
   1054     argument represents the package location to import from in a relative
   1055     import (e.g. ``from ..pkg import mod`` would have a 'level' of 2).
   1056 
   1057     """
   1058     if level == 0:
   1059         module = _gcd_import(name)
   1060     else:
   1061         globals_ = globals if globals is not None else {}
   1062         package = _calc___package__(globals_)
   1063         module = _gcd_import(name, package, level)
   1064     if not fromlist:
   1065         # Return up to the first dot in 'name'. This is complicated by the fact
   1066         # that 'name' may be relative.
   1067         if level == 0:
   1068             return _gcd_import(name.partition('.')[0])
   1069         elif not name:
   1070             return module
   1071         else:
   1072             # Figure out where to slice the module's name up to the first dot
   1073             # in 'name'.
   1074             cut_off = len(name) - len(name.partition('.')[0])
   1075             # Slice end needs to be positive to alleviate need to special-case
   1076             # when ``'.' not in name``.
   1077             return sys.modules[module.__name__[:len(module.__name__)-cut_off]]
   1078     else:
   1079         return _handle_fromlist(module, fromlist, _gcd_import)
   1080 
   1081 
   1082 def _builtin_from_name(name):
   1083     spec = BuiltinImporter.find_spec(name)
   1084     if spec is None:
   1085         raise ImportError('no built-in module named ' + name)
   1086     return _load_unlocked(spec)
   1087 
   1088 
   1089 def _setup(sys_module, _imp_module):
   1090     """Setup importlib by importing needed built-in modules and injecting them
   1091     into the global namespace.
   1092 
   1093     As sys is needed for sys.modules access and _imp is needed to load built-in
   1094     modules, those two modules must be explicitly passed in.
   1095 
   1096     """
   1097     global _imp, sys
   1098     _imp = _imp_module
   1099     sys = sys_module
   1100 
   1101     # Set up the spec for existing builtin/frozen modules.
   1102     module_type = type(sys)
   1103     for name, module in sys.modules.items():
   1104         if isinstance(module, module_type):
   1105             if name in sys.builtin_module_names:
   1106                 loader = BuiltinImporter
   1107             elif _imp.is_frozen(name):
   1108                 loader = FrozenImporter
   1109             else:
   1110                 continue
   1111             spec = _spec_from_module(module, loader)
   1112             _init_module_attrs(spec, module)
   1113 
   1114     # Directly load built-in modules needed during bootstrap.
   1115     self_module = sys.modules[__name__]
   1116     for builtin_name in ('_warnings',):
   1117         if builtin_name not in sys.modules:
   1118             builtin_module = _builtin_from_name(builtin_name)
   1119         else:
   1120             builtin_module = sys.modules[builtin_name]
   1121         setattr(self_module, builtin_name, builtin_module)
   1122 
   1123     # Directly load the _thread module (needed during bootstrap).
   1124     try:
   1125         thread_module = _builtin_from_name('_thread')
   1126     except ImportError:
   1127         # Python was built without threads
   1128         thread_module = None
   1129     setattr(self_module, '_thread', thread_module)
   1130 
   1131     # Directly load the _weakref module (needed during bootstrap).
   1132     weakref_module = _builtin_from_name('_weakref')
   1133     setattr(self_module, '_weakref', weakref_module)
   1134 
   1135 
   1136 def _install(sys_module, _imp_module):
   1137     """Install importlib as the implementation of import."""
   1138     _setup(sys_module, _imp_module)
   1139 
   1140     sys.meta_path.append(BuiltinImporter)
   1141     sys.meta_path.append(FrozenImporter)
   1142 
   1143     global _bootstrap_external
   1144     import _frozen_importlib_external
   1145     _bootstrap_external = _frozen_importlib_external
   1146     _frozen_importlib_external._install(sys.modules[__name__])
   1147