Skip to content

Commit

Permalink
runner: add support for sys.last_exc for post-mortem debugging on P…
Browse files Browse the repository at this point in the history
…ython>=3.12

Fix pytest-dev#11850
  • Loading branch information
robotherapist authored and flying-sheep committed Apr 9, 2024
1 parent d96d5ea commit a5a737f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -128,6 +128,7 @@ Edison Gustavo Muenz
Edoardo Batini
Edson Tadeu M. Manoel
Eduardo Schettino
Edward Haigh
Eero Vaher
Eli Boyarski
Elizaveta Shashkova
Expand Down
1 change: 1 addition & 0 deletions changelog/11850.improvement.rst
@@ -0,0 +1 @@
Added support for :data:`sys.last_exc` for post-mortem debugging on Python>=3.12.
4 changes: 4 additions & 0 deletions src/_pytest/runner.py
Expand Up @@ -164,6 +164,8 @@ def pytest_runtest_call(item: Item) -> None:
del sys.last_type
del sys.last_value
del sys.last_traceback
if sys.version_info >= (3, 12, 0):
del sys.last_exc # type: ignore[attr-defined]
except AttributeError:
pass
try:
Expand All @@ -172,6 +174,8 @@ def pytest_runtest_call(item: Item) -> None:
# Store trace info to allow postmortem debugging
sys.last_type = type(e)
sys.last_value = e
if sys.version_info >= (3, 12, 0):
sys.last_exc = e # type: ignore[attr-defined]
assert e.__traceback__ is not None
# Skip *this* frame
sys.last_traceback = e.__traceback__.tb_next
Expand Down
5 changes: 5 additions & 0 deletions testing/test_runner.py
Expand Up @@ -926,6 +926,9 @@ def runtest(self):
# Check that exception info is stored on sys
assert sys.last_type is IndexError
assert isinstance(sys.last_value, IndexError)
if sys.version_info >= (3, 12, 0):
assert isinstance(sys.last_exc, IndexError) # type: ignore[attr-defined]

assert sys.last_value.args[0] == "TEST"
assert sys.last_traceback

Expand All @@ -934,6 +937,8 @@ def runtest(self):
runner.pytest_runtest_call(ItemMightRaise()) # type: ignore[arg-type]
assert not hasattr(sys, "last_type")
assert not hasattr(sys, "last_value")
if sys.version_info >= (3, 12, 0):
assert not hasattr(sys, "last_exc")
assert not hasattr(sys, "last_traceback")


Expand Down

0 comments on commit a5a737f

Please sign in to comment.