diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2013-11-22 16:05:39 (GMT) |
---|---|---|
committer | Eric Snow <ericsnowcurrently@gmail.com> | 2013-11-22 16:05:39 (GMT) |
commit | b523f8433a8982e10eb41a3e2b37ee0e6d6a6e00 (patch) | |
tree | b38661db4903b7edc4042e7562b32720dd3687bf /Lib/imp.py | |
parent | 9e6097ebe7bb99a4a22b949ef4b1563b21ad7166 (diff) | |
download | cpython-b523f8433a8982e10eb41a3e2b37ee0e6d6a6e00.zip cpython-b523f8433a8982e10eb41a3e2b37ee0e6d6a6e00.tar.gz cpython-b523f8433a8982e10eb41a3e2b37ee0e6d6a6e00.tar.bz2 |
Implement PEP 451 (ModuleSpec).
Diffstat (limited to 'Lib/imp.py')
-rw-r--r-- | Lib/imp.py | 30 |
1 files changed, 24 insertions, 6 deletions
@@ -16,7 +16,7 @@ except ImportError: # Platform doesn't support dynamic loading. load_dynamic = None -from importlib._bootstrap import SourcelessFileLoader, _ERR_MSG +from importlib._bootstrap import SourcelessFileLoader, _ERR_MSG, _SpecMethods from importlib import machinery from importlib import util @@ -162,11 +162,17 @@ class _LoadSourceCompatibility(_HackedGetData, machinery.SourceFileLoader): def load_source(name, pathname, file=None): - _LoadSourceCompatibility(name, pathname, file).load_module(name) - module = sys.modules[name] + loader = _LoadSourceCompatibility(name, pathname, file) + spec = util.spec_from_file_location(name, pathname, loader=loader) + methods = _SpecMethods(spec) + if name in sys.modules: + module = methods.exec(sys.modules[name]) + else: + module = methods.load() # To allow reloading to potentially work, use a non-hacked loader which # won't rely on a now-closed file object. module.__loader__ = machinery.SourceFileLoader(name, pathname) + module.__spec__.loader = module.__loader__ return module @@ -177,11 +183,17 @@ class _LoadCompiledCompatibility(_HackedGetData, SourcelessFileLoader): def load_compiled(name, pathname, file=None): """**DEPRECATED**""" - _LoadCompiledCompatibility(name, pathname, file).load_module(name) - module = sys.modules[name] + loader = _LoadCompiledCompatibility(name, pathname, file) + spec = util.spec_from_file_location(name, pathname, loader=loader) + methods = _SpecMethods(spec) + if name in sys.modules: + module = methods.exec(sys.modules[name]) + else: + module = methods.load() # To allow reloading to potentially work, use a non-hacked loader which # won't rely on a now-closed file object. module.__loader__ = SourcelessFileLoader(name, pathname) + module.__spec__.loader = module.__loader__ return module @@ -196,7 +208,13 @@ def load_package(name, path): break else: raise ValueError('{!r} is not a package'.format(path)) - return machinery.SourceFileLoader(name, path).load_module(name) + spec = util.spec_from_file_location(name, path, + submodule_search_locations=[]) + methods = _SpecMethods(spec) + if name in sys.modules: + return methods.exec(sys.modules[name]) + else: + return methods.load() def load_module(name, file, filename, details): |