mirror of https://github.com/python/cpython.git
Issue #20616: Add a format() method to tracemalloc.Traceback.
This commit is contained in:
parent
34c1540009
commit
a91ff1423f
|
@ -118,7 +118,6 @@ Get the traceback of a memory block
|
||||||
|
|
||||||
Code to display the traceback of the biggest memory block::
|
Code to display the traceback of the biggest memory block::
|
||||||
|
|
||||||
import linecache
|
|
||||||
import tracemalloc
|
import tracemalloc
|
||||||
|
|
||||||
# Store 25 frames
|
# Store 25 frames
|
||||||
|
@ -132,12 +131,8 @@ Code to display the traceback of the biggest memory block::
|
||||||
# pick the biggest memory block
|
# pick the biggest memory block
|
||||||
stat = top_stats[0]
|
stat = top_stats[0]
|
||||||
print("%s memory blocks: %.1f KiB" % (stat.count, stat.size / 1024))
|
print("%s memory blocks: %.1f KiB" % (stat.count, stat.size / 1024))
|
||||||
for frame in stat.traceback:
|
for line in stat.traceback.format():
|
||||||
print(' File "%s", line %s' % (frame.filename, frame.lineno))
|
print(line)
|
||||||
line = linecache.getline(frame.filename, frame.lineno)
|
|
||||||
line = line.strip()
|
|
||||||
if line:
|
|
||||||
print(' ' + line)
|
|
||||||
|
|
||||||
Example of output of the Python test suite (traceback limited to 25 frames)::
|
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`
|
The :attr:`Trace.traceback` attribute is an instance of :class:`Traceback`
|
||||||
instance.
|
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())
|
||||||
|
|
||||||
|
|
|
@ -510,6 +510,26 @@ def test_slices(self):
|
||||||
self.assertEqual(traceback[:2],
|
self.assertEqual(traceback[:2],
|
||||||
(traceback[0], traceback[1]))
|
(traceback[0], traceback[1]))
|
||||||
|
|
||||||
|
def test_format_traceback(self):
|
||||||
|
snapshot, snapshot2 = create_snapshots()
|
||||||
|
def getline(filename, lineno):
|
||||||
|
return ' <%s, %s>' % (filename, lineno)
|
||||||
|
with unittest.mock.patch('tracemalloc.linecache.getline',
|
||||||
|
side_effect=getline):
|
||||||
|
tb = snapshot.traces[0].traceback
|
||||||
|
self.assertEqual(tb.format(),
|
||||||
|
[' File "a.py", line 2',
|
||||||
|
' <a.py, 2>',
|
||||||
|
' File "b.py", line 4',
|
||||||
|
' <b.py, 4>'])
|
||||||
|
|
||||||
|
self.assertEqual(tb.format(limit=1),
|
||||||
|
[' File "a.py", line 2',
|
||||||
|
' <a.py, 2>'])
|
||||||
|
|
||||||
|
self.assertEqual(tb.format(limit=-1),
|
||||||
|
[])
|
||||||
|
|
||||||
|
|
||||||
class TestFilters(unittest.TestCase):
|
class TestFilters(unittest.TestCase):
|
||||||
maxDiff = 2048
|
maxDiff = 2048
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from collections import Sequence
|
from collections import Sequence
|
||||||
from functools import total_ordering
|
from functools import total_ordering
|
||||||
import fnmatch
|
import fnmatch
|
||||||
|
import linecache
|
||||||
import os.path
|
import os.path
|
||||||
import pickle
|
import pickle
|
||||||
|
|
||||||
|
@ -205,6 +206,18 @@ def __str__(self):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<Traceback %r>" % (tuple(self),)
|
return "<Traceback %r>" % (tuple(self),)
|
||||||
|
|
||||||
|
def format(self, limit=None):
|
||||||
|
lines = []
|
||||||
|
if limit is not None and limit < 0:
|
||||||
|
return lines
|
||||||
|
for frame in self[:limit]:
|
||||||
|
lines.append(' File "%s", line %s'
|
||||||
|
% (frame.filename, frame.lineno))
|
||||||
|
line = linecache.getline(frame.filename, frame.lineno).strip()
|
||||||
|
if line:
|
||||||
|
lines.append(' %s' % line)
|
||||||
|
return lines
|
||||||
|
|
||||||
|
|
||||||
def get_object_traceback(obj):
|
def get_object_traceback(obj):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -23,6 +23,8 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #20616: Add a format() method to tracemalloc.Traceback.
|
||||||
|
|
||||||
- Issue #19744: the ensurepip installation step now just prints a warning to
|
- Issue #19744: the ensurepip installation step now just prints a warning to
|
||||||
stderr rather than failing outright if SSL/TLS is unavailable. This allows
|
stderr rather than failing outright if SSL/TLS is unavailable. This allows
|
||||||
local installation of POSIX builds without SSL/TLS support.
|
local installation of POSIX builds without SSL/TLS support.
|
||||||
|
|
Loading…
Reference in New Issue