Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b0d4a2

Browse files
committed
Issue #4486: When an exception has an explicit cause, do not print its implicit context too.
1 parent 1fc0d2b commit 7b0d4a2

4 files changed

Lines changed: 30 additions & 5 deletions

File tree

Lib/test/test_traceback.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,26 @@ def outer_raise():
253253
self.check_zero_div(blocks[0])
254254
self.assertTrue('inner_raise() # Marker' in blocks[2])
255255

256+
def test_cause_and_context(self):
257+
# When both a cause and a context are set, only the cause should be
258+
# displayed and the context should be muted.
259+
def inner_raise():
260+
try:
261+
self.zero_div()
262+
except ZeroDivisionError as _e:
263+
e = _e
264+
try:
265+
xyzzy
266+
except NameError:
267+
raise KeyError from e
268+
def outer_raise():
269+
inner_raise() # Marker
270+
blocks = boundaries.split(self.get_report(outer_raise))
271+
self.assertEquals(len(blocks), 3)
272+
self.assertEquals(blocks[1], cause_message)
273+
self.check_zero_div(blocks[0])
274+
self.assert_('inner_raise() # Marker' in blocks[2])
275+
256276
def test_cause_recursive(self):
257277
def inner_raise():
258278
try:

Lib/traceback.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,14 @@ def _iter_chain(exc, custom_tb=None, seen=None):
120120
seen.add(exc)
121121
its = []
122122
cause = exc.__cause__
123-
context = exc.__context__
124123
if cause is not None and cause not in seen:
125124
its.append(_iter_chain(cause, None, seen))
126125
its.append([(_cause_message, None)])
127-
if context is not None and context is not cause and context not in seen:
128-
its.append(_iter_chain(context, None, seen))
129-
its.append([(_context_message, None)])
126+
else:
127+
context = exc.__context__
128+
if context is not None and context not in seen:
129+
its.append(_iter_chain(context, None, seen))
130+
its.append([(_context_message, None)])
130131
its.append([(exc, custom_tb or exc.__traceback__)])
131132
# itertools.chain is in an extension module and may be unavailable
132133
for it in its:

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ C-API
140140
Library
141141
-------
142142

143+
- Issue #4486: When an exception has an explicit cause, do not print its
144+
implicit context too. This affects the `traceback` module as well as
145+
built-in exception printing.
146+
143147
- Issue #1515: Enable use of deepcopy() with instance methods. Patch by
144148
Robert Collins.
145149

Python/pythonrun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
15761576
cause_message, f);
15771577
}
15781578
}
1579-
if (context) {
1579+
else if (context) {
15801580
res = PySet_Contains(seen, context);
15811581
if (res == -1)
15821582
PyErr_Clear();

0 commit comments

Comments
 (0)