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