Skip to content

Commit 64a6365

Browse files
authored
Include pytest version in the cached pyc tags (#5484)
Include pytest version in the cached pyc tags
2 parents a54e2e1 + f43fb13 commit 64a6365

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

changelog/1671.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The name of the ``.pyc`` files cached by the assertion writer now includes the pytest version
2+
to avoid stale caches.

src/_pytest/assertion/rewrite.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import atomicwrites
1414

1515
from _pytest._io.saferepr import saferepr
16+
from _pytest._version import version
1617
from _pytest.assertion import util
1718
from _pytest.assertion.util import ( # noqa: F401
1819
format_explanation as _format_explanation,
@@ -21,7 +22,7 @@
2122
from _pytest.pathlib import PurePath
2223

2324
# pytest caches rewritten pycs in __pycache__.
24-
PYTEST_TAG = "{}-PYTEST".format(sys.implementation.cache_tag)
25+
PYTEST_TAG = "{}-pytest-{}".format(sys.implementation.cache_tag, version)
2526
PYC_EXT = ".py" + (__debug__ and "c" or "o")
2627
PYC_TAIL = "." + PYTEST_TAG + PYC_EXT
2728

testing/test_assertrewrite.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,24 @@ def test_it():
780780

781781
assert testdir.runpytest().ret == 0
782782

783+
def test_cached_pyc_includes_pytest_version(self, testdir, monkeypatch):
784+
"""Avoid stale caches (#1671)"""
785+
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
786+
testdir.makepyfile(
787+
test_foo="""
788+
def test_foo():
789+
assert True
790+
"""
791+
)
792+
result = testdir.runpytest_subprocess()
793+
assert result.ret == 0
794+
found_names = glob.glob(
795+
"__pycache__/*-pytest-{}.pyc".format(pytest.__version__)
796+
)
797+
assert found_names, "pyc with expected tag not found in names: {}".format(
798+
glob.glob("__pycache__/*.pyc")
799+
)
800+
783801
@pytest.mark.skipif('"__pypy__" in sys.modules')
784802
def test_pyc_vs_pyo(self, testdir, monkeypatch):
785803
testdir.makepyfile(

0 commit comments

Comments
 (0)