@@ -118,7 +118,6 @@ Get the traceback of a memory block
118118
119119Code 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
142137Example 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
0 commit comments