From b28c439494123bd74a345d7eb7bf620298487081 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sun, 4 Jul 2010 22:13:12 +0200 Subject: [PATCH] some minor compatibility issues wrt to the just released python2.7 --HG-- branch : trunk --- CHANGELOG | 3 +++ py/_plugin/pytest_recwarn.py | 15 +++++++++++---- testing/code/test_source.py | 2 +- testing/log/test_warning.py | 10 +++++----- testing/plugin/test_pytest_recwarn.py | 2 +- testing/root/test_oldmagic.py | 5 +---- tox.ini | 3 +++ 7 files changed, 25 insertions(+), 15 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 5d9b54f83..9de9d92fe 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -37,6 +37,9 @@ New features Bug fixes / Maintenance ++++++++++++++++++++++++++ +- make tests and the ``pytest_recwarn`` plugin in paricular fully compatible + to Python2.7 (if you use the ``recwarn`` funcarg warnings will be enabled so that + you can properly check for their existence in a cross-python manner). - improve error messages if importing a test module failed (ImportError, import file mismatches, syntax errors) - refine --pdb: ignore xfailed tests, unify its TB-reporting and diff --git a/py/_plugin/pytest_recwarn.py b/py/_plugin/pytest_recwarn.py index 4bfc57cdd..68bf25472 100644 --- a/py/_plugin/pytest_recwarn.py +++ b/py/_plugin/pytest_recwarn.py @@ -33,7 +33,7 @@ warning: """ import py -import os +import sys, os def pytest_funcarg__recwarn(request): """Return a WarningsRecorder instance that provides these methods: @@ -41,9 +41,16 @@ def pytest_funcarg__recwarn(request): * ``pop(category=None)``: return last warning matching the category. * ``clear()``: clear list of warnings """ - warnings = WarningsRecorder() - request.addfinalizer(warnings.finalize) - return warnings + if sys.version_info >= (2,7): + import warnings + oldfilters = warnings.filters[:] + warnings.simplefilter('default') + def reset_filters(): + warnings.filters[:] = oldfilters + request.addfinalizer(reset_filters) + wrec = WarningsRecorder() + request.addfinalizer(wrec.finalize) + return wrec def pytest_namespace(): return {'deprecated_call': deprecated_call} diff --git a/testing/code/test_source.py b/testing/code/test_source.py index e333c2d7b..bdedeeff2 100644 --- a/testing/code/test_source.py +++ b/testing/code/test_source.py @@ -341,7 +341,7 @@ def test_deindent(): lines = deindent(source.splitlines()) assert lines == ['', 'def f():', ' def g():', ' pass', ' '] -@py.test.mark.xfail +@py.test.mark.xfail("sys.version_info[:2] != (2,7)") def test_source_of_class_at_eof_without_newline(tmpdir): # this test fails because the implicit inspect.getsource(A) below # does not return the "x = 1" last line. diff --git a/testing/log/test_warning.py b/testing/log/test_warning.py index c16432d66..9227e843c 100644 --- a/testing/log/test_warning.py +++ b/testing/log/test_warning.py @@ -4,7 +4,7 @@ mypath = py.path.local(__file__).new(ext=".py") def test_forwarding_to_warnings_module(): py.test.deprecated_call(py.log._apiwarn, "1.3", "..") -def test_apiwarn_functional(): +def test_apiwarn_functional(recwarn): capture = py.io.StdCapture() py.log._apiwarn("x.y.z", "something", stacklevel=1) out, err = capture.reset() @@ -15,7 +15,7 @@ def test_apiwarn_functional(): exp = "%s:%s" % (mypath, lno) assert err.find(exp) != -1 -def test_stacklevel(): +def test_stacklevel(recwarn): def f(): py.log._apiwarn("x", "some", stacklevel=2) # 3 @@ -27,7 +27,7 @@ def test_stacklevel(): warning = str(err) assert warning.find(":%s" % lno) != -1 -def test_stacklevel_initpkg_with_resolve(testdir): +def test_stacklevel_initpkg_with_resolve(testdir, recwarn): testdir.makepyfile(modabc=""" import py def f(): @@ -49,7 +49,7 @@ def test_stacklevel_initpkg_with_resolve(testdir): loc = 'test_stacklevel_initpkg_with_resolve.py:2' assert warning.find(loc) != -1 -def test_stacklevel_initpkg_no_resolve(): +def test_stacklevel_initpkg_no_resolve(recwarn): def f(): py.log._apiwarn("x", "some", stacklevel="apipkg") capture = py.io.StdCapture() @@ -60,7 +60,7 @@ def test_stacklevel_initpkg_no_resolve(): assert warning.find(":%s" % lno) != -1 -def test_function(): +def test_function(recwarn): capture = py.io.StdCapture() py.log._apiwarn("x.y.z", "something", function=test_function) out, err = capture.reset() diff --git a/testing/plugin/test_pytest_recwarn.py b/testing/plugin/test_pytest_recwarn.py index 6c004e9f0..c35b04633 100644 --- a/testing/plugin/test_pytest_recwarn.py +++ b/testing/plugin/test_pytest_recwarn.py @@ -1,7 +1,7 @@ import py from py._plugin.pytest_recwarn import WarningsRecorder -def test_WarningRecorder(): +def test_WarningRecorder(recwarn): showwarning = py.std.warnings.showwarning rec = WarningsRecorder() assert py.std.warnings.showwarning != showwarning diff --git a/testing/root/test_oldmagic.py b/testing/root/test_oldmagic.py index 92accc699..f5558d671 100644 --- a/testing/root/test_oldmagic.py +++ b/testing/root/test_oldmagic.py @@ -23,10 +23,7 @@ def test_invoke_compile(recwarn, monkeypatch): monkeypatch.setattr(py.builtin.builtins, 'compile', None) py.magic.invoke(compile=True) try: - co = compile("""if 1: - def f(): - return 1 - \n""", '', 'exec') + co = compile("def f(): return 1\n", '', 'exec') d = {} py.builtin.exec_(co, d) assert py.code.Source(d['f']) diff --git a/tox.ini b/tox.ini index e632e99b5..2b0234b65 100644 --- a/tox.ini +++ b/tox.ini @@ -3,6 +3,9 @@ changedir=testing commands= py.test --confcutdir=.. -rfsxX --junitxml=junit-{envname}.xml --tools-on-path [] deps=pexpect +[testenv:py27] +distribute=True +basepython=python2.7 [testenv:py26] basepython=python2.6 [testenv:doc]