aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_traceback.py20
-rw-r--r--Lib/traceback.py9
2 files changed, 25 insertions, 4 deletions
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 2145710ffc7..17413db7f9e 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -253,6 +253,26 @@ class BaseExceptionReportingTests:
self.check_zero_div(blocks[0])
self.assertTrue('inner_raise() # Marker' in blocks[2])
+ def test_cause_and_context(self):
+ # When both a cause and a context are set, only the cause should be
+ # displayed and the context should be muted.
+ def inner_raise():
+ try:
+ self.zero_div()
+ except ZeroDivisionError as _e:
+ e = _e
+ try:
+ xyzzy
+ except NameError:
+ raise KeyError from e
+ def outer_raise():
+ inner_raise() # Marker
+ blocks = boundaries.split(self.get_report(outer_raise))
+ self.assertEquals(len(blocks), 3)
+ self.assertEquals(blocks[1], cause_message)
+ self.check_zero_div(blocks[0])
+ self.assert_('inner_raise() # Marker' in blocks[2])
+
def test_cause_recursive(self):
def inner_raise():
try:
diff --git a/Lib/traceback.py b/Lib/traceback.py
index c0d8061ecd3..8d4e96edcb6 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -120,13 +120,14 @@ def _iter_chain(exc, custom_tb=None, seen=None):
seen.add(exc)
its = []
cause = exc.__cause__
- context = exc.__context__
if cause is not None and cause not in seen:
its.append(_iter_chain(cause, None, seen))
its.append([(_cause_message, None)])
- if context is not None and context is not cause and context not in seen:
- its.append(_iter_chain(context, None, seen))
- its.append([(_context_message, None)])
+ else:
+ context = exc.__context__
+ if context is not None and context not in seen:
+ its.append(_iter_chain(context, None, seen))
+ its.append([(_context_message, None)])
its.append([(exc, custom_tb or exc.__traceback__)])
# itertools.chain is in an extension module and may be unavailable
for it in its: