From abe080c6b4b82486791de97b21262c274d22ff03 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Mon, 29 Aug 2011 10:13:00 -0400 Subject: [PATCH] use different caches for optimized and unoptimized code (fixes #66) --- CHANGELOG | 2 ++ _pytest/assertion/rewrite.py | 5 ++++- testing/test_assertrewrite.py | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 6709d04f9..8aab63ce4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,8 @@ Changes between 2.1.1 and [NEXT VERSION] ---------------------------------------- +- fix issue66: use different assertion rewriting caches when the -O option is passed + Changes between 2.1.0 and 2.1.1 ---------------------------------------------- diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py index 32d34acf7..8d850433b 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -35,6 +35,9 @@ else: PYTEST_TAG = "%s-%s%s-PYTEST" % (impl, ver[0], ver[1]) del ver, impl +PYC_EXT = ".py" + "c" if __debug__ else "o" +PYC_TAIL = "." + PYTEST_TAG + PYC_EXT + class AssertionRewritingHook(object): """Import hook which rewrites asserts.""" @@ -121,7 +124,7 @@ class AssertionRewritingHook(object): write = False else: raise - cache_name = fn_pypath.basename[:-3] + "." + PYTEST_TAG + ".pyc" + cache_name = fn_pypath.basename[:-3] + PYC_TAIL pyc = os.path.join(cache_dir, cache_name) # Notice that even if we're in a read-only directory, I'm going to check # for a cached pyc. This may not be optimal... diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 3731f3aed..8c322c09e 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -326,3 +326,17 @@ def test_no_bytecode(): assert not os.path.exists(os.path.dirname(__cached__))""") monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1") assert testdir.runpytest().ret == 0 + + def test_pyc_vs_pyo(self, testdir, monkeypatch): + testdir.makepyfile(""" +import pytest +def test_optimized(): + "hello" + assert test_optimized.__doc__ is None""") + p = py.path.local.make_numbered_dir(prefix="runpytest-", keep=None, + rootdir=testdir.tmpdir) + tmp = "--basetemp=%s" % p + monkeypatch.setenv("PYTHONOPTIMIZE", "2") + assert testdir.runpybin("py.test", tmp).ret == 0 + monkeypatch.undo() + assert testdir.runpybin("py.test", tmp).ret == 1