Skip to content

Commit 40ada89

Browse files
committed
address review comments
1 parent c40a6d5 commit 40ada89

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

Lib/traceback.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
481481
# permit backwards compat with the existing API, otherwise we
482482
# need stub thunk objects just to glue it together.
483483
# Handle loops in __cause__ or __context__.
484-
_is_chained = _seen is not None
484+
is_recursive_call = _seen is not None
485485
if _seen is None:
486486
_seen = set()
487487
_seen.add(id(exc_value))
@@ -506,14 +506,12 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
506506
self.__suppress_context__ = \
507507
exc_value.__suppress_context__ if exc_value else False
508508

509-
# Convert __cause__ and __context__ to TracebackExceptions, use a
510-
# queue to avoid recursion
511-
if not _is_chained:
509+
# Convert __cause__ and __context__ to `TracebackExceptions`s, use a
510+
# queue to avoid recursion (only the top-level call gets _seen == None)
511+
if not is_recursive_call:
512512
queue = [(self, exc_value)]
513513
while queue:
514514
te, e = queue.pop()
515-
# Gracefully handle (the way Python 2.4 and earlier did) the
516-
# case of being called with no type or value (None, None, None)
517515
if (e and e.__cause__ is not None
518516
and id(e.__cause__) not in _seen):
519517
cause = TracebackException(
@@ -631,28 +629,28 @@ def format(self, *, chain=True):
631629
string in the output.
632630
"""
633631

634-
stack = []
632+
output = []
635633
exc = self
636634
while exc:
637635
if chain:
638-
if exc.__cause__:
636+
if exc.__cause__ is not None:
639637
chained_msg = _cause_message
640638
chained_exc = exc.__cause__
641-
elif exc.__context__ and not exc.__suppress_context__:
639+
elif (exc.__context__ is not None and
640+
not exc.__suppress_context__):
642641
chained_msg = _context_message
643642
chained_exc = exc.__context__
644643
else:
645644
chained_msg = None
646645
chained_exc = None
647646

648-
stack.append((chained_msg, exc))
647+
output.append((chained_msg, exc))
649648
exc = chained_exc
650649
else:
651-
stack.append((None, exc))
650+
output.append((None, exc))
652651
exc = None
653652

654-
while stack:
655-
msg, exc = stack.pop()
653+
for msg, exc in reversed(output):
656654
if msg is not None:
657655
yield msg
658656
if exc.stack:

0 commit comments

Comments
 (0)