runner: add support for `sys.last_exc` for post-mortem debugging on Python>=3.12

Fix #11850
This commit is contained in:
robotherapist 2024-02-05 21:09:28 +01:00 committed by Ran Benita
parent b510adf9ef
commit c09f74619b
4 changed files with 11 additions and 0 deletions

View File

@ -128,6 +128,7 @@ Edison Gustavo Muenz
Edoardo Batini
Edson Tadeu M. Manoel
Eduardo Schettino
Edward Haigh
Eero Vaher
Eli Boyarski
Elizaveta Shashkova

View File

@ -0,0 +1 @@
Added support for :data:`sys.last_exc` for post-mortem debugging on Python>=3.12.

View File

@ -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:
@ -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

View File

@ -926,6 +926,9 @@ def test_store_except_info_on_error() -> None:
# 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
@ -934,6 +937,8 @@ def test_store_except_info_on_error() -> None:
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")