Skip to content

Commit 7e1cb8a

Browse files
Make test more reliable.
1 parent 5f2d87e commit 7e1cb8a

2 files changed

Lines changed: 22 additions & 19 deletions

File tree

Lib/importlib/_bootstrap_external.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ def _validate_timestamp_pyc(self, data, source_mtime, source_size, name,
762762
pass
763763
else:
764764
if bytecode_mtime < source_mtime:
765-
message = f'bytecode may be stale for {name!r}'
765+
message = f'bytecode is stale for {name!r}'
766766
_bootstrap._verbose_message('{}', message)
767767
raise ImportError(message, **exc_details)
768768

Lib/test/test_import/__init__.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,25 +1523,28 @@ def test_recompute_pyc_same_second_same_size(self):
15231523
# to seconds) and size, the difference between the source and
15241524
# the bytecode timestamps is enough to trigger recomputation of
15251525
# the pyc file.
1526-
prev_mtime = 0
1526+
pyc_file = importlib.util.cache_from_source(self.source)
15271527
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)
1528+
try:
1529+
bytecode_mtime = os.stat(pyc_file).st_mtime
1530+
except FileNotFoundError:
1531+
# The pyc file has not yet be created.
1532+
bytecode_mtime = 0
1533+
delay = 1e-3
1534+
for j in range(10):
1535+
time.sleep(delay)
1536+
with open(self.source, 'w', encoding='utf-8') as fp:
1537+
print(f"x = {i}", file=fp)
1538+
if os.stat(self.source).st_mtime > bytecode_mtime:
1539+
break
1540+
delay *= 2
1541+
1542+
m = __import__(TESTFN)
1543+
self.assertEqual(m.x, i)
1544+
unload(TESTFN)
1545+
m = __import__(TESTFN)
1546+
self.assertEqual(m.x, i)
1547+
unload(TESTFN)
15451548

15461549

15471550
class TestSymbolicallyLinkedPackage(unittest.TestCase):

0 commit comments

Comments
 (0)