Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,8 @@ def getmodule(object, _filename=None):
return sys.modules.get(modulesbyfile[_filename])
# Try the cache again with the absolute file name
try:
if _filename is None:
_filename = getfile(object)
file = getabsfile(object, _filename)
except (TypeError, FileNotFoundError):
return None
Expand Down
30 changes: 30 additions & 0 deletions Lib/test/test_zipimport_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# The tests are centralised in this fashion to make it easy to drop them
# if a platform doesn't support zipimport
import test.support
import importlib
import os
import os.path
import sys
Expand Down Expand Up @@ -96,6 +97,35 @@ def test_inspect_getsource_issue4223(self):
finally:
del sys.modules["zip_pkg"]

def test_inspect_fresh_namespace_uses_module_loader(self):
# gh-92041: a frame exec'd in a plain namespace resolves to the
# zipimported module owning its filename, so getsource() can use
# the module's loader.
test_src = textwrap.dedent("""\
import inspect

def capture():
return inspect.currentframe()

frame = capture()
""")
with os_helper.temp_dir() as d:
script_name = make_script(d, "zipped_mod", test_src)
zip_name, _ = make_zip_script(d, "test_zip", script_name)
os.remove(script_name)
sys.path.insert(0, zip_name)
module = importlib.import_module("zipped_mod")
try:
namespace = {}
exec(compile(test_src, module.__file__, "exec"), namespace)
frame = namespace["frame"]
self.assertIs(inspect.getmodule(frame), module)
self.assertEqual(inspect.getsource(frame),
"def capture():\n"
" return inspect.currentframe()\n")
finally:
del sys.modules["zipped_mod"]

def test_doctest_issue4197(self):
# To avoid having to keep two copies of the doctest module's
# unit tests in sync, this test works by taking the source of
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speed up :func:`inspect.getmodule` for objects resolved by filename, such
as frames, tracebacks and code objects.
Loading