Skip to content

Commit f1ac0ee

Browse files
authored
Remove sys.last_traceback attribute using del instead of settin… (#6256)
Remove sys.last_traceback attribute using del instead of setting to None
2 parents 490c7c7 + 82424c9 commit f1ac0ee

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ Marcelo Duarte Trevisani
165165
Marcin Bachry
166166
Marco Gorelli
167167
Mark Abramowitz
168+
Mark Dickinson
168169
Markus Unterwaditzer
169170
Martijn Faassen
170171
Martin Altmayer

changelog/6255.bugfix.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Clear the ``sys.last_traceback``, ``sys.last_type`` and ``sys.last_value``
2+
attributes by deleting them instead of setting them to ``None``. This better
3+
matches the behaviour of the Python standard library.

src/_pytest/runner.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ def pytest_runtest_setup(item):
121121

122122
def pytest_runtest_call(item):
123123
_update_current_test_var(item, "call")
124-
sys.last_type, sys.last_value, sys.last_traceback = (None, None, None)
124+
try:
125+
del sys.last_type
126+
del sys.last_value
127+
del sys.last_traceback
128+
except AttributeError:
129+
pass
125130
try:
126131
item.runtest()
127132
except Exception:

testing/test_runner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -900,9 +900,9 @@ def runtest(self):
900900
# The next run should clear the exception info stored by the previous run
901901
ItemMightRaise.raise_error = False
902902
runner.pytest_runtest_call(ItemMightRaise())
903-
assert sys.last_type is None
904-
assert sys.last_value is None
905-
assert sys.last_traceback is None
903+
assert not hasattr(sys, "last_type")
904+
assert not hasattr(sys, "last_value")
905+
assert not hasattr(sys, "last_traceback")
906906

907907

908908
def test_current_test_env_var(testdir, monkeypatch):

0 commit comments

Comments
 (0)