Merge pull request #12027 from bluetech/sys-last-exc

runner: add support for `sys.last_exc` for post-mortem debugging on Python>=3.12
This commit is contained in:
Ran Benita 2024-02-23 22:41:14 +02:00 committed by GitHub
commit a2a9aa6cde
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 11 additions and 0 deletions

View File

@ -128,6 +128,7 @@ Edison Gustavo Muenz
Edoardo Batini Edoardo Batini
Edson Tadeu M. Manoel Edson Tadeu M. Manoel
Eduardo Schettino Eduardo Schettino
Edward Haigh
Eero Vaher Eero Vaher
Eli Boyarski Eli Boyarski
Elizaveta Shashkova 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_type
del sys.last_value del sys.last_value
del sys.last_traceback del sys.last_traceback
if sys.version_info >= (3, 12, 0):
del sys.last_exc # type: ignore[attr-defined]
except AttributeError: except AttributeError:
pass pass
try: try:
@ -172,6 +174,8 @@ def pytest_runtest_call(item: Item) -> None:
# Store trace info to allow postmortem debugging # Store trace info to allow postmortem debugging
sys.last_type = type(e) sys.last_type = type(e)
sys.last_value = 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 assert e.__traceback__ is not None
# Skip *this* frame # Skip *this* frame
sys.last_traceback = e.__traceback__.tb_next 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 # Check that exception info is stored on sys
assert sys.last_type is IndexError assert sys.last_type is IndexError
assert isinstance(sys.last_value, 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_value.args[0] == "TEST"
assert sys.last_traceback 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] runner.pytest_runtest_call(ItemMightRaise()) # type: ignore[arg-type]
assert not hasattr(sys, "last_type") assert not hasattr(sys, "last_type")
assert not hasattr(sys, "last_value") 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") assert not hasattr(sys, "last_traceback")