Skip to content

Commit 40b92f1

Browse files
[3.9] bpo-42482: remove reference to exc_traceback from TracebackException (GH-23531) (GH-23578)
(cherry picked from commit 427613f) Co-authored-by: Irit Katriel <[email protected]>
1 parent 81ac030 commit 40b92f1

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

Lib/test/test_traceback.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,18 @@ def test_context(self):
11031103
self.assertEqual(exc_info[0], exc.exc_type)
11041104
self.assertEqual(str(exc_info[1]), str(exc))
11051105

1106+
def test_no_refs_to_exception_and_traceback_objects(self):
1107+
try:
1108+
1/0
1109+
except Exception:
1110+
exc_info = sys.exc_info()
1111+
1112+
refcnt1 = sys.getrefcount(exc_info[1])
1113+
refcnt2 = sys.getrefcount(exc_info[2])
1114+
exc = traceback.TracebackException(*exc_info)
1115+
self.assertEqual(sys.getrefcount(exc_info[1]), refcnt1)
1116+
self.assertEqual(sys.getrefcount(exc_info[2]), refcnt2)
1117+
11061118
def test_comparison_basic(self):
11071119
try:
11081120
1/0
@@ -1152,6 +1164,16 @@ def raise_with_locals():
11521164
exc7 = traceback.TracebackException(*exc_info, limit=-2, capture_locals=True)
11531165
self.assertNotEqual(exc6, exc7)
11541166

1167+
def test_comparison_equivalent_exceptions_are_equal(self):
1168+
excs = []
1169+
for _ in range(2):
1170+
try:
1171+
1/0
1172+
except:
1173+
excs.append(traceback.TracebackException(*sys.exc_info()))
1174+
self.assertEqual(excs[0], excs[1])
1175+
self.assertEqual(list(excs[0].format()), list(excs[1].format()))
1176+
11551177
def test_unhashable(self):
11561178
class UnhashableException(Exception):
11571179
def __eq__(self, other):

Lib/traceback.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,6 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
500500
_seen=_seen)
501501
else:
502502
context = None
503-
self.exc_traceback = exc_traceback
504503
self.__cause__ = cause
505504
self.__context__ = context
506505
self.__suppress_context__ = \
@@ -617,7 +616,7 @@ def format(self, *, chain=True):
617616
not self.__suppress_context__):
618617
yield from self.__context__.format(chain=chain)
619618
yield _context_message
620-
if self.exc_traceback is not None:
619+
if self.stack:
621620
yield 'Traceback (most recent call last):\n'
622-
yield from self.stack.format()
621+
yield from self.stack.format()
623622
yield from self.format_exception_only()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:class:`~traceback.TracebackException` no longer holds a reference to the exception's traceback object. Consequently, instances of TracebackException for equivalent but non-equal exceptions now compare as equal.

0 commit comments

Comments
 (0)