]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42482: remove reference to exc_traceback from TracebackException (GH-23531)
authorIrit Katriel <iritkatriel@yahoo.com>
Tue, 1 Dec 2020 01:35:25 +0000 (01:35 +0000)
committerGitHub <noreply@github.com>
Tue, 1 Dec 2020 01:35:25 +0000 (17:35 -0800)
Lib/test/test_traceback.py
Lib/traceback.py
Misc/NEWS.d/next/Library/2020-11-27-16-46-58.bpo-42482.EJC3sd.rst [new file with mode: 0644]

index f86afc673d86e7b680a4b01429abba10f46bb257..5df701caf0f01e71afa6f5fcffc94410e022e3af 100644 (file)
@@ -1123,6 +1123,18 @@ class TestTracebackException(unittest.TestCase):
         self.assertEqual(exc_info[0], exc.exc_type)
         self.assertEqual(str(exc_info[1]), str(exc))
 
+    def test_no_refs_to_exception_and_traceback_objects(self):
+        try:
+            1/0
+        except Exception:
+            exc_info = sys.exc_info()
+
+        refcnt1 = sys.getrefcount(exc_info[1])
+        refcnt2 = sys.getrefcount(exc_info[2])
+        exc = traceback.TracebackException(*exc_info)
+        self.assertEqual(sys.getrefcount(exc_info[1]), refcnt1)
+        self.assertEqual(sys.getrefcount(exc_info[2]), refcnt2)
+
     def test_comparison_basic(self):
         try:
             1/0
@@ -1172,6 +1184,16 @@ class TestTracebackException(unittest.TestCase):
         exc7 = traceback.TracebackException(*exc_info, limit=-2, capture_locals=True)
         self.assertNotEqual(exc6, exc7)
 
+    def test_comparison_equivalent_exceptions_are_equal(self):
+        excs = []
+        for _ in range(2):
+            try:
+                1/0
+            except:
+                excs.append(traceback.TracebackException(*sys.exc_info()))
+        self.assertEqual(excs[0], excs[1])
+        self.assertEqual(list(excs[0].format()), list(excs[1].format()))
+
     def test_unhashable(self):
         class UnhashableException(Exception):
             def __eq__(self, other):
index d2d93c8a32ac290d6d10714916e1ba122b87f862..457d92511af0512545a799f28e34249fdc961176 100644 (file)
@@ -510,7 +510,6 @@ class TracebackException:
                 _seen=_seen)
         else:
             context = None
-        self.exc_traceback = exc_traceback
         self.__cause__ = cause
         self.__context__ = context
         self.__suppress_context__ = \
@@ -627,7 +626,7 @@ class TracebackException:
                 not self.__suppress_context__):
                 yield from self.__context__.format(chain=chain)
                 yield _context_message
-        if self.exc_traceback is not None:
+        if self.stack:
             yield 'Traceback (most recent call last):\n'
-        yield from self.stack.format()
+            yield from self.stack.format()
         yield from self.format_exception_only()
diff --git a/Misc/NEWS.d/next/Library/2020-11-27-16-46-58.bpo-42482.EJC3sd.rst b/Misc/NEWS.d/next/Library/2020-11-27-16-46-58.bpo-42482.EJC3sd.rst
new file mode 100644 (file)
index 0000000..79afa65
--- /dev/null
@@ -0,0 +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.
\ No newline at end of file