Skip to content

Commit a2a9aa6

Browse files
authored
Merge pull request #12027 from bluetech/sys-last-exc
runner: add support for `sys.last_exc` for post-mortem debugging on Python>=3.12
2 parents b510adf + c09f746 commit a2a9aa6

File tree

4 files changed

+11
-0
lines changed

4 files changed

+11
-0
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Edison Gustavo Muenz
128128
Edoardo Batini
129129
Edson Tadeu M. Manoel
130130
Eduardo Schettino
131+
Edward Haigh
131132
Eero Vaher
132133
Eli Boyarski
133134
Elizaveta Shashkova

changelog/11850.improvement.rst

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

src/_pytest/runner.py

+4
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ def pytest_runtest_call(item: Item) -> None:
164164
del sys.last_type
165165
del sys.last_value
166166
del sys.last_traceback
167+
if sys.version_info >= (3, 12, 0):
168+
del sys.last_exc # type: ignore[attr-defined]
167169
except AttributeError:
168170
pass
169171
try:
@@ -172,6 +174,8 @@ def pytest_runtest_call(item: Item) -> None:
172174
# Store trace info to allow postmortem debugging
173175
sys.last_type = type(e)
174176
sys.last_value = e
177+
if sys.version_info >= (3, 12, 0):
178+
sys.last_exc = e # type: ignore[attr-defined]
175179
assert e.__traceback__ is not None
176180
# Skip *this* frame
177181
sys.last_traceback = e.__traceback__.tb_next

testing/test_runner.py

+5
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,9 @@ def runtest(self):
926926
# Check that exception info is stored on sys
927927
assert sys.last_type is IndexError
928928
assert isinstance(sys.last_value, IndexError)
929+
if sys.version_info >= (3, 12, 0):
930+
assert isinstance(sys.last_exc, IndexError) # type: ignore[attr-defined]
931+
929932
assert sys.last_value.args[0] == "TEST"
930933
assert sys.last_traceback
931934

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

939944

0 commit comments

Comments
 (0)