diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-02-16 23:53:38 +0100 |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-02-16 23:53:38 +0100 |
commit | a91ff1423fbd57e7bd0853ac494d8cdea1fb5bb9 (patch) | |
tree | b8c65ceade43aeacb44551211d63be995e07a493 /Lib/tracemalloc.py | |
parent | 34c15400092bba37ea36e155d151cfcbfa846db7 (diff) | |
download | cpython-git-a91ff1423fbd57e7bd0853ac494d8cdea1fb5bb9.tar.gz |
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 b075946939..6f0a234244 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): """ |