Issue #20616: Add a format() method to tracemalloc.Traceback.

This commit is contained in:
Victor Stinner 2014-02-16 23:53:38 +01:00
parent 34c1540009
commit a91ff1423f
4 changed files with 59 additions and 7 deletions

View file

@ -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):
"""