Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 23f628d

Browse files
committed
Issue #20616: Add a format() method to tracemalloc.Traceback.
1 parent f617fa8 commit 23f628d

4 files changed

Lines changed: 59 additions & 7 deletions

File tree

Doc/library/tracemalloc.rst

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ Get the traceback of a memory block
118118

119119
Code to display the traceback of the biggest memory block::
120120

121-
import linecache
122121
import tracemalloc
123122

124123
# Store 25 frames
@@ -132,12 +131,8 @@ Code to display the traceback of the biggest memory block::
132131
# pick the biggest memory block
133132
stat = top_stats[0]
134133
print("%s memory blocks: %.1f KiB" % (stat.count, stat.size / 1024))
135-
for frame in stat.traceback:
136-
print(' File "%s", line %s' % (frame.filename, frame.lineno))
137-
line = linecache.getline(frame.filename, frame.lineno)
138-
line = line.strip()
139-
if line:
140-
print(' ' + line)
134+
for line in stat.traceback.format():
135+
print(line)
141136

142137
Example of output of the Python test suite (traceback limited to 25 frames)::
143138

@@ -602,4 +597,26 @@ Traceback
602597
The :attr:`Trace.traceback` attribute is an instance of :class:`Traceback`
603598
instance.
604599

600+
.. method:: format(limit=None)
601+
602+
Format the traceback as a list of lines with newlines. Use the
603+
:mod:`linecache` module to retrieve lines from the source code. If
604+
*limit* is set, only format the *limit* most recent frames.
605+
606+
Similar to the :func:`traceback.format_tb` function, except that
607+
:meth:`format` does not include newlines.
608+
609+
Example::
610+
611+
print("Traceback (most recent call first):")
612+
for line in traceback:
613+
print(line)
614+
615+
Output::
616+
617+
Traceback (most recent call first):
618+
File "test.py", line 9
619+
obj = Object()
620+
File "test.py", line 12
621+
tb = tracemalloc.get_object_traceback(f())
605622

Lib/test/test_tracemalloc.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,26 @@ def test_slices(self):
510510
self.assertEqual(traceback[:2],
511511
(traceback[0], traceback[1]))
512512

513+
def test_format_traceback(self):
514+
snapshot, snapshot2 = create_snapshots()
515+
def getline(filename, lineno):
516+
return ' <%s, %s>' % (filename, lineno)
517+
with unittest.mock.patch('tracemalloc.linecache.getline',
518+
side_effect=getline):
519+
tb = snapshot.traces[0].traceback
520+
self.assertEqual(tb.format(),
521+
[' File "a.py", line 2',
522+
' <a.py, 2>',
523+
' File "b.py", line 4',
524+
' <b.py, 4>'])
525+
526+
self.assertEqual(tb.format(limit=1),
527+
[' File "a.py", line 2',
528+
' <a.py, 2>'])
529+
530+
self.assertEqual(tb.format(limit=-1),
531+
[])
532+
513533

514534
class TestFilters(unittest.TestCase):
515535
maxDiff = 2048

Lib/tracemalloc.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import Sequence
22
from functools import total_ordering
33
import fnmatch
4+
import linecache
45
import os.path
56
import pickle
67

@@ -205,6 +206,18 @@ def __str__(self):
205206
def __repr__(self):
206207
return "<Traceback %r>" % (tuple(self),)
207208

209+
def format(self, limit=None):
210+
lines = []
211+
if limit is not None and limit < 0:
212+
return lines
213+
for frame in self[:limit]:
214+
lines.append(' File "%s", line %s'
215+
% (frame.filename, frame.lineno))
216+
line = linecache.getline(frame.filename, frame.lineno).strip()
217+
if line:
218+
lines.append(' %s' % line)
219+
return lines
220+
208221

209222
def get_object_traceback(obj):
210223
"""

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Core and Builtins
2525
Library
2626
-------
2727

28+
- Issue #20616: Add a format() method to tracemalloc.Traceback.
29+
2830
- Issue #19744: the ensurepip installation step now just prints a warning to
2931
stderr rather than failing outright if SSL/TLS is unavailable. This allows
3032
local installation of POSIX builds without SSL/TLS support.

0 commit comments

Comments
 (0)