diff options
Diffstat (limited to 'Lib/importlib/__init__.py')
-rw-r--r-- | Lib/importlib/__init__.py | 144 |
1 files changed, 54 insertions, 90 deletions
diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py index 2baaf93732..6f40dacf13 100644 --- a/Lib/importlib/__init__.py +++ b/Lib/importlib/__init__.py @@ -1,108 +1,72 @@ -"""A pure Python implementation of import. - -References on import: - - * Language reference - http://docs.python.org/ref/import.html - * __import__ function - http://docs.python.org/lib/built-in-funcs.html - * Packages - http://www.python.org/doc/essays/packages.html - * PEP 235: Import on Case-Insensitive Platforms - http://www.python.org/dev/peps/pep-0235 - * PEP 275: Import Modules from Zip Archives - http://www.python.org/dev/peps/pep-0273 - * PEP 302: New Import Hooks - http://www.python.org/dev/peps/pep-0302/ - * PEP 328: Imports: Multi-line and Absolute/Relative - http://www.python.org/dev/peps/pep-0328 - -""" -__all__ = ['__import__', 'import_module'] - -from . import _bootstrap - -import os -import re -import tokenize +"""A pure Python implementation of import.""" +__all__ = ['__import__', 'import_module', 'invalidate_caches'] # Bootstrap help ##################################################### -def _case_ok(directory, check): - """Check if the directory contains something matching 'check'. +# Until bootstrapping is complete, DO NOT import any modules that attempt +# to import importlib._bootstrap (directly or indirectly). Since this +# partially initialised package would be present in sys.modules, those +# modules would get an uninitialised copy of the source version, instead +# of a fully initialised version (either the frozen one or the one +# initialised below if the frozen one is not available). +import _imp # Just the builtin component, NOT the full Python module +import sys - No check is done if the file/directory exists or not. +try: + import _frozen_importlib as _bootstrap +except ImportError: + from . import _bootstrap + _bootstrap._setup(sys, _imp) +else: + # importlib._bootstrap is the built-in import, ensure we don't create + # a second copy of the module. + _bootstrap.__name__ = 'importlib._bootstrap' + _bootstrap.__package__ = 'importlib' + _bootstrap.__file__ = __file__.replace('__init__.py', '_bootstrap.py') + sys.modules['importlib._bootstrap'] = _bootstrap + +# To simplify imports in test code +_w_long = _bootstrap._w_long +_r_long = _bootstrap._r_long + +# Fully bootstrapped at this point, import whatever you like, circular +# dependencies and startup overhead minimisation permitting :) - """ - if 'PYTHONCASEOK' in os.environ: - return True - elif check in os.listdir(directory if directory else os.getcwd()): - return True - return False +# Public API ######################################################### +from ._bootstrap import __import__ -def _w_long(x): - """Convert a 32-bit integer to little-endian. - XXX Temporary until marshal's long functions are exposed. +def invalidate_caches(): + """Call the invalidate_caches() method on all meta path finders stored in + sys.meta_path (where implemented).""" + for finder in sys.meta_path: + if hasattr(finder, 'invalidate_caches'): + finder.invalidate_caches() - """ - x = int(x) - int_bytes = [] - int_bytes.append(x & 0xFF) - int_bytes.append((x >> 8) & 0xFF) - int_bytes.append((x >> 16) & 0xFF) - int_bytes.append((x >> 24) & 0xFF) - return bytearray(int_bytes) +def find_loader(name, path=None): + """Find the loader for the specified module. -def _r_long(int_bytes): - """Convert 4 bytes in little-endian to an integer. + First, sys.modules is checked to see if the module was already imported. If + so, then sys.modules[name].__loader__ 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 loader with the + value of 'path' given to the finders. None is returned if no loader could + be found. - XXX Temporary until marshal's long function are exposed. + Dotted names do not have their parent packages implicitly imported. """ - x = int_bytes[0] - x |= int_bytes[1] << 8 - x |= int_bytes[2] << 16 - x |= int_bytes[3] << 24 - return x - - -# Required built-in modules. -try: - import posix as _os -except ImportError: try: - import nt as _os - except ImportError: - try: - import os2 as _os - except ImportError: - raise ImportError('posix, nt, or os2 module required for importlib') -_bootstrap._os = _os -import imp, sys, marshal, errno, _io -_bootstrap.imp = imp -_bootstrap.sys = sys -_bootstrap.marshal = marshal -_bootstrap.errno = errno -_bootstrap._io = _io -import _warnings -_bootstrap._warnings = _warnings - - -from os import sep -# For os.path.join replacement; pull from Include/osdefs.h:SEP . -_bootstrap.path_sep = sep - -_bootstrap._case_ok = _case_ok -marshal._w_long = _w_long -marshal._r_long = _r_long - - -# Public API ######################################################### - -from ._bootstrap import __import__ + loader = sys.modules[name].__loader__ + if loader is None: + raise ValueError('{}.__loader__ is None'.format(name)) + else: + return loader + except KeyError: + pass + return _bootstrap._find_module(name, path) def import_module(name, package=None): |