2009-02-16 22:45:03 -04:00
|
|
|
"""Utility code for constructing importers, etc."""
|
2020-06-17 18:15:59 -03:00
|
|
|
from ._abc import Loader
|
2014-05-30 15:55:29 -03:00
|
|
|
from ._bootstrap import module_from_spec
|
2012-05-13 14:45:09 -03:00
|
|
|
from ._bootstrap import _resolve_name
|
2015-05-02 22:15:18 -03:00
|
|
|
from ._bootstrap import spec_from_loader
|
2014-01-25 18:32:46 -04:00
|
|
|
from ._bootstrap import _find_spec
|
2015-05-02 22:15:18 -03:00
|
|
|
from ._bootstrap_external import MAGIC_NUMBER
|
2017-12-09 14:26:52 -04:00
|
|
|
from ._bootstrap_external import _RAW_MAGIC_NUMBER
|
2015-05-02 22:15:18 -03:00
|
|
|
from ._bootstrap_external import cache_from_source
|
|
|
|
from ._bootstrap_external import decode_source
|
|
|
|
from ._bootstrap_external import source_from_cache
|
|
|
|
from ._bootstrap_external import spec_from_file_location
|
2012-05-13 14:45:09 -03:00
|
|
|
|
2017-12-09 14:26:52 -04:00
|
|
|
import _imp
|
2013-11-22 12:05:39 -04:00
|
|
|
import sys
|
2014-04-04 14:53:38 -03:00
|
|
|
import types
|
2013-05-31 19:56:47 -03:00
|
|
|
|
2012-05-13 14:45:09 -03:00
|
|
|
|
2017-12-09 14:26:52 -04:00
|
|
|
def source_hash(source_bytes):
|
|
|
|
"Return the hash of *source_bytes* as used in hash-based pyc files."
|
|
|
|
return _imp.source_hash(_RAW_MAGIC_NUMBER, source_bytes)
|
|
|
|
|
|
|
|
|
2012-05-13 14:45:09 -03:00
|
|
|
def resolve_name(name, package):
|
|
|
|
"""Resolve a relative module name to an absolute one."""
|
|
|
|
if not name.startswith('.'):
|
|
|
|
return name
|
|
|
|
elif not package:
|
2019-08-03 02:46:02 -03:00
|
|
|
raise ImportError(f'no package specified for {repr(name)} '
|
|
|
|
'(required for relative module names)')
|
2012-05-13 14:45:09 -03:00
|
|
|
level = 0
|
|
|
|
for character in name:
|
|
|
|
if character != '.':
|
|
|
|
break
|
|
|
|
level += 1
|
|
|
|
return _resolve_name(name[level:], package, level)
|
2013-05-31 19:56:47 -03:00
|
|
|
|
|
|
|
|
2014-01-25 18:32:46 -04:00
|
|
|
def _find_spec_from_path(name, path=None):
|
|
|
|
"""Return the spec for the specified module.
|
|
|
|
|
|
|
|
First, sys.modules is checked to see if the module was already imported. If
|
|
|
|
so, then sys.modules[name].__spec__ is returned. If that happens to be
|
|
|
|
set to None, then ValueError is raised. If the module is not in
|
|
|
|
sys.modules, then sys.meta_path is searched for a suitable spec with the
|
|
|
|
value of 'path' given to the finders. None is returned if no spec could
|
|
|
|
be found.
|
|
|
|
|
|
|
|
Dotted names do not have their parent packages implicitly imported. You will
|
|
|
|
most likely need to explicitly import all parent packages in the proper
|
|
|
|
order for a submodule to get the correct spec.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if name not in sys.modules:
|
|
|
|
return _find_spec(name, path)
|
|
|
|
else:
|
|
|
|
module = sys.modules[name]
|
|
|
|
if module is None:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
spec = module.__spec__
|
|
|
|
except AttributeError:
|
2022-10-06 22:27:51 -03:00
|
|
|
raise ValueError(f'{name}.__spec__ is not set') from None
|
2014-01-25 18:32:46 -04:00
|
|
|
else:
|
|
|
|
if spec is None:
|
2022-10-06 22:27:51 -03:00
|
|
|
raise ValueError(f'{name}.__spec__ is None')
|
2014-01-25 18:32:46 -04:00
|
|
|
return spec
|
|
|
|
|
|
|
|
|
|
|
|
def find_spec(name, package=None):
|
|
|
|
"""Return the spec for the specified module.
|
|
|
|
|
|
|
|
First, sys.modules is checked to see if the module was already imported. If
|
|
|
|
so, then sys.modules[name].__spec__ is returned. If that happens to be
|
|
|
|
set to None, then ValueError is raised. If the module is not in
|
|
|
|
sys.modules, then sys.meta_path is searched for a suitable spec with the
|
|
|
|
value of 'path' given to the finders. None is returned if no spec could
|
|
|
|
be found.
|
|
|
|
|
|
|
|
If the name is for submodule (contains a dot), the parent module is
|
|
|
|
automatically imported.
|
|
|
|
|
|
|
|
The name and package arguments work the same as importlib.import_module().
|
|
|
|
In other words, relative module names (with leading dots) work.
|
|
|
|
|
|
|
|
"""
|
|
|
|
fullname = resolve_name(name, package) if name.startswith('.') else name
|
|
|
|
if fullname not in sys.modules:
|
|
|
|
parent_name = fullname.rpartition('.')[0]
|
|
|
|
if parent_name:
|
|
|
|
parent = __import__(parent_name, fromlist=['__path__'])
|
2017-06-14 18:34:50 -03:00
|
|
|
try:
|
|
|
|
parent_path = parent.__path__
|
|
|
|
except AttributeError as e:
|
|
|
|
raise ModuleNotFoundError(
|
2018-06-07 03:02:24 -03:00
|
|
|
f"__path__ attribute not found on {parent_name!r} "
|
2017-06-14 18:34:50 -03:00
|
|
|
f"while trying to find {fullname!r}", name=fullname) from e
|
2014-01-25 18:32:46 -04:00
|
|
|
else:
|
2017-06-14 18:34:50 -03:00
|
|
|
parent_path = None
|
|
|
|
return _find_spec(fullname, parent_path)
|
2014-01-25 18:32:46 -04:00
|
|
|
else:
|
|
|
|
module = sys.modules[fullname]
|
|
|
|
if module is None:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
spec = module.__spec__
|
|
|
|
except AttributeError:
|
2022-10-06 22:27:51 -03:00
|
|
|
raise ValueError(f'{name}.__spec__ is not set') from None
|
2014-01-25 18:32:46 -04:00
|
|
|
else:
|
|
|
|
if spec is None:
|
2022-10-06 22:27:51 -03:00
|
|
|
raise ValueError(f'{name}.__spec__ is None')
|
2014-01-25 18:32:46 -04:00
|
|
|
return spec
|
|
|
|
|
|
|
|
|
2023-05-08 19:56:01 -03:00
|
|
|
# Normally we would use contextlib.contextmanager. However, this module
|
|
|
|
# is imported by runpy, which means we want to avoid any unnecessary
|
|
|
|
# dependencies. Thus we use a class.
|
|
|
|
|
2023-06-08 15:19:58 -03:00
|
|
|
class _incompatible_extension_module_restrictions:
|
|
|
|
"""A context manager that can temporarily skip the compatibility check.
|
|
|
|
|
|
|
|
NOTE: This function is meant to accommodate an unusual case; one
|
|
|
|
which is likely to eventually go away. There's is a pretty good
|
|
|
|
chance this is not what you were looking for.
|
|
|
|
|
|
|
|
WARNING: Using this function to disable the check can lead to
|
|
|
|
unexpected behavior and even crashes. It should only be used during
|
|
|
|
extension module development.
|
|
|
|
|
|
|
|
If "disable_check" is True then the compatibility check will not
|
|
|
|
happen while the context manager is active. Otherwise the check
|
|
|
|
*will* happen.
|
2023-05-08 19:56:01 -03:00
|
|
|
|
|
|
|
Normally, extensions that do not support multiple interpreters
|
|
|
|
may not be imported in a subinterpreter. That implies modules
|
2023-06-08 15:19:58 -03:00
|
|
|
that do not implement multi-phase init or that explicitly of out.
|
2023-05-08 19:56:01 -03:00
|
|
|
|
2023-10-18 19:09:45 -03:00
|
|
|
Likewise for modules import in a subinterpreter with its own GIL
|
2023-05-08 19:56:01 -03:00
|
|
|
when the extension does not support a per-interpreter GIL. This
|
|
|
|
implies the module does not have a Py_mod_multiple_interpreters slot
|
|
|
|
set to Py_MOD_PER_INTERPRETER_GIL_SUPPORTED.
|
|
|
|
|
|
|
|
In both cases, this context manager may be used to temporarily
|
|
|
|
disable the check for compatible extension modules.
|
2023-06-08 15:19:58 -03:00
|
|
|
|
|
|
|
You can get the same effect as this function by implementing the
|
|
|
|
basic interface of multi-phase init (PEP 489) and lying about
|
2024-03-05 12:05:52 -04:00
|
|
|
support for multiple interpreters (or per-interpreter GIL).
|
2023-05-08 19:56:01 -03:00
|
|
|
"""
|
|
|
|
|
2023-06-08 15:19:58 -03:00
|
|
|
def __init__(self, *, disable_check):
|
|
|
|
self.disable_check = bool(disable_check)
|
2023-05-08 19:56:01 -03:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.old = _imp._override_multi_interp_extensions_check(self.override)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
old = self.old
|
|
|
|
del self.old
|
|
|
|
_imp._override_multi_interp_extensions_check(old)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def override(self):
|
|
|
|
return -1 if self.disable_check else 1
|
|
|
|
|
|
|
|
|
2014-04-04 14:53:38 -03:00
|
|
|
class _LazyModule(types.ModuleType):
|
|
|
|
|
|
|
|
"""A subclass of the module type which triggers loading upon attribute access."""
|
|
|
|
|
|
|
|
def __getattribute__(self, attr):
|
|
|
|
"""Trigger the load of the module and return the attribute."""
|
2024-02-23 20:02:16 -04:00
|
|
|
__spec__ = object.__getattribute__(self, '__spec__')
|
|
|
|
loader_state = __spec__.loader_state
|
|
|
|
with loader_state['lock']:
|
|
|
|
# Only the first thread to get the lock should trigger the load
|
|
|
|
# and reset the module's class. The rest can now getattr().
|
|
|
|
if object.__getattribute__(self, '__class__') is _LazyModule:
|
2024-04-09 00:08:48 -03:00
|
|
|
__class__ = loader_state['__class__']
|
|
|
|
|
2024-03-28 07:59:31 -03:00
|
|
|
# Reentrant calls from the same thread must be allowed to proceed without
|
|
|
|
# triggering the load again.
|
|
|
|
# exec_module() and self-referential imports are the primary ways this can
|
|
|
|
# happen, but in any case we must return something to avoid deadlock.
|
|
|
|
if loader_state['is_loading']:
|
2024-04-09 00:08:48 -03:00
|
|
|
return __class__.__getattribute__(self, attr)
|
2024-02-23 20:02:16 -04:00
|
|
|
loader_state['is_loading'] = True
|
|
|
|
|
2024-04-09 00:08:48 -03:00
|
|
|
__dict__ = __class__.__getattribute__(self, '__dict__')
|
2024-02-23 20:02:16 -04:00
|
|
|
|
|
|
|
# All module metadata must be gathered from __spec__ in order to avoid
|
|
|
|
# using mutated values.
|
|
|
|
# Get the original name to make sure no object substitution occurred
|
|
|
|
# in sys.modules.
|
|
|
|
original_name = __spec__.name
|
|
|
|
# Figure out exactly what attributes were mutated between the creation
|
|
|
|
# of the module and now.
|
|
|
|
attrs_then = loader_state['__dict__']
|
|
|
|
attrs_now = __dict__
|
|
|
|
attrs_updated = {}
|
|
|
|
for key, value in attrs_now.items():
|
|
|
|
# Code that set an attribute may have kept a reference to the
|
|
|
|
# assigned object, making identity more important than equality.
|
|
|
|
if key not in attrs_then:
|
|
|
|
attrs_updated[key] = value
|
|
|
|
elif id(attrs_now[key]) != id(attrs_then[key]):
|
|
|
|
attrs_updated[key] = value
|
|
|
|
__spec__.loader.exec_module(self)
|
|
|
|
# If exec_module() was used directly there is no guarantee the module
|
|
|
|
# object was put into sys.modules.
|
|
|
|
if original_name in sys.modules:
|
|
|
|
if id(self) != id(sys.modules[original_name]):
|
|
|
|
raise ValueError(f"module object for {original_name!r} "
|
|
|
|
"substituted in sys.modules during a lazy "
|
|
|
|
"load")
|
|
|
|
# Update after loading since that's what would happen in an eager
|
|
|
|
# loading situation.
|
|
|
|
__dict__.update(attrs_updated)
|
2024-04-09 00:08:48 -03:00
|
|
|
# Finally, stop triggering this method, if the module did not
|
|
|
|
# already update its own __class__.
|
|
|
|
if isinstance(self, _LazyModule):
|
|
|
|
object.__setattr__(self, '__class__', __class__)
|
2024-02-23 20:02:16 -04:00
|
|
|
|
2014-04-04 14:53:38 -03:00
|
|
|
return getattr(self, attr)
|
|
|
|
|
|
|
|
def __delattr__(self, attr):
|
|
|
|
"""Trigger the load and then perform the deletion."""
|
|
|
|
# To trigger the load and raise an exception if the attribute
|
|
|
|
# doesn't exist.
|
|
|
|
self.__getattribute__(attr)
|
|
|
|
delattr(self, attr)
|
|
|
|
|
|
|
|
|
2020-06-17 18:15:59 -03:00
|
|
|
class LazyLoader(Loader):
|
2014-04-04 14:53:38 -03:00
|
|
|
|
|
|
|
"""A loader that creates a module which defers loading until attribute access."""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __check_eager_loader(loader):
|
|
|
|
if not hasattr(loader, 'exec_module'):
|
|
|
|
raise TypeError('loader must define exec_module()')
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def factory(cls, loader):
|
|
|
|
"""Construct a callable which returns the eager loader made lazy."""
|
|
|
|
cls.__check_eager_loader(loader)
|
|
|
|
return lambda *args, **kwargs: cls(loader(*args, **kwargs))
|
|
|
|
|
|
|
|
def __init__(self, loader):
|
|
|
|
self.__check_eager_loader(loader)
|
|
|
|
self.loader = loader
|
|
|
|
|
|
|
|
def create_module(self, spec):
|
2016-06-25 14:58:17 -03:00
|
|
|
return self.loader.create_module(spec)
|
2014-04-04 14:53:38 -03:00
|
|
|
|
|
|
|
def exec_module(self, module):
|
|
|
|
"""Make the module load lazily."""
|
2024-07-03 18:14:42 -03:00
|
|
|
# Threading is only needed for lazy loading, and importlib.util can
|
|
|
|
# be pulled in at interpreter startup, so defer until needed.
|
|
|
|
import threading
|
2014-04-04 14:53:38 -03:00
|
|
|
module.__spec__.loader = self.loader
|
|
|
|
module.__loader__ = self.loader
|
|
|
|
# Don't need to worry about deep-copying as trying to set an attribute
|
|
|
|
# on an object would have triggered the load,
|
|
|
|
# e.g. ``module.__spec__.loader = None`` would trigger a load from
|
|
|
|
# trying to access module.__spec__.
|
2016-06-25 14:58:17 -03:00
|
|
|
loader_state = {}
|
|
|
|
loader_state['__dict__'] = module.__dict__.copy()
|
|
|
|
loader_state['__class__'] = module.__class__
|
2024-02-23 20:02:16 -04:00
|
|
|
loader_state['lock'] = threading.RLock()
|
|
|
|
loader_state['is_loading'] = False
|
2016-06-25 14:58:17 -03:00
|
|
|
module.__spec__.loader_state = loader_state
|
2014-04-04 14:53:38 -03:00
|
|
|
module.__class__ = _LazyModule
|