summaryrefslogtreecommitdiff
path: root/Lib/traceback.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-05-03 13:19:46 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2015-05-03 13:19:46 +0300
commit24559e483400073c6ae1446c0ac2a7990c755f02 (patch)
tree892132385115a66fc159c577eb3f37b254901d9b /Lib/traceback.py
parent9a578d9ee6ef64ed8ac2db7db5bc2b9233a2dca5 (diff)
downloadcpython-git-24559e483400073c6ae1446c0ac2a7990c755f02.tar.gz
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.py14
1 files changed, 10 insertions, 4 deletions
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 8a554cfc06..61c9fb57ac 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