Skip to content

Commit 427613f

Browse files
authored
bpo-42482: remove reference to exc_traceback from TracebackException (GH-23531)
1 parent 1244c81 commit 427613f

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
@@ -1123,6 +1123,18 @@ def test_context(self):
11231123
self.assertEqual(exc_info[0], exc.exc_type)
11241124
self.assertEqual(str(exc_info[1]), str(exc))
11251125

1126+
def test_no_refs_to_exception_and_traceback_objects(self):
1127+
try:
1128+
1/0
1129+
except Exception:
1130+
exc_info = sys.exc_info()
1131+
1132+
refcnt1 = sys.getrefcount(exc_info[1])
1133+
refcnt2 = sys.getrefcount(exc_info[2])
1134+
exc = traceback.TracebackException(*exc_info)
1135+
self.assertEqual(sys.getrefcount(exc_info[1]), refcnt1)
1136+
self.assertEqual(sys.getrefcount(exc_info[2]), refcnt2)
1137+
11261138
def test_comparison_basic(self):
11271139
try:
11281140
1/0
@@ -1172,6 +1184,16 @@ def raise_with_locals():
11721184
exc7 = traceback.TracebackException(*exc_info, limit=-2, capture_locals=True)
11731185
self.assertNotEqual(exc6, exc7)
11741186

1187+
def test_comparison_equivalent_exceptions_are_equal(self):
1188+
excs = []
1189+
for _ in range(2):
1190+
try:
1191+
1/0
1192+
except:
1193+
excs.append(traceback.TracebackException(*sys.exc_info()))
1194+
self.assertEqual(excs[0], excs[1])
1195+
self.assertEqual(list(excs[0].format()), list(excs[1].format()))
1196+
11751197
def test_unhashable(self):
11761198
class UnhashableException(Exception):
11771199
def __eq__(self, other):

Lib/traceback.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,6 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
510510
_seen=_seen)
511511
else:
512512
context = None
513-
self.exc_traceback = exc_traceback
514513
self.__cause__ = cause
515514
self.__context__ = context
516515
self.__suppress_context__ = \
@@ -627,7 +626,7 @@ def format(self, *, chain=True):
627626
not self.__suppress_context__):
628627
yield from self.__context__.format(chain=chain)
629628
yield _context_message
630-
if self.exc_traceback is not None:
629+
if self.stack:
631630
yield 'Traceback (most recent call last):\n'
632-
yield from self.stack.format()
631+
yield from self.stack.format()
633632
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)