Skip to content

Commit 5f2d87e

Browse files
Add a test (flawed).
1 parent 0a6335e commit 5f2d87e

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

Lib/importlib/_bootstrap_external.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ def _validate_timestamp_pyc(self, data, source_mtime, source_size, name,
757757
raise ImportError(f'bytecode is stale for {name!r}', **exc_details)
758758
if time.time() - source_mtime < 2:
759759
try:
760-
bytecode_mtime = self.path_mtime(bytecode_path)
760+
bytecode_mtime = self.path_stats(bytecode_path)['mtime']
761761
except OSError:
762762
pass
763763
else:

Lib/test/test_import/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,31 @@ def test_recompute_pyc_same_second(self):
15181518
m = __import__(TESTFN)
15191519
self.assertEqual(m.x, 5)
15201520

1521+
def test_recompute_pyc_same_second_same_size(self):
1522+
# Even when the source file doesn't change timestamp (truncated
1523+
# to seconds) and size, the difference between the source and
1524+
# the bytecode timestamps is enough to trigger recomputation of
1525+
# the pyc file.
1526+
prev_mtime = 0
1527+
for i in range(10, 100):
1528+
with self.subTest(i):
1529+
while True:
1530+
with open(self.source, 'w', encoding='utf-8') as fp:
1531+
print(f"x = {i}", file=fp)
1532+
mtime = os.stat(self.source).st_mtime
1533+
if mtime > prev_mtime:
1534+
break
1535+
time.sleep(1e-3)
1536+
self.assertGreater(mtime, prev_mtime)
1537+
prev_mtime = mtime
1538+
1539+
m = __import__(TESTFN)
1540+
self.assertEqual(m.x, i)
1541+
unload(TESTFN)
1542+
m = __import__(TESTFN)
1543+
self.assertEqual(m.x, i)
1544+
unload(TESTFN)
1545+
15211546

15221547
class TestSymbolicallyLinkedPackage(unittest.TestCase):
15231548
package_name = 'sample'

0 commit comments

Comments
 (0)