diff options
author | Jesse-Bakker <jessebakker00@gmail.com> | 2017-11-29 23:05:07 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2017-11-29 23:05:07 (GMT) |
commit | 706e10b186992e086e661a62d2c8ec9588525b31 (patch) | |
tree | 6ef28e2e18a1183757d3a31236c2b5e8b4434254 /Lib/tracemalloc.py | |
parent | 859f7ce7a49f8bd6309748c05e460c10f6a433ed (diff) | |
download | cpython-706e10b186992e086e661a62d2c8ec9588525b31.zip cpython-706e10b186992e086e661a62d2c8ec9588525b31.tar.gz cpython-706e10b186992e086e661a62d2c8ec9588525b31.tar.bz2 |
bpo-32121: Add most_recent_first parameter to tracemalloc.Traceback.format (#4534)
* Add most_recent_first parameter to tracemalloc.Traceback.format to allow
reversing the order of the frames in the output
* Reversed default sorting of tracemalloc.Traceback frames
* Allowed negative limit, truncating from the other side.
Diffstat (limited to 'Lib/tracemalloc.py')
-rw-r--r-- | Lib/tracemalloc.py | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/Lib/tracemalloc.py b/Lib/tracemalloc.py index 597a297..2c1ac3b 100644 --- a/Lib/tracemalloc.py +++ b/Lib/tracemalloc.py @@ -171,16 +171,18 @@ class Frame: @total_ordering class Traceback(Sequence): """ - Sequence of Frame instances sorted from the most recent frame - to the oldest frame. + Sequence of Frame instances sorted from the oldest frame + to the most recent frame. """ __slots__ = ("_frames",) def __init__(self, frames): Sequence.__init__(self) # frames is a tuple of frame tuples: see Frame constructor for the - # format of a frame tuple - self._frames = frames + # format of a frame tuple; it is reversed, because _tracemalloc + # returns frames sorted from most recent to oldest, but the + # Python API expects oldest to most recent + self._frames = tuple(reversed(frames)) def __len__(self): return len(self._frames) @@ -209,11 +211,19 @@ class Traceback(Sequence): def __repr__(self): return "<Traceback %r>" % (tuple(self),) - def format(self, limit=None): + def format(self, limit=None, most_recent_first=False): lines = [] - if limit is not None and limit < 0: - return lines - for frame in self[:limit]: + if limit is not None: + if limit > 0: + frame_slice = self[-limit:] + else: + frame_slice = self[:limit] + else: + frame_slice = self + + if most_recent_first: + frame_slice = reversed(frame_slice) + for frame in frame_slice: lines.append(' File "%s", line %s' % (frame.filename, frame.lineno)) line = linecache.getline(frame.filename, frame.lineno).strip() |