summaryrefslogtreecommitdiffstats
path: root/Lib/imp.py
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2013-11-22 16:05:39 (GMT)
committerEric Snow <ericsnowcurrently@gmail.com>2013-11-22 16:05:39 (GMT)
commitb523f8433a8982e10eb41a3e2b37ee0e6d6a6e00 (patch)
treeb38661db4903b7edc4042e7562b32720dd3687bf /Lib/imp.py
parent9e6097ebe7bb99a4a22b949ef4b1563b21ad7166 (diff)
downloadcpython-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.py30
1 files changed, 24 insertions, 6 deletions
diff --git a/Lib/imp.py b/Lib/imp.py
index ee6cfe2..1144cd1 100644
--- a/Lib/imp.py
+++ b/Lib/imp.py
@@ -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):