diff -r 535232275fa2 Lib/test/test_traceback.py --- a/Lib/test/test_traceback.py Tue Jan 06 13:58:57 2015 +0100 +++ b/Lib/test/test_traceback.py Tue Jan 06 08:13:10 2015 -0500 @@ -447,6 +447,70 @@ return s.getvalue() +class LimitTests(unittest.TestCase): + + ''' Tests for limit argument. + It's enough to test extact_tb, extract_stack and format_exception ''' + + def last_raises(self, num): + # Explicitly raise exception after num recursive calls + num -= 1 + if not num: + raise Exception('Last raised') + else: + self.last_raises(num) + + def last_returns_frame(self, num): + # Return a frame after num recursive calls + num -= 1 + return self.last_returns_frame(num) if num else sys._getframe() + + def _assert_loaded(self, n, nolim, poslim, neglim, stack=False): + # Must not be empty + self.assertTrue(neglim) + self.assertTrue(poslim) + # The lengths must be equal + self.assertEqual(len(poslim), len(neglim)) + # The order is different, but this is the intended behaviour + if stack: + self.assertEqual(nolim[-n:], poslim) + self.assertEqual(nolim[:n], neglim) + else: + self.assertEqual(nolim[:n], poslim) + self.assertEqual(nolim[-n:], neglim) + + def test_stack_limit(self): + for n in (1, 2, 3): + with self.subTest(calls=n): + frame = self.last_returns_frame(n) + nolim = traceback.extract_stack(frame) + poslim = traceback.extract_stack(frame, limit=n) + neglim = traceback.extract_stack(frame, limit=-n) + self._assert_loaded(n, nolim, poslim, neglim, stack=True) + + def test_tb_exception_limit(self): + for n in (1, 2, 3): + try: + self.last_raises(n) + except Exception: + exc_type, exc_value, tb = sys.exc_info() + # Traceback subtest + with self.subTest(calls=n, type='traceback'): + nolim = traceback.extract_tb(tb) + poslim = traceback.extract_tb(tb, limit=n) + neglim = traceback.extract_tb(tb, limit=-n) + self._assert_loaded(n, nolim, poslim, neglim) + + # Exception subtest + with self.subTest(calls=n, type='exception'): + nolim = traceback.format_exception(exc_type, exc_value, tb) + poslim = traceback.format_exception(exc_type, exc_value, tb, limit=n) + neglim = traceback.format_exception(exc_type, exc_value, tb, limit=-n) + # [1:-1] to exclude "Traceback (...)" header and + # exception type and value + self._assert_loaded(n, nolim[1:-1], poslim[1:-1], neglim[1:-1]) + + class MiscTracebackCases(unittest.TestCase): # # Check non-printing functions in traceback module diff -r 535232275fa2 Lib/traceback.py --- a/Lib/traceback.py Tue Jan 06 13:58:57 2015 +0100 +++ b/Lib/traceback.py Tue Jan 06 08:13:10 2015 -0500 @@ -3,6 +3,7 @@ import linecache import sys import operator +import collections __all__ = ['extract_stack', 'extract_tb', 'format_exception', 'format_exception_only', 'format_list', 'format_stack', @@ -50,10 +51,8 @@ # - Line number # - Next item (same type as curr) # In practice, curr is either a traceback or a frame. -def _extract_tb_or_stack_iter(curr, limit, extractor): - if limit is None: - limit = getattr(sys, 'tracebacklimit', None) - +def _extract_tb_or_stack_iter_raw(curr, limit, extractor): + # Yield (filename, lineno, name, f_globals) tuples n = 0 while curr is not None and (limit is None or n < limit): f, lineno, next_item = extractor(curr) @@ -61,18 +60,28 @@ filename = co.co_filename name = co.co_name - linecache.checkcache(filename) - line = linecache.getline(filename, lineno, f.f_globals) + yield (filename, lineno, name, f.f_globals) - if line: - line = line.strip() - else: - line = None - - yield (filename, lineno, name, line) curr = next_item n += 1 +def _extract_tb_or_stack_iter(curr, limit, extractor): + # Check if limit is negative + negative_limit = limit is not None and limit < 0 + + if limit is None or negative_limit: + _limit = getattr(sys, 'tracebacklimit', None) + else: + _limit = limit + + raw = _extract_tb_or_stack_iter_raw(curr, _limit, extractor) + it = collections.deque(raw, maxlen=abs(limit)) if negative_limit else raw + + for filename, lineno, name, f_globals in it: + linecache.checkcache(filename) + line = linecache.getline(filename, lineno, f_globals) + yield (filename, lineno, name, line.strip() if line else None) + def _extract_tb_iter(tb, limit): return _extract_tb_or_stack_iter( tb, limit, @@ -311,3 +320,4 @@ # Ignore the exception raised if the frame is still executing. pass tb = tb.tb_next +