diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-02-16 22:53:38 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-02-16 22:53:38 (GMT) |
commit | 23f628de4ab75acde14de9593793e67ec74d851c (patch) | |
tree | 359a01f47944f6e16dabe222f76a705ab4d02844 /Lib/tracemalloc.py | |
parent | f617fa88debe53584b9986f86d9f5c4e2397d5f1 (diff) | |
download | cpython-23f628de4ab75acde14de9593793e67ec74d851c.zip cpython-23f628de4ab75acde14de9593793e67ec74d851c.tar.gz cpython-23f628de4ab75acde14de9593793e67ec74d851c.tar.bz2 |
Issue #20616: Add a format() method to tracemalloc.Traceback.
Diffstat (limited to 'Lib/tracemalloc.py')
-rw-r--r-- | Lib/tracemalloc.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/Lib/tracemalloc.py b/Lib/tracemalloc.py index b075946..6f0a234 100644 --- a/Lib/tracemalloc.py +++ b/Lib/tracemalloc.py @@ -1,6 +1,7 @@ from collections import Sequence from functools import total_ordering import fnmatch +import linecache import os.path import pickle @@ -205,6 +206,18 @@ class Traceback(Sequence): def __repr__(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): """ |