You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cpython uses old cached bytecode when you change the source to fast #136224
I found a problem when I change python source code real fast and execute it again. It looks like cpython uses the old cached bytecode when the timestamp of the bytecode and the timestamp of the new source are the same (it looks like the resolution is 1 second because it uses st_mtime).
The following code demonstrates the problem:
fromsubprocessimportrunimportsysimporttimewithopen("import_print_number.py", "w") asf:
f.write(
"""\from print_number import print_number print_number()"""
)
foriinrange(20):
print("test", i)
withopen("print_number.py", "w") asf:
f.write(f"def print_number(): print({i})")
# low numbers make the bug more likelytime.sleep(0.6)
# run the coderesult=run([sys.executable, "import_print_number.py"], capture_output=True)
n=int(result.stdout.decode().strip())
# numbers should be equal but are not in some cases,# because python uses some old cached bytecodeasserti==n, (i, n)
My current work around is that i delete __pycache__ every time I change code but It would be nice if this is not neccessary. It is also really hard to figure out what is going on when it happens because the problem is often not deterministic.
Activity
15r10nk commentedon Jul 3, 2025
sorry this was already reported #121376 I will close this issue.
.pycfile invalidation #121376