mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Issue #20383: Introduce importlib.util.module_from_spec().
Along the way, dismantle importlib._bootstrap._SpecMethods as it was no longer relevant and constructing the new function required partially dismantling the class anyway.
This commit is contained in:
parent
c8f0d6ebfc
commit
2a17bde930
17 changed files with 4536 additions and 4736 deletions
17
Lib/imp.py
17
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, _SpecMethods
|
||||
from importlib._bootstrap import SourcelessFileLoader, _ERR_MSG, _exec, _load
|
||||
|
||||
from importlib import machinery
|
||||
from importlib import util
|
||||
|
@ -164,11 +164,10 @@ class _LoadSourceCompatibility(_HackedGetData, machinery.SourceFileLoader):
|
|||
def load_source(name, pathname, file=None):
|
||||
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])
|
||||
module = _exec(spec, sys.modules[name])
|
||||
else:
|
||||
module = methods.load()
|
||||
module = _load(spec)
|
||||
# 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)
|
||||
|
@ -185,11 +184,10 @@ def load_compiled(name, pathname, file=None):
|
|||
"""**DEPRECATED**"""
|
||||
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])
|
||||
module = _exec(spec, sys.modules[name])
|
||||
else:
|
||||
module = methods.load()
|
||||
module = _load(spec)
|
||||
# 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)
|
||||
|
@ -210,11 +208,10 @@ def load_package(name, path):
|
|||
raise ValueError('{!r} is not a package'.format(path))
|
||||
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])
|
||||
return _exec(spec, sys.modules[name])
|
||||
else:
|
||||
return methods.load()
|
||||
return _load(spec)
|
||||
|
||||
|
||||
def load_module(name, file, filename, details):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue