Skip to content

Commit

Permalink
bpo-37961: Fix regression in tracemalloc.Traceback.__repr__ (pythonGH…
Browse files Browse the repository at this point in the history
…-23805)

Regression in 8d59eb1.
  • Loading branch information
blueyed authored Dec 16, 2020
1 parent 66d3b58 commit 051b981
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Lib/test/test_tracemalloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -1065,6 +1084,7 @@ def test_stop_untrack(self):

def test_main():
support.run_unittest(
TestTraceback,
TestTracemallocEnabled,
TestSnapshot,
TestFilters,
Expand Down
2 changes: 1 addition & 1 deletion Lib/tracemalloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def __str__(self):
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:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crash in :func:`tracemalloc.Traceback.__repr__` (regressed in Python 3.9).

0 comments on commit 051b981

Please sign in to comment.