From bf5b2264747fdccd56c124147f1971125bf82566 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 15 Sep 2012 15:20:49 +0200 Subject: [PATCH] fix issue 188 - ensure sys.exc_info on py2 is clear before calling into a test --- CHANGELOG | 3 +++ _pytest/main.py | 13 ++++++++++--- testing/test_runner.py | 7 ++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 9034cbfc3..1edca48e7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -56,6 +56,9 @@ Changes between 2.2.4 and 2.3.0.dev - fix issue 182: testdir.inprocess_run now considers passed plugins +- fix issue 188: ensure sys.exc_info is clear on python2 + before calling into a test + - reporting refinements: - pytest_report_header now receives a "startdir" so that diff --git a/_pytest/main.py b/_pytest/main.py index 52bff5bc6..202129d1e 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -114,11 +114,18 @@ def pytest_collection(session): def pytest_runtestloop(session): if session.config.option.collectonly: return True - for i, item in enumerate(session.items): + + def getnextitem(i): + # this is a function to avoid python2 + # keeping sys.exc_info set when calling into a test + # python2 keeps sys.exc_info till the frame is left try: - nextitem = session.items[i+1] + return session.items[i+1] except IndexError: - nextitem = None + return None + + for i, item in enumerate(session.items): + nextitem = getnextitem(i) item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem) if session.shouldstop: raise session.Interrupted(session.shouldstop) diff --git a/testing/test_runner.py b/testing/test_runner.py index e02434b8d..54009f066 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -180,7 +180,12 @@ class BaseFunctionalTests: raise Exception() def test_func(): - pass + import sys + # on python2 exc_info is keept till a function exits + # so we would end up calling test functions while + # sys.exc_info would return the indexerror + # from guessing the lastitem + assert sys.exc_info()[0] is None def teardown_function(func): raise ValueError(42) """)