Include pytest version in the cached pyc tags

Fix #1671
This commit is contained in:
Bruno Oliveira 2019-06-24 20:37:07 -03:00
parent a24933b0a6
commit f43fb13179
3 changed files with 22 additions and 1 deletions

View File

@ -0,0 +1,2 @@
The name of the ``.pyc`` files cached by the assertion writer now includes the pytest version
to avoid stale caches.

View File

@ -13,6 +13,7 @@ import types
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,
@ -21,7 +22,7 @@ from _pytest.pathlib import fnmatch_ex
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

View File

@ -780,6 +780,24 @@ def test_rewritten():
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(