Skip to content

Commit 713f763

Browse files
authored
Merge pull request #2632 from jmoldow/pep_0415_suppress_exception_context
Support PEP-415's Exception.__suppress_context__
2 parents 4cd8727 + 2e61f70 commit 713f763

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ John Towler
8484
Jon Sonesen
8585
Jonas Obrist
8686
Jordan Guymon
87+
Jordan Moldow
8788
Joshua Bronson
8889
Jurko Gospodnetić
8990
Justyna Janczyszyn

_pytest/_code/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ def repr_excinfo(self, excinfo):
679679
e = e.__cause__
680680
excinfo = ExceptionInfo((type(e), e, e.__traceback__)) if e.__traceback__ else None
681681
descr = 'The above exception was the direct cause of the following exception:'
682-
elif e.__context__ is not None:
682+
elif (e.__context__ is not None and not e.__suppress_context__):
683683
e = e.__context__
684684
excinfo = ExceptionInfo((type(e), e, e.__traceback__)) if e.__traceback__ else None
685685
descr = 'During handling of the above exception, another exception occurred:'

changelog/2631.feature

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Added support for `PEP-415's <https://www.python.org/dev/peps/pep-0415/>`_
2+
``Exception.__suppress_context__``. Now if a ``raise exception from None`` is
3+
caught by pytest, pytest will no longer chain the context in the test report.
4+
The behavior now matches Python's traceback behavior.

testing/code/test_excinfo.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,36 @@ def h():
10951095
assert line.endswith('mod.py')
10961096
assert tw.lines[47] == ":15: AttributeError"
10971097

1098+
@pytest.mark.skipif("sys.version_info[0] < 3")
1099+
def test_exc_repr_with_raise_from_none_chain_suppression(self, importasmod):
1100+
mod = importasmod("""
1101+
def f():
1102+
try:
1103+
g()
1104+
except Exception:
1105+
raise AttributeError() from None
1106+
def g():
1107+
raise ValueError()
1108+
""")
1109+
excinfo = pytest.raises(AttributeError, mod.f)
1110+
r = excinfo.getrepr(style="long")
1111+
tw = TWMock()
1112+
r.toterminal(tw)
1113+
for line in tw.lines:
1114+
print(line)
1115+
assert tw.lines[0] == ""
1116+
assert tw.lines[1] == " def f():"
1117+
assert tw.lines[2] == " try:"
1118+
assert tw.lines[3] == " g()"
1119+
assert tw.lines[4] == " except Exception:"
1120+
assert tw.lines[5] == "> raise AttributeError() from None"
1121+
assert tw.lines[6] == "E AttributeError"
1122+
assert tw.lines[7] == ""
1123+
line = tw.get_write_msg(8)
1124+
assert line.endswith('mod.py')
1125+
assert tw.lines[9] == ":6: AttributeError"
1126+
assert len(tw.lines) == 10
1127+
10981128
@pytest.mark.skipif("sys.version_info[0] < 3")
10991129
@pytest.mark.parametrize('reason, description', [
11001130
('cause', 'The above exception was the direct cause of the following exception:'),

0 commit comments

Comments
 (0)