[Python-checkins] bpo-42482: remove reference to exc_traceback from TracebackException (GH-23531)
miss-islington
webhook-mailer at python.org
Mon Nov 30 20:53:48 EST 2020
https://github.com/python/cpython/commit/1cc5c943c007b92116f06b3ec8e71f2a510d1898
commit: 1cc5c943c007b92116f06b3ec8e71f2a510d1898
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2020-11-30T17:53:40-08:00
summary:
bpo-42482: remove reference to exc_traceback from TracebackException (GH-23531)
(cherry picked from commit 427613f005f0f412d12f0d775d2b609bae0ae1ad)
Co-authored-by: Irit Katriel <iritkatriel at yahoo.com>
files:
A Misc/NEWS.d/next/Library/2020-11-27-16-46-58.bpo-42482.EJC3sd.rst
M Lib/test/test_traceback.py
M Lib/traceback.py
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 8a80d5802bb41..a5ba6090cf08d 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -1083,6 +1083,18 @@ def test_context(self):
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
@@ -1133,6 +1145,16 @@ def raise_with_locals():
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):
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 5ef3be74be74a..19de84b26bf35 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -500,7 +500,6 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
_seen=_seen)
else:
context = None
- self.exc_traceback = exc_traceback
self.__cause__ = cause
self.__context__ = context
self.__suppress_context__ = \
@@ -608,7 +607,7 @@ def format(self, *, chain=True):
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
index 0000000000000..79afa654f352e
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-11-27-16-46-58.bpo-42482.EJC3sd.rst
@@ -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
More information about the Python-checkins
mailing list