diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-05-03 10:19:46 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-05-03 10:19:46 (GMT) |
commit | 24559e483400073c6ae1446c0ac2a7990c755f02 (patch) | |
tree | 892132385115a66fc159c577eb3f37b254901d9b /Lib/traceback.py | |
parent | 9a578d9ee6ef64ed8ac2db7db5bc2b9233a2dca5 (diff) | |
download | cpython-24559e483400073c6ae1446c0ac2a7990c755f02.zip cpython-24559e483400073c6ae1446c0ac2a7990c755f02.tar.gz cpython-24559e483400073c6ae1446c0ac2a7990c755f02.tar.bz2 |
Issue #22619: Added negative limit support in the traceback module.
Based on patch by Dmitry Kazakov.
Diffstat (limited to 'Lib/traceback.py')
-rw-r--r-- | Lib/traceback.py | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/Lib/traceback.py b/Lib/traceback.py index 8a554cf..61c9fb5 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1,8 +1,9 @@ """Extract, format and print information about Python stack traces.""" +import collections +import itertools import linecache import sys -import operator __all__ = ['extract_stack', 'extract_tb', 'format_exception', 'format_exception_only', 'format_list', 'format_stack', @@ -315,12 +316,17 @@ class StackSummary(list): """ if limit is None: limit = getattr(sys, 'tracebacklimit', None) + if limit is not None and limit < 0: + limit = 0 + if limit is not None: + if limit >= 0: + frame_gen = itertools.islice(frame_gen, limit) + else: + frame_gen = collections.deque(frame_gen, maxlen=-limit) result = klass() fnames = set() - for pos, (f, lineno) in enumerate(frame_gen): - if limit is not None and pos >= limit: - break + for f, lineno in frame_gen: co = f.f_code filename = co.co_filename name = co.co_name |