◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
177 changes: 136 additions & 41 deletions Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,10 @@ def has_location(self, value):

def spec_from_loader(name, loader, *, origin=None, is_package=None):
"""Return a module spec based on various loader methods."""
if hasattr(loader, 'get_filename'):
if _bootstrap_external is None:
raise NotImplementedError
spec_from_file_location = _bootstrap_external.spec_from_file_location
Expand Down Expand Up @@ -467,12 +470,9 @@ def _spec_from_module(module, loader=None, origin=None):
except AttributeError:
location = None
if origin is None:
if location is None:
try:
origin = loader._ORIGIN
except AttributeError:
origin = None
else:
origin = location
try:
cached = module.__cached__
@@ -484,7 +484,7 @@ def _spec_from_module(module, loader=None, origin=None):
submodule_search_locations = None

spec = ModuleSpec(name, loader, origin=origin)
spec._set_fileattr = False if location is None else True
spec.cached = cached
spec.submodule_search_locations = submodule_search_locations
return spec
Expand Up @@ -541,6 +541,7 @@ def _init_module_attrs(spec, module, *, override=False):
# __path__
if override or getattr(module, '__path__', None) is None:
if spec.submodule_search_locations is not None:
try:
module.__path__ = spec.submodule_search_locations
except AttributeError:
Expand Down Expand Up @@ -825,38 +826,127 @@ def module_repr(m):
return '<module {!r} ({})>'.format(m.__name__, FrozenImporter._ORIGIN)

@classmethod
def _setup_module(cls, module):
assert not hasattr(module, '__file__'), module.__file__
ispkg = hasattr(module, '__path__')
assert not ispkg or not module.__path__, module.__path__
spec = module.__spec__
assert not ispkg or not spec.submodule_search_locations

if spec.loader_state is None:
spec.loader_state = type(sys.implementation)(
data=None,
origname=None,
)
elif not hasattr(spec.loader_state, 'data'):
spec.loader_state.data = None
if not getattr(spec.loader_state, 'origname', None):
origname = vars(module).pop('__origname__', None)
assert origname, 'see PyImport_ImportFrozenModuleObject()'
spec.loader_state.origname = origname

@classmethod
def find_spec(cls, fullname, path=None, target=None):
info = _call_with_frames_removed(_imp.find_frozen, fullname)
if info is None:
return None
data, ispkg, origname = info
spec = spec_from_loader(fullname, cls,
origin=cls._ORIGIN,
is_package=ispkg)
spec.loader_state = type(sys.implementation)(
data=data,
origname=origname,
)
return spec

@classmethod
Expand All @@ -873,26 +963,22 @@ def find_module(cls, fullname, path=None):

@staticmethod
def create_module(spec):
"""Use default semantics for module creation."""

@staticmethod
def exec_module(module):
spec = module.__spec__
name = spec.name
try:
data = spec.loader_state.data
except AttributeError:
if not _imp.is_frozen(name):
raise ImportError('{!r} is not a frozen module'.format(name),
name=name)
data = None
else:
# We clear the extra data we got from the finder, to save memory.
# Note that if this method is called again (e.g. by
# importlib.reload()) then _imp.get_frozen_object() will notice
# no data was provided and will look it up.
spec.loader_state.data = None
code = _call_with_frames_removed(_imp.get_frozen_object, name, data)
exec(code, module.__dict__)

@classmethod
Expand All @@ -903,7 +989,16 @@ def load_module(cls, fullname):

"""
# Warning about deprecation implemented in _load_module_shim().
return _load_module_shim(cls, fullname)

@classmethod
@_requires_frozen
Expand Down Expand Up @@ -1244,7 +1339,7 @@ def _setup(sys_module, _imp_module):
spec = _spec_from_module(module, loader)
_init_module_attrs(spec, module)
if loader is FrozenImporter:
loader._setup_module(module)

# Directly load built-in modules needed during bootstrap.
self_module = sys.modules[__name__]
Expand Down
3 changes: 0 additions & 3 deletions Lib/test/test_frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ def test_frozen_submodule_in_unfrozen_package(self):
self.assertIs(spam.__spec__.loader,
importlib.machinery.FrozenImporter)

# This is not possible until frozen packages have __path__ set properly.
# See https://bugs.python.org/issue21736.
@unittest.expectedFailure
def test_unfrozen_submodule_in_frozen_package(self):
with import_helper.CleanImport('__phello__', '__phello__.spam'):
with import_helper.frozen_modules(enabled=True):
Expand Down
29 changes: 15 additions & 14 deletions Lib/test/test_importlib/frozen/test_finder.py
Original file line number Diff line number Diff line change
@@ -44,30 +44,31 @@ def check_loader_state(self, spec, origname=None, filename=None):
if not filename:
if not origname:
origname = spec.name

actual = dict(vars(spec.loader_state))

# Check the code object used to import the frozen module.
# We can't compare the marshaled data directly because
# marshal.dumps() would mark "expected" (below) as a ref,
# which slightly changes the output.
# (See https://bugs.python.org/issue34093.)
data = actual.pop('data')
with import_helper.frozen_modules():
expected = _imp.get_frozen_object(spec.name)
code = marshal.loads(data)
self.assertEqual(code, expected)

# Check the rest of spec.loader_state.
expected = dict(
origname=origname,
)
self.assertDictEqual(actual, expected)

def check_search_locations(self, spec):
# Frozen packages do not have any path entries.
# (See https://bugs.python.org/issue21736.)
expected = []
self.assertListEqual(spec.submodule_search_locations, expected)

def test_module(self):
Expand Down
Loading
Toggle all file notes Toggle all file annotations