Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Set __file__ on frozen modules if possible.
  • Loading branch information
ericsnowcurrently committed Oct 5, 2021
commit 5c538f5da7e97ead989d55490753f42f0b887634
20 changes: 19 additions & 1 deletion Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,13 +850,19 @@ def find_spec(cls, fullname, path=None,>
if info is None:
return None
data, ispkg, origname = info
filename = pkgdir = None
spec = spec_from_loader(fullname, cls,
origin=cls._ORIGIN,
is_package=ispkg)
spec.loader_state = type(sys.implementation)(
data=data,
filename=filename,
origname=origname,
)
if ispkg and filename:
assert filename.endswith('__init__.py'), filename
assert pkgdir, pkgdir
spec.submodule_search_locations.insert(0, pkgdir)
return spec

@classmethod
Expand All @@ -873,7 +879,16 @@ def find_module(cls, fullname, path=None):

@staticmethod
def create_module(spec):
"""Use default semantics for module creation."""
"""Set __file__, if able."""
module = _new_module(spec.name)
try:
filename = spec.loader_state.filename
except AttributeError:
pass
else:
if filename:
module.__file__ = filename
return module

@staticmethod
def exec_module(module):
Expand Down Expand Up @@ -905,6 +920,9 @@ def load_module(cls, fullname):
# Warning about deprecation implemented in _load_module_shim().
return _load_module_shim(cls, fullname)

# XXX We should also add get_filename(), but when we do we need
# to fix spec_from_loader().

@classmethod
@_requires_frozen
def get_code(cls, fullname):
Expand Down