Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #11850 - Add sys.last_exc #11934

Closed
wants to merge 10 commits into from
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Edison Gustavo Muenz
Edoardo Batini
Edson Tadeu M. Manoel
Eduardo Schettino
Edward Haigh
Eli Boyarski
Elizaveta Shashkova
Éloi Rivard
Expand Down
1 change: 1 addition & 0 deletions changelog/11850.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added the new `sys.last_exc` value to `pytest_runtest_call()` in `src/_pytest/runner.py`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the changelog is intended for end-users to read, they don't really need to know about details like pytest_runtest_call or the internal file it's done in. I suggest something like this:

Suggested change
Added the new `sys.last_exc` value to `pytest_runtest_call()` in `src/_pytest/runner.py`.
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
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@
del sys.last_type
del sys.last_value
del sys.last_traceback
if sys.version_info >= (3, 12, 0):
del sys.last_exc

Check warning on line 172 in src/_pytest/runner.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/runner.py#L172

Added line #L172 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns it the type stub for it is just missing from typeshed (the project which provides type annotations for the standard library). I sent a fix for it, but until it's integrated (in a future mypy version), let's add type-ignores for it, like this:

Suggested change
del sys.last_exc
del sys.last_exc # type: ignore[attr-defined]

except AttributeError:
pass
try:
Expand All @@ -176,6 +178,8 @@
# 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

Check warning on line 182 in src/_pytest/runner.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/runner.py#L182

Added line #L182 was not covered by tests
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
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,9 @@
# 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)

Check warning on line 930 in testing/test_runner.py

View check run for this annotation

Codecov / codecov/patch

testing/test_runner.py#L930

Added line #L930 was not covered by tests

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

Expand All @@ -934,6 +937,8 @@
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")

Check warning on line 941 in testing/test_runner.py

View check run for this annotation

Codecov / codecov/patch

testing/test_runner.py#L941

Added line #L941 was not covered by tests
assert not hasattr(sys, "last_traceback")


Expand Down