Skip to content

Commit

Permalink
Include pytest version in the cached pyc tags
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Jun 24, 2019
1 parent a24933b commit f43fb13
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelog/1671.bugfix.rst
@@ -0,0 +1,2 @@
The name of the ``.pyc`` files cached by the assertion writer now includes the pytest version
to avoid stale caches.
3 changes: 2 additions & 1 deletion src/_pytest/assertion/rewrite.py
Expand Up @@ -13,6 +13,7 @@
import atomicwrites

from _pytest._io.saferepr import saferepr
from _pytest._version import version
from _pytest.assertion import util
from _pytest.assertion.util import ( # noqa: F401
format_explanation as _format_explanation,
Expand All @@ -21,7 +22,7 @@
from _pytest.pathlib import PurePath

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

Expand Down
18 changes: 18 additions & 0 deletions testing/test_assertrewrite.py
Expand Up @@ -780,6 +780,24 @@ def test_it():

assert testdir.runpytest().ret == 0

def test_cached_pyc_includes_pytest_version(self, testdir, monkeypatch):
"""Avoid stale caches (#1671)"""
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
testdir.makepyfile(
test_foo="""
def test_foo():
assert True
"""
)
result = testdir.runpytest_subprocess()
assert result.ret == 0
found_names = glob.glob(
"__pycache__/*-pytest-{}.pyc".format(pytest.__version__)
)
assert found_names, "pyc with expected tag not found in names: {}".format(
glob.glob("__pycache__/*.pyc")
)

@pytest.mark.skipif('"__pypy__" in sys.modules')
def test_pyc_vs_pyo(self, testdir, monkeypatch):
testdir.makepyfile(
Expand Down

0 comments on commit f43fb13

Please sign in to comment.