diff -r 14c1ff6a8086 Doc/library/tracemalloc.rst --- a/Doc/library/tracemalloc.rst Tue Mar 11 08:12:48 2014 +0100 +++ b/Doc/library/tracemalloc.rst Tue Mar 11 08:19:38 2014 +0100 @@ -184,7 +184,6 @@ Pretty top Code to display the 10 lines allocating the most memory with a pretty output, ignoring ```` and ```` files:: - import linecache import os import tracemalloc @@ -202,7 +201,7 @@ ignoring `` filename = os.sep.join(frame.filename.split(os.sep)[-2:]) print("#%s: %s:%s: %.1f KiB" % (index, filename, frame.lineno, stat.size / 1024)) - line = linecache.getline(frame.filename, frame.lineno).strip() + line = stat.getline().strip() if line: print(' %s' % line) @@ -420,6 +419,14 @@ Frame Line number (``int``). + .. method:: get_line() + + Get the line of source code. This function will never raise an exception: + it will return ``''`` on errors. The terminating newline character will + be included for lines that are found. + + See also the :func:`linecache.getline` function. + Snapshot -------- @@ -590,6 +597,14 @@ Trace Traceback where the memory block was allocated, :class:`Traceback` instance. + .. method:: get_line() + + Get the line of source code of the most recent frame. This function will + never raise an exception: it will return ``''`` on errors. The + terminating newline character will be included for lines that are found. + + See also the :func:`linecache.getline` function. + Traceback --------- diff -r 14c1ff6a8086 Lib/test/test_tracemalloc.py --- a/Lib/test/test_tracemalloc.py Tue Mar 11 08:12:48 2014 +0100 +++ b/Lib/test/test_tracemalloc.py Tue Mar 11 08:19:38 2014 +0100 @@ -532,6 +532,20 @@ class TestSnapshot(unittest.TestCase): self.assertEqual(tb.format(limit=-1), []) + def test_get_line(self): + snapshot, snapshot2 = create_snapshots() + def getline(filename, lineno): + return '<%s,%s>' % (filename, lineno) + with unittest.mock.patch('tracemalloc.linecache.getline', + side_effect=getline): + trace= snapshot.traces[0] + self.assertEqual(trace.get_line(), + '') + self.assertEqual(trace.traceback[0].get_line(), + '') + self.assertEqual(trace.traceback[1].get_line(), + '') + class TestFilters(unittest.TestCase): maxDiff = 2048 diff -r 14c1ff6a8086 Lib/tracemalloc.py --- a/Lib/tracemalloc.py Tue Mar 11 08:12:48 2014 +0100 +++ b/Lib/tracemalloc.py Tue Mar 11 08:19:38 2014 +0100 @@ -167,6 +167,9 @@ class Frame: def __repr__(self): return "" % (self.filename, self.lineno) + def get_line(self): + return linecache.getline(self.filename, self.lineno) + @total_ordering class Traceback(Sequence): @@ -216,7 +219,7 @@ class Traceback(Sequence): for frame in self[:limit]: lines.append(' File "%s", line %s' % (frame.filename, frame.lineno)) - line = linecache.getline(frame.filename, frame.lineno).strip() + line = frame.get_line().strip() if line: lines.append(' %s' % line) return lines @@ -269,6 +272,9 @@ class Trace: return ("" % (_format_size(self.size, False), self.traceback)) + def get_line(self): + return self.traceback[0].get_line() + class _Traces(Sequence): def __init__(self, traces):