summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-02-16 22:53:38 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2014-02-16 22:53:38 (GMT)
commita91ff1423fbd57e7bd0853ac494d8cdea1fb5bb9 (patch)
treeb8c65ceade43aeacb44551211d63be995e07a493 /Doc/library
parent34c15400092bba37ea36e155d151cfcbfa846db7 (diff)
downloadcpython-a91ff1423fbd57e7bd0853ac494d8cdea1fb5bb9.zip
cpython-a91ff1423fbd57e7bd0853ac494d8cdea1fb5bb9.tar.gz
cpython-a91ff1423fbd57e7bd0853ac494d8cdea1fb5bb9.tar.bz2
Issue #20616: Add a format() method to tracemalloc.Traceback.
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/tracemalloc.rst31
1 files changed, 24 insertions, 7 deletions
diff --git a/Doc/library/tracemalloc.rst b/Doc/library/tracemalloc.rst
index e49d4ca..b79c5f6 100644
--- a/Doc/library/tracemalloc.rst
+++ b/Doc/library/tracemalloc.rst
@@ -118,7 +118,6 @@ Get the traceback of a memory block
Code to display the traceback of the biggest memory block::
- import linecache
import tracemalloc
# Store 25 frames
@@ -132,12 +131,8 @@ Code to display the traceback of the biggest memory block::
# pick the biggest memory block
stat = top_stats[0]
print("%s memory blocks: %.1f KiB" % (stat.count, stat.size / 1024))
- for frame in stat.traceback:
- print(' File "%s", line %s' % (frame.filename, frame.lineno))
- line = linecache.getline(frame.filename, frame.lineno)
- line = line.strip()
- if line:
- print(' ' + line)
+ for line in stat.traceback.format():
+ print(line)
Example of output of the Python test suite (traceback limited to 25 frames)::
@@ -602,4 +597,26 @@ Traceback
The :attr:`Trace.traceback` attribute is an instance of :class:`Traceback`
instance.
+ .. method:: format(limit=None)
+
+ Format the traceback as a list of lines with newlines. Use the
+ :mod:`linecache` module to retrieve lines from the source code. If
+ *limit* is set, only format the *limit* most recent frames.
+
+ Similar to the :func:`traceback.format_tb` function, except that
+ :meth:`format` does not include newlines.
+
+ Example::
+
+ print("Traceback (most recent call first):")
+ for line in traceback:
+ print(line)
+
+ Output::
+
+ Traceback (most recent call first):
+ File "test.py", line 9
+ obj = Object()
+ File "test.py", line 12
+ tb = tracemalloc.get_object_traceback(f())