Skip to content

Commit bda170b

Browse files
committed
Avoid shadowed module filename in dynamic metadata fixture
The fixture left a stale __file__ value beneath a module subclass property. LOAD_ATTR_MODULE specialization can read that value, making the assertion depend on warmup state in the existing filename fallback, including main. Remove the shadow value and exercise warmed getfile access, two changing origins, and independent cold frame/traceback lookups. Keep production code unchanged. Verified the corrected test 50 times on each of main and the candidate; all six focused suites pass (1,135 tests reported, four skipped).
1 parent 6c72434 commit bda170b

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

‎Lib/test/test_inspect/test_inspect.py‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,11 +1155,22 @@ class DynamicModule(types.ModuleType):
11551155
@property
11561156
def __file__(self):
11571157
return self.source
1158+
# Do not leave a shadow value that LOAD_ATTR_MODULE can read instead
1159+
# of the property when attribute access specializes.
1160+
del self.module.__dict__['__file__']
11581161
self.module.__class__ = DynamicModule
1159-
self.module.source = self.filename + '.dynamic'
1160-
frame, traceback = self.capture(filename=self.module.source)
1161-
for object in (frame, traceback):
1162-
self.assertIs(inspect.getmodule(object), self.module)
1162+
for suffix in ('.dynamic', '.changed'):
1163+
with self.subTest(suffix=suffix):
1164+
self.module.source = self.filename + suffix
1165+
frame, traceback = self.capture(filename=self.module.source)
1166+
# Exercise warmed attribute access as well as cold caches.
1167+
for _ in range(100):
1168+
self.assertEqual(inspect.getfile(self.module),
1169+
self.module.source)
1170+
for object in (frame, traceback):
1171+
inspect.modulesbyfile.clear()
1172+
inspect._filesbymodname.clear()
1173+
self.assertIs(inspect.getmodule(object), self.module)
11631174

11641175
def test_sourceless_import(self):
11651176
import py_compile

0 commit comments

Comments
 (0)