Skip to content

Commit 03df224

Browse files
eendebakptmdeckclaude
committed
gh-92041: Test getmodule of a frame exec'd in a fresh namespace
A frame whose filename belongs to a zipimported module must resolve through the filename fallback so getsource() can use the module's loader. Test adapted from PR #92042. Co-authored-by: Mike Decker <mrd999@gmail.com> Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent da559e7 commit 03df224

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lib/test/test_zipimport_support.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# The tests are centralised in this fashion to make it easy to drop them
44
# if a platform doesn't support zipimport
55
import test.support
6+
import importlib
67
import os
78
import os.path
89
import sys
@@ -96,6 +97,35 @@ def test_inspect_getsource_issue4223(self):
9697
finally:
9798
del sys.modules["zip_pkg"]
9899

100+
def test_inspect_fresh_namespace_uses_module_loader(self):
101+
# gh-92041: a frame exec'd in a plain namespace resolves to the
102+
# zipimported module owning its filename, so getsource() can use
103+
# the module's loader.
104+
test_src = textwrap.dedent("""\
105+
import inspect
106+
107+
def capture():
108+
return inspect.currentframe()
109+
110+
frame = capture()
111+
""")
112+
with os_helper.temp_dir() as d:
113+
script_name = make_script(d, "zipped_mod", test_src)
114+
zip_name, _ = make_zip_script(d, "test_zip", script_name)
115+
os.remove(script_name)
116+
sys.path.insert(0, zip_name)
117+
module = importlib.import_module("zipped_mod")
118+
try:
119+
namespace = {}
120+
exec(compile(test_src, module.__file__, "exec"), namespace)
121+
frame = namespace["frame"]
122+
self.assertIs(inspect.getmodule(frame), module)
123+
self.assertEqual(inspect.getsource(frame),
124+
"def capture():\n"
125+
" return inspect.currentframe()\n")
126+
finally:
127+
del sys.modules["zipped_mod"]
128+
99129
def test_doctest_issue4197(self):
100130
# To avoid having to keep two copies of the doctest module's
101131
# unit tests in sync, this test works by taking the source of

0 commit comments

Comments
 (0)