diff -r 684b75600fa9 Lib/traceback.py --- a/Lib/traceback.py Tue Mar 19 17:22:51 2013 -0700 +++ b/Lib/traceback.py Tue Mar 19 19:39:47 2013 -0700 @@ -50,25 +50,7 @@ 'file' should be an open file or file-like object with a write() method. """ - if file is None: - file = sys.stderr - if limit is None: - if hasattr(sys, 'tracebacklimit'): - limit = sys.tracebacklimit - n = 0 - while tb is not None and (limit is None or n < limit): - f = tb.tb_frame - lineno = tb.tb_lineno - co = f.f_code - filename = co.co_filename - name = co.co_name - _print(file, - ' File "%s", line %d, in %s' % (filename, lineno, name)) - linecache.checkcache(filename) - line = linecache.getline(filename, lineno, f.f_globals) - if line: _print(file, ' ' + line.strip()) - tb = tb.tb_next - n = n+1 + _get_tb(tb, limit, should_print=True, output=file) def format_tb(tb, limit=None): """A shorthand for 'format_list(extract_stack(f, limit)).""" @@ -85,10 +67,17 @@ leading and trailing whitespace stripped; if the source is not available it is None. """ + return _get_tb(tb, limit, should_print=False) + +def _get_tb(tb, limit=None, should_print=False, output=None): + if should_print and output is None: + output = sys.stderr + if limit is None: if hasattr(sys, 'tracebacklimit'): limit = sys.tracebacklimit - list = [] + + lines = [] n = 0 while tb is not None and (limit is None or n < limit): f = tb.tb_frame @@ -96,15 +85,21 @@ co = f.f_code filename = co.co_filename name = co.co_name + if should_print: + _print(output, + ' File "%s", line %d, in %s' % (filename, lineno, name)) linecache.checkcache(filename) line = linecache.getline(filename, lineno, f.f_globals) - if line: line = line.strip() - else: line = None - list.append((filename, lineno, name, line)) + if line: + line = line.strip() + if should_print: + _print(output, ' ' + line) + else: + line = None + lines.append((filename, lineno, name, line)) tb = tb.tb_next - n = n+1 - return list - + n = n + 1 + return lines _cause_message = ( "\nThe above exception was the direct cause "