]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-37961: Fix regression in tracemalloc.Traceback.__repr__ (GH-23805)
authorDaniel Hahler <git@thequod.de>
Wed, 16 Dec 2020 21:38:32 +0000 (22:38 +0100)
committerGitHub <noreply@github.com>
Wed, 16 Dec 2020 21:38:32 +0000 (22:38 +0100)
Regression in 8d59eb1b66c51b2b918da9881c57d07d08df43b7.

Lib/test/test_tracemalloc.py
Lib/tracemalloc.py
Misc/NEWS.d/next/Library/2020-12-16-16-16-33.bpo-37961.jrESEq.rst [new file with mode: 0644]

index a0037f81b88f277e21541a943793969bcf570ffe..556656747bbcbe42eb4f597e41bec2c92d9d2464 100644 (file)
@@ -85,6 +85,25 @@ def traceback_filename(filename):
     return traceback_lineno(filename, 0)
 
 
+class TestTraceback(unittest.TestCase):
+    def test_repr(self):
+        def get_repr(*args) -> str:
+            return repr(tracemalloc.Traceback(*args))
+
+        self.assertEqual(get_repr(()), "<Traceback ()>")
+        self.assertEqual(get_repr((), 0), "<Traceback () total_nframe=0>")
+
+        frames = (("f1", 1), ("f2", 2))
+        exp_repr_frames = (
+            "(<Frame filename='f2' lineno=2>,"
+            " <Frame filename='f1' lineno=1>)"
+        )
+        self.assertEqual(get_repr(frames),
+                         f"<Traceback {exp_repr_frames}>")
+        self.assertEqual(get_repr(frames, 2),
+                         f"<Traceback {exp_repr_frames} total_nframe=2>")
+
+
 class TestTracemallocEnabled(unittest.TestCase):
     def setUp(self):
         if tracemalloc.is_tracing():
@@ -1065,6 +1084,7 @@ class TestCAPI(unittest.TestCase):
 
 def test_main():
     support.run_unittest(
+        TestTraceback,
         TestTracemallocEnabled,
         TestSnapshot,
         TestFilters,
index 69b4170ec82462ff6e0289991324e62e3be91708..cec99c59700fe05e457f3668d2b947cc6bfef9c6 100644 (file)
@@ -226,7 +226,7 @@ class Traceback(Sequence):
         return str(self[0])
 
     def __repr__(self):
-        s = "<Traceback %r" % tuple(self)
+        s = f"<Traceback {tuple(self)}"
         if self._total_nframe is None:
             s += ">"
         else:
diff --git a/Misc/NEWS.d/next/Library/2020-12-16-16-16-33.bpo-37961.jrESEq.rst b/Misc/NEWS.d/next/Library/2020-12-16-16-16-33.bpo-37961.jrESEq.rst
new file mode 100644 (file)
index 0000000..5b363ad
--- /dev/null
@@ -0,0 +1 @@
+Fix crash in :func:`tracemalloc.Traceback.__repr__` (regressed in Python 3.9).
\ No newline at end of file