Issue #22619: Added negative limit support in the traceback module.

Based on patch by Dmitry Kazakov.
This commit is contained in:
Serhiy Storchaka 2015-05-03 13:19:46 +03:00
parent 9a578d9ee6
commit 24559e4834
5 changed files with 170 additions and 27 deletions

View file

@ -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