summaryrefslogtreecommitdiff
path: root/Lib/importlib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/importlib')
-rw-r--r--Lib/importlib/_bootstrap.py70
-rw-r--r--Lib/importlib/_bootstrap_external.py99
-rw-r--r--Lib/importlib/util.py28
3 files changed, 103 insertions, 94 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 9eecbfe967..a531a0351d 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -36,23 +36,6 @@ def _new_module(name):
return type(sys)(name)
-class _ManageReload:
-
- """Manages the possible clean-up of sys.modules for load_module()."""
-
- def __init__(self, name):
- self._name = name
-
- def __enter__(self):
- self._is_reload = self._name in sys.modules
-
- def __exit__(self, *args):
- if any(arg is not None for arg in args) and not self._is_reload:
- try:
- del sys.modules[self._name]
- except KeyError:
- pass
-
# Module-level locking ########################################################
# A dict mapping module names to weakrefs of _ModuleLock instances
@@ -270,7 +253,7 @@ def _load_module_shim(self, fullname):
# Module specifications #######################################################
def _module_repr(module):
- # The implementation of ModuleType__repr__().
+ # The implementation of ModuleType.__repr__().
loader = getattr(module, '__loader__', None)
if hasattr(loader, 'module_repr'):
# As soon as BuiltinImporter, FrozenImporter, and NamespaceLoader
@@ -576,9 +559,8 @@ def module_from_spec(spec):
# module creation should be used.
module = spec.loader.create_module(spec)
elif hasattr(spec.loader, 'exec_module'):
- _warnings.warn('starting in Python 3.6, loaders defining exec_module() '
- 'must also define create_module()',
- DeprecationWarning, stacklevel=2)
+ raise ImportError('loaders that define exec_module() '
+ 'must also define create_module()')
if module is None:
module = _new_module(spec.name)
_init_module_attrs(spec, module)
@@ -603,7 +585,7 @@ def _module_repr_from_spec(spec):
# Used by importlib.reload() and _load_module_shim().
def _exec(spec, module):
- """Execute the spec in an existing module's namespace."""
+ """Execute the spec's specified module in an existing module's namespace."""
name = spec.name
_imp.acquire_lock()
with _ModuleLockManager(name):
@@ -877,14 +859,21 @@ def _find_spec_legacy(finder, name, path):
def _find_spec(name, path, target=None):
- """Find a module's loader."""
- if sys.meta_path is not None and not sys.meta_path:
+ """Find a module's spec."""
+ meta_path = sys.meta_path
+ if meta_path is None:
+ # PyImport_Cleanup() is running or has been called.
+ raise ImportError("sys.meta_path is None, Python is likely "
+ "shutting down")
+
+ if not meta_path:
_warnings.warn('sys.meta_path is empty', ImportWarning)
+
# We check sys.modules here for the reload case. While a passed-in
# target will usually indicate a reload there is no guarantee, whereas
# sys.modules provides one.
is_reload = name in sys.modules
- for finder in sys.meta_path:
+ for finder in meta_path:
with _ImportLockContext():
try:
find_spec = finder.find_spec
@@ -925,6 +914,9 @@ def _sanity_check(name, package, level):
if level > 0:
if not isinstance(package, str):
raise TypeError('__package__ not set to a string')
+ elif not package:
+ raise ImportError('attempted relative import with no known parent '
+ 'package')
elif package not in sys.modules:
msg = ('Parent module {!r} not loaded, cannot perform relative '
'import')
@@ -950,10 +942,10 @@ def _find_and_load_unlocked(name, import_):
path = parent_module.__path__
except AttributeError:
msg = (_ERR_MSG + '; {!r} is not a package').format(name, parent)
- raise ImportError(msg, name=name) from None
+ raise ModuleNotFoundError(msg, name=name) from None
spec = _find_spec(name, path)
if spec is None:
- raise ImportError(_ERR_MSG.format(name), name=name)
+ raise ModuleNotFoundError(_ERR_MSG.format(name), name=name)
else:
module = _load_unlocked(spec)
if parent:
@@ -989,10 +981,11 @@ def _gcd_import(name, package=None, level=0):
_imp.release_lock()
message = ('import of {} halted; '
'None in sys.modules'.format(name))
- raise ImportError(message, name=name)
+ raise ModuleNotFoundError(message, name=name)
_lock_unlock_module(name)
return module
+
def _handle_fromlist(module, fromlist, import_):
"""Figure out what __import__ should return.
@@ -1014,13 +1007,12 @@ def _handle_fromlist(module, fromlist, import_):
from_name = '{}.{}'.format(module.__name__, x)
try:
_call_with_frames_removed(import_, from_name)
- except ImportError as exc:
+ except ModuleNotFoundError as exc:
# Backwards-compatibility dictates we ignore failed
# imports triggered by fromlist for modules that don't
# exist.
- if str(exc).startswith(_ERR_MSG_PREFIX):
- if exc.name == from_name:
- continue
+ if exc.name == from_name:
+ continue
raise
return module
@@ -1033,7 +1025,19 @@ def _calc___package__(globals):
"""
package = globals.get('__package__')
- if package is None:
+ spec = globals.get('__spec__')
+ if package is not None:
+ if spec is not None and package != spec.parent:
+ _warnings.warn("__package__ != __spec__.parent "
+ f"({package!r} != {spec.parent!r})",
+ ImportWarning, stacklevel=3)
+ return package
+ elif spec is not None:
+ return spec.parent
+ else:
+ _warnings.warn("can't resolve package from __spec__ or __package__, "
+ "falling back on __name__ and __path__",
+ ImportWarning, stacklevel=3)
package = globals['__name__']
if '__path__' not in globals:
package = package.rpartition('.')[0]
diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py
index e54d6916e8..5cb58ab2f5 100644
--- a/Lib/importlib/_bootstrap_external.py
+++ b/Lib/importlib/_bootstrap_external.py
@@ -137,10 +137,6 @@ _code_type = type(_write_atomic.__code__)
# a .pyc file in text mode the magic number will be wrong; also, the
# Apple MPW compiler swaps their values, botching string constants.
#
-# The magic numbers must be spaced apart at least 2 values, as the
-# -U interpeter flag will cause MAGIC+1 being used. They have been
-# odd numbers for some time now.
-#
# There were a variety of old schemes for setting the magic number.
# The current working scheme is to increment the previous value by
# 10.
@@ -231,6 +227,18 @@ _code_type = type(_write_atomic.__code__)
# Python 3.5b2 3340 (fix dictionary display evaluation order #11205)
# Python 3.5b2 3350 (add GET_YIELD_FROM_ITER opcode #24400)
# Python 3.5.2 3351 (fix BUILD_MAP_UNPACK_WITH_CALL opcode #27286)
+# Python 3.6a0 3360 (add FORMAT_VALUE opcode #25483
+# Python 3.6a0 3361 (lineno delta of code.co_lnotab becomes signed)
+# Python 3.6a1 3370 (16 bit wordcode)
+# Python 3.6a1 3371 (add BUILD_CONST_KEY_MAP opcode #27140)
+# Python 3.6a1 3372 (MAKE_FUNCTION simplification, remove MAKE_CLOSURE
+# #27095)
+# Python 3.6b1 3373 (add BUILD_STRING opcode #27078)
+# Python 3.6b1 3375 (add SETUP_ANNOTATIONS and STORE_ANNOTATION opcodes
+# #27985)
+# Python 3.6b1 3376 (simplify CALL_FUNCTIONs & BUILD_MAP_UNPACK_WITH_CALL)
+# Python 3.6b1 3377 (set __class__ cell from type.__new__ #23722)
+# Python 3.6b2 3378 (add BUILD_TUPLE_UNPACK_WITH_CALL #28257)
#
# MAGIC must change whenever the bytecode emitted by the compiler may no
# longer be understood by older implementations of the eval loop (usually
@@ -239,7 +247,7 @@ _code_type = type(_write_atomic.__code__)
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.
-MAGIC_NUMBER = (3351).to_bytes(2, 'little') + b'\r\n'
+MAGIC_NUMBER = (3378).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
_PYCACHE = '__pycache__'
@@ -276,6 +284,7 @@ def cache_from_source(path, debug_override=None, *, optimization=None):
message = 'debug_override or optimization must be set to None'
raise TypeError(message)
optimization = '' if debug_override else 1
+ path = _os.fspath(path)
head, tail = _path_split(path)
base, sep, rest = tail.rpartition('.')
tag = sys.implementation.cache_tag
@@ -306,6 +315,7 @@ def source_from_cache(path):
"""
if sys.implementation.cache_tag is None:
raise NotImplementedError('sys.implementation.cache_tag is None')
+ path = _os.fspath(path)
head, pycache_filename = _path_split(path)
head, pycache = _path_split(head)
if pycache != _PYCACHE:
@@ -371,14 +381,6 @@ def _calc_mode(path):
return mode
-def _verbose_message(message, *args, verbosity=1):
- """Print the message to stderr if -v/PYTHONVERBOSE is turned on."""
- if sys.flags.verbose >= verbosity:
- if not message.startswith(('#', 'import ')):
- message = '# ' + message
- print(message.format(*args), file=sys.stderr)
-
-
def _check_name(method):
"""Decorator to verify that the module being requested matches the one the
loader can handle.
@@ -448,15 +450,15 @@ def _validate_bytecode_header(data, source_stats=None, name=None, path=None):
raw_size = data[8:12]
if magic != MAGIC_NUMBER:
message = 'bad magic number in {!r}: {!r}'.format(name, magic)
- _verbose_message('{}', message)
+ _bootstrap._verbose_message('{}', message)
raise ImportError(message, **exc_details)
elif len(raw_timestamp) != 4:
message = 'reached EOF while reading timestamp in {!r}'.format(name)
- _verbose_message('{}', message)
+ _bootstrap._verbose_message('{}', message)
raise EOFError(message)
elif len(raw_size) != 4:
message = 'reached EOF while reading size of source in {!r}'.format(name)
- _verbose_message('{}', message)
+ _bootstrap._verbose_message('{}', message)
raise EOFError(message)
if source_stats is not None:
try:
@@ -466,7 +468,7 @@ def _validate_bytecode_header(data, source_stats=None, name=None, path=None):
else:
if _r_long(raw_timestamp) != source_mtime:
message = 'bytecode is stale for {!r}'.format(name)
- _verbose_message('{}', message)
+ _bootstrap._verbose_message('{}', message)
raise ImportError(message, **exc_details)
try:
source_size = source_stats['size'] & 0xFFFFFFFF
@@ -483,7 +485,7 @@ def _compile_bytecode(data, name=None, bytecode_path=None, source_path=None):
"""Compile bytecode as returned by _validate_bytecode_header()."""
code = marshal.loads(data)
if isinstance(code, _code_type):
- _verbose_message('code object from {!r}', bytecode_path)
+ _bootstrap._verbose_message('code object from {!r}', bytecode_path)
if source_path is not None:
_imp._fix_co_filename(code, source_path)
return code
@@ -541,6 +543,8 @@ def spec_from_file_location(name, location=None, *, loader=None,
location = loader.get_filename(name)
except ImportError:
pass
+ else:
+ location = _os.fspath(location)
# If the location is on the filesystem, but doesn't actually exist,
# we could return None here, indicating that the location is not
@@ -610,7 +614,7 @@ class WindowsRegistryFinder:
else:
registry_key = cls.REGISTRY_KEY
key = registry_key.format(fullname=fullname,
- sys_version=sys.version[:3])
+ sys_version='%d.%d' % sys.version_info[:2])
try:
with cls._open_registry(key) as hkey:
filepath = _winreg.QueryValue(hkey, '')
@@ -673,6 +677,7 @@ class _LoaderBasics:
_bootstrap._call_with_frames_removed(exec, code, module.__dict__)
def load_module(self, fullname):
+ """This module is deprecated."""
return _bootstrap._load_module_shim(self, fullname)
@@ -766,21 +771,21 @@ class SourceLoader(_LoaderBasics):
except (ImportError, EOFError):
pass
else:
- _verbose_message('{} matches {}', bytecode_path,
- source_path)
+ _bootstrap._verbose_message('{} matches {}', bytecode_path,
+ source_path)
return _compile_bytecode(bytes_data, name=fullname,
bytecode_path=bytecode_path,
source_path=source_path)
source_bytes = self.get_data(source_path)
code_object = self.source_to_code(source_bytes, source_path)
- _verbose_message('code object from {}', source_path)
+ _bootstrap._verbose_message('code object from {}', source_path)
if (not sys.dont_write_bytecode and bytecode_path is not None and
source_mtime is not None):
data = _code_to_bytecode(code_object, source_mtime,
len(source_bytes))
try:
self._cache_bytecode(source_path, bytecode_path, data)
- _verbose_message('wrote {!r}', bytecode_path)
+ _bootstrap._verbose_message('wrote {!r}', bytecode_path)
except NotImplementedError:
pass
return code_object
@@ -860,14 +865,16 @@ class SourceFileLoader(FileLoader, SourceLoader):
except OSError as exc:
# Could be a permission error, read-only filesystem: just forget
# about writing the data.
- _verbose_message('could not create {!r}: {!r}', parent, exc)
+ _bootstrap._verbose_message('could not create {!r}: {!r}',
+ parent, exc)
return
try:
_write_atomic(path, data, _mode)
- _verbose_message('created {!r}', path)
+ _bootstrap._verbose_message('created {!r}', path)
except OSError as exc:
# Same as above: just don't write the bytecode.
- _verbose_message('could not create {!r}: {!r}', path, exc)
+ _bootstrap._verbose_message('could not create {!r}: {!r}', path,
+ exc)
class SourcelessFileLoader(FileLoader, _LoaderBasics):
@@ -912,14 +919,14 @@ class ExtensionFileLoader(FileLoader, _LoaderBasics):
"""Create an unitialized extension module"""
module = _bootstrap._call_with_frames_removed(
_imp.create_dynamic, spec)
- _verbose_message('extension module {!r} loaded from {!r}',
+ _bootstrap._verbose_message('extension module {!r} loaded from {!r}',
spec.name, self.path)
return module
def exec_module(self, module):
"""Initialize an extension module"""
_bootstrap._call_with_frames_removed(_imp.exec_dynamic, module)
- _verbose_message('extension module {!r} executed from {!r}',
+ _bootstrap._verbose_message('extension module {!r} executed from {!r}',
self.name, self.path)
def is_package(self, fullname):
@@ -985,6 +992,9 @@ class _NamespacePath:
def __iter__(self):
return iter(self._recalculate())
+ def __setitem__(self, index, path):
+ self._path[index] = path
+
def __len__(self):
return len(self._recalculate())
@@ -1034,7 +1044,8 @@ class _NamespaceLoader:
"""
# The import system never calls this method.
- _verbose_message('namespace module loaded with path {!r}', self._path)
+ _bootstrap._verbose_message('namespace module loaded with path {!r}',
+ self._path)
return _bootstrap._load_module_shim(self, fullname)
@@ -1054,11 +1065,7 @@ class PathFinder:
@classmethod
def _path_hooks(cls, path):
- """Search sequence of hooks for a finder for 'path'.
-
- If 'hooks' is false then use sys.path_hooks.
-
- """
+ """Search sys.path_hooks for a finder for 'path'."""
if sys.path_hooks is not None and not sys.path_hooks:
_warnings.warn('sys.path_hooks is empty', ImportWarning)
for hook in sys.path_hooks:
@@ -1140,8 +1147,10 @@ class PathFinder:
@classmethod
def find_spec(cls, fullname, path=None, target=None):
- """find the module on sys.path or 'path' based on sys.path_hooks and
- sys.path_importer_cache."""
+ """Try to find a spec for 'fullname' on sys.path or 'path'.
+
+ The search is based on sys.path_hooks and sys.path_importer_cache.
+ """
if path is None:
path = sys.path
spec = cls._get_spec(fullname, path, target)
@@ -1221,8 +1230,10 @@ class FileFinder:
submodule_search_locations=smsl)
def find_spec(self, fullname, target=None):
- """Try to find a spec for the specified module. Returns the
- matching spec, or None if not found."""
+ """Try to find a spec for the specified module.
+
+ Returns the matching spec, or None if not found.
+ """
is_namespace = False
tail_module = fullname.rpartition('.')[2]
try:
@@ -1254,12 +1265,13 @@ class FileFinder:
# Check for a file w/ a proper suffix exists.
for suffix, loader_class in self._loaders:
full_path = _path_join(self.path, tail_module + suffix)
- _verbose_message('trying {}'.format(full_path), verbosity=2)
+ _bootstrap._verbose_message('trying {}', full_path, verbosity=2)
if cache_module + suffix in cache:
if _path_isfile(full_path):
- return self._get_spec(loader_class, fullname, full_path, None, target)
+ return self._get_spec(loader_class, fullname, full_path,
+ None, target)
if is_namespace:
- _verbose_message('possible namespace for {}'.format(base_path))
+ _bootstrap._verbose_message('possible namespace for {}', base_path)
spec = _bootstrap.ModuleSpec(fullname, None)
spec.submodule_search_locations = [base_path]
return spec
@@ -1430,8 +1442,3 @@ def _install(_bootstrap_module):
if _os.__name__ == 'nt':
sys.meta_path.append(WindowsRegistryFinder)
sys.meta_path.append(PathFinder)
-
- # XXX We expose a couple of classes in _bootstrap for the sake of
- # a setuptools bug (https://bitbucket.org/pypa/setuptools/issue/378).
- _bootstrap_module.FileFinder = FileFinder
- _bootstrap_module.SourceFileLoader = SourceFileLoader
diff --git a/Lib/importlib/util.py b/Lib/importlib/util.py
index e1fa07a664..6bdf0d445d 100644
--- a/Lib/importlib/util.py
+++ b/Lib/importlib/util.py
@@ -22,8 +22,8 @@ def resolve_name(name, package):
if not name.startswith('.'):
return name
elif not package:
- raise ValueError('{!r} is not a relative name '
- '(no leading dot)'.format(name))
+ raise ValueError(f'no package specified for {repr(name)} '
+ '(required for relative module names)')
level = 0
for character in name:
if character != '.':
@@ -204,11 +204,6 @@ def module_for_loader(fxn):
return module_for_loader_wrapper
-class _Module(types.ModuleType):
-
- """A subclass of the module type to allow __class__ manipulation."""
-
-
class _LazyModule(types.ModuleType):
"""A subclass of the module type which triggers loading upon attribute access."""
@@ -218,13 +213,14 @@ class _LazyModule(types.ModuleType):
# All module metadata must be garnered from __spec__ in order to avoid
# using mutated values.
# Stop triggering this method.
- self.__class__ = _Module
+ self.__class__ = types.ModuleType
# Get the original name to make sure no object substitution occurred
# in sys.modules.
original_name = self.__spec__.name
# Figure out exactly what attributes were mutated between the creation
# of the module and now.
- attrs_then = self.__spec__.loader_state
+ attrs_then = self.__spec__.loader_state['__dict__']
+ original_type = self.__spec__.loader_state['__class__']
attrs_now = self.__dict__
attrs_updated = {}
for key, value in attrs_now.items():
@@ -239,9 +235,9 @@ class _LazyModule(types.ModuleType):
# object was put into sys.modules.
if original_name in sys.modules:
if id(self) != id(sys.modules[original_name]):
- msg = ('module object for {!r} substituted in sys.modules '
- 'during a lazy load')
- raise ValueError(msg.format(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.
self.__dict__.update(attrs_updated)
@@ -275,8 +271,7 @@ class LazyLoader(abc.Loader):
self.loader = loader
def create_module(self, spec):
- """Create a module which can have its __class__ manipulated."""
- return _Module(spec.name)
+ return self.loader.create_module(spec)
def exec_module(self, module):
"""Make the module load lazily."""
@@ -286,5 +281,8 @@ class LazyLoader(abc.Loader):
# on an object would have triggered the load,
# e.g. ``module.__spec__.loader = None`` would trigger a load from
# trying to access module.__spec__.
- module.__spec__.loader_state = module.__dict__.copy()
+ loader_state = {}
+ loader_state['__dict__'] = module.__dict__.copy()
+ loader_state['__class__'] = module.__class__
+ module.__spec__.loader_state = loader_state
module.__class__ = _LazyModule