annotate Lib/traceback.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 9eaa3e28e514
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 7718
diff changeset
1 """Extract, format and print information about Python stack traces."""
1699
93df83022be8 New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
93df83022be8 New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 import linecache
93df83022be8 New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 import sys
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
5 import operator
1699
93df83022be8 New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16548
diff changeset
7 __all__ = ['extract_stack', 'extract_tb', 'format_exception',
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16548
diff changeset
8 'format_exception_only', 'format_list', 'format_stack',
30862
d45f8981fdea Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24844
diff changeset
9 'format_tb', 'print_exc', 'format_exc', 'print_exception',
85723
100606ef02cf #1565525: Add traceback.clear_frames() helper function to clear locals ref'd by a traceback
Andrew Kuchling <amk@amk.ca>
parents: 83553
diff changeset
10 'print_last', 'print_stack', 'print_tb',
100606ef02cf #1565525: Add traceback.clear_frames() helper function to clear locals ref'd by a traceback
Andrew Kuchling <amk@amk.ca>
parents: 83553
diff changeset
11 'clear_frames']
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16548
diff changeset
12
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
13 #
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
14 # Formatting and printing lists of traceback lines.
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
15 #
16889
0feb63490093 final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents: 16548
diff changeset
16
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
17 def _format_list_iter(extracted_list):
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
18 for filename, lineno, name, line in extracted_list:
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
19 item = ' File "{}", line {}, in {}\n'.format(filename, lineno, name)
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
20 if line:
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
21 item = item + ' {}\n'.format(line.strip())
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
22 yield item
4004
d3a1f6088c28 Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents: 3640
diff changeset
23
d3a1f6088c28 Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents: 3640
diff changeset
24 def print_list(extracted_list, file=None):
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
25 """Print the list of tuples as returned by extract_tb() or
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
26 extract_stack() as a formatted stack trace to the given file."""
23628
ea979c5206b7 Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents: 18067
diff changeset
27 if file is None:
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
28 file = sys.stderr
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
29 for item in _format_list_iter(extracted_list):
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
30 print(item, file=file, end="")
4004
d3a1f6088c28 Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents: 3640
diff changeset
31
d3a1f6088c28 Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents: 3640
diff changeset
32 def format_list(extracted_list):
17113
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
33 """Format a list of traceback entry tuples for printing.
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
34
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
35 Given a list of tuples as returned by extract_tb() or
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
36 extract_stack(), return a list of strings ready for printing.
17113
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
37 Each string in the resulting list corresponds to the item with the
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
38 same index in the argument list. Each string ends in a newline;
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
39 the strings may contain internal newlines as well, for those items
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
40 whose source text line is not None.
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
41 """
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
42 return list(_format_list_iter(extracted_list))
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
43
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
44 #
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
45 # Printing and Extracting Tracebacks.
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
46 #
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
47
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
48 # extractor takes curr and needs to return a tuple of:
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
49 # - Frame object
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
50 # - Line number
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
51 # - Next item (same type as curr)
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
52 # In practice, curr is either a traceback or a frame.
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
53 def _extract_tb_or_stack_iter(curr, limit, extractor):
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
54 if limit is None:
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
55 limit = getattr(sys, 'tracebacklimit', None)
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
56
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
57 n = 0
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
58 while curr is not None and (limit is None or n < limit):
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
59 f, lineno, next_item = extractor(curr)
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
60 co = f.f_code
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
61 filename = co.co_filename
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
62 name = co.co_name
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
63
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
64 linecache.checkcache(filename)
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
65 line = linecache.getline(filename, lineno, f.f_globals)
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
66
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
67 if line:
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
68 line = line.strip()
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
69 else:
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
70 line = None
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
71
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
72 yield (filename, lineno, name, line)
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
73 curr = next_item
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
74 n += 1
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
75
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
76 def _extract_tb_iter(tb, limit):
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
77 return _extract_tb_or_stack_iter(
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
78 tb, limit,
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
79 operator.attrgetter("tb_frame", "tb_lineno", "tb_next"))
3115
9497cdcdb6aa add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents: 2901
diff changeset
80
9497cdcdb6aa add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents: 2901
diff changeset
81 def print_tb(tb, limit=None, file=None):
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
82 """Print up to 'limit' stack trace entries from the traceback 'tb'.
17113
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
83
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
84 If 'limit' is omitted or None, all entries are printed. If 'file'
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
85 is omitted or None, the output goes to sys.stderr; otherwise
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
86 'file' should be an open file or file-like object with a write()
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
87 method.
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
88 """
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
89 print_list(extract_tb(tb, limit=limit), file=file)
1699
93df83022be8 New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
56002
fa91270b6087 Merged revisions 74821,74828-74831,74833,74835 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54571
diff changeset
91 def format_tb(tb, limit=None):
86351
dcded8c7af89 Closes #16657: fix docstring of traceback.format_tb().
Georg Brandl <georg@python.org>
parents: 76942
diff changeset
92 """A shorthand for 'format_list(extract_tb(tb, limit))'."""
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
93 return format_list(extract_tb(tb, limit=limit))
2901
57ec24e0f35a added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents: 2440
diff changeset
94
56002
fa91270b6087 Merged revisions 74821,74828-74831,74833,74835 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54571
diff changeset
95 def extract_tb(tb, limit=None):
17113
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
96 """Return list of up to limit pre-processed entries from traceback.
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
97
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
98 This is useful for alternate formatting of stack traces. If
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
99 'limit' is omitted or None, all entries are extracted. A
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
100 pre-processed stack trace entry is a quadruple (filename, line
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
101 number, function name, text) representing the information that is
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
102 usually printed for a stack trace. The text is a string with
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
103 leading and trailing whitespace stripped; if the source is not
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
104 available it is None.
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
105 """
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
106 return list(_extract_tb_iter(tb, limit=limit))
1699
93df83022be8 New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff changeset
107
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
108 #
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
109 # Exception formatting and output.
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
110 #
2901
57ec24e0f35a added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents: 2440
diff changeset
111
48474
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
112 _cause_message = (
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
113 "\nThe above exception was the direct cause "
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
114 "of the following exception:\n")
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
115
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
116 _context_message = (
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
117 "\nDuring handling of the above exception, "
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
118 "another exception occurred:\n")
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
119
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
120 def _iter_chain(exc, custom_tb=None, seen=None):
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
121 if seen is None:
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
122 seen = set()
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
123 seen.add(exc)
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
124 its = []
76942
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 75292
diff changeset
125 context = exc.__context__
48474
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
126 cause = exc.__cause__
76942
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 75292
diff changeset
127 if cause is not None and cause not in seen:
75292
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 57498
diff changeset
128 its.append(_iter_chain(cause, False, seen))
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 57498
diff changeset
129 its.append([(_cause_message, None)])
76942
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 75292
diff changeset
130 elif (context is not None and
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 75292
diff changeset
131 not exc.__suppress_context__ and
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 75292
diff changeset
132 context not in seen):
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 75292
diff changeset
133 its.append(_iter_chain(context, None, seen))
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 75292
diff changeset
134 its.append([(_context_message, None)])
48474
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
135 its.append([(exc, custom_tb or exc.__traceback__)])
49112
a90b285458ab Issue #3812: Failed to build python if configure --without-threads.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48474
diff changeset
136 # itertools.chain is in an extension module and may be unavailable
a90b285458ab Issue #3812: Failed to build python if configure --without-threads.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48474
diff changeset
137 for it in its:
79378
fb90e2ff95b7 utilize yield from
Philip Jenvey <pjenvey@underboss.org>
parents: 76942
diff changeset
138 yield from it
48474
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
139
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
140 def _format_exception_iter(etype, value, tb, limit, chain):
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
141 if chain:
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
142 values = _iter_chain(value, tb)
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
143 else:
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
144 values = [(value, tb)]
48474
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
145
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
146 for value, tb in values:
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
147 if isinstance(value, str):
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
148 # This is a cause/context message line
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
149 yield value + '\n'
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
150 continue
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
151 if tb:
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
152 yield 'Traceback (most recent call last):\n'
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
153 yield from _format_list_iter(_extract_tb_iter(tb, limit=limit))
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
154 yield from _format_exception_only_iter(type(value), value)
48474
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
155
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
156 def print_exception(etype, value, tb, limit=None, file=None, chain=True):
17113
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
157 """Print exception up to 'limit' stack trace entries from 'tb' to 'file'.
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
158
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
159 This differs from print_tb() in the following ways: (1) if
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
160 traceback is not None, it prints a header "Traceback (most recent
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
161 call last):"; (2) it prints the exception type and value after the
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
162 stack trace; (3) if type is SyntaxError and value has the
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
163 appropriate format, it prints the line where the syntax error
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
164 occurred with a caret on the next line indicating the approximate
17113
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
165 position of the error.
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
166 """
23628
ea979c5206b7 Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents: 18067
diff changeset
167 if file is None:
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
168 file = sys.stderr
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
169 for line in _format_exception_iter(etype, value, tb, limit, chain):
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
170 print(line, file=file, end="")
2901
57ec24e0f35a added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents: 2440
diff changeset
171
48474
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
172 def format_exception(etype, value, tb, limit=None, chain=True):
17113
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
173 """Format a stack trace and the exception information.
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
174
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
175 The arguments have the same meaning as the corresponding arguments
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
176 to print_exception(). The return value is a list of strings, each
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
177 ending in a newline and some containing internal newlines. When
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
178 these lines are concatenated and printed, exactly the same text is
17113
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
179 printed as does print_exception().
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
180 """
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
181 return list(_format_exception_iter(etype, value, tb, limit, chain))
2901
57ec24e0f35a added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents: 2440
diff changeset
182
57ec24e0f35a added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents: 2440
diff changeset
183 def format_exception_only(etype, value):
17113
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
184 """Format the exception part of a traceback.
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
185
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
186 The arguments are the exception type and value such as given by
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
187 sys.last_type and sys.last_value. The return value is a list of
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
188 strings, each ending in a newline.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
189
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
190 Normally, the list contains a single string; however, for
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
191 SyntaxError exceptions, it contains several lines that (when
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
192 printed) display detailed information about where the syntax
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
193 error occurred.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
194
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
195 The message indicating which exception occurred is always the last
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
196 string in the list.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
197
17113
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
198 """
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
199 return list(_format_exception_only_iter(etype, value))
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
200
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
201 def _format_exception_only_iter(etype, value):
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39690
diff changeset
202 # Gracefully handle (the way Python 2.4 and earlier did) the case of
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39690
diff changeset
203 # being called with (None, None).
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39690
diff changeset
204 if etype is None:
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
205 yield _format_final_exc_line(etype, value)
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
206 return
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39690
diff changeset
207
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
208 stype = etype.__name__
39690
0cb9e57ec406 Inspired by SF patch #860326, make the exception formatting by
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
209 smod = etype.__module__
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 42455
diff changeset
210 if smod not in ("__main__", "builtins"):
39690
0cb9e57ec406 Inspired by SF patch #860326, make the exception formatting by
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
211 stype = smod + '.' + stype
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
212
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
213 if not issubclass(etype, SyntaxError):
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
214 yield _format_final_exc_line(stype, value)
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
215 return
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
216
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
217 # It was a syntax error; show exactly where the problem was found.
42455
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40959
diff changeset
218 filename = value.filename or "<string>"
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40959
diff changeset
219 lineno = str(value.lineno) or '?'
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
220 yield ' File "{}", line {}\n'.format(filename, lineno)
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
221
42455
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40959
diff changeset
222 badline = value.text
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40959
diff changeset
223 offset = value.offset
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40959
diff changeset
224 if badline is not None:
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
225 yield ' {}\n'.format(badline.strip())
42455
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40959
diff changeset
226 if offset is not None:
54571
e9b0db553d01 Merged revisions 73186,73206,73212 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52877
diff changeset
227 caretspace = badline.rstrip('\n')[:offset].lstrip()
42455
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40959
diff changeset
228 # non-space whitespace (likes tabs) must be kept for alignment
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40959
diff changeset
229 caretspace = ((c.isspace() and c or ' ') for c in caretspace)
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40959
diff changeset
230 # only three spaces to account for offset1 == pos 0
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
231 yield ' {}^\n'.format(''.join(caretspace))
42455
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40959
diff changeset
232 msg = value.msg or "<no detail available>"
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
233 yield "{}: {}\n".format(stype, msg)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
234
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
235 def _format_final_exc_line(etype, value):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
236 valuestr = _some_str(value)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
237 if value is None or not valuestr:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
238 line = "%s\n" % etype
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
239 else:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
240 line = "%s: %s\n" % (etype, valuestr)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
241 return line
2901
57ec24e0f35a added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents: 2440
diff changeset
242
14116
92980920d5ed Patch by Toby Dickenson: don't die when an error occurs during string
Guido van Rossum <guido@python.org>
parents: 13620
diff changeset
243 def _some_str(value):
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
244 try:
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
245 return str(value)
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
246 except:
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
247 return '<unprintable %s object>' % type(value).__name__
14116
92980920d5ed Patch by Toby Dickenson: don't die when an error occurs during string
Guido van Rossum <guido@python.org>
parents: 13620
diff changeset
248
48474
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
249 def print_exc(limit=None, file=None, chain=True):
37042
7ada16a14968 Remove sys.exc_type, sys.exc_value, sys.exc_traceback
Neal Norwitz <nnorwitz@gmail.com>
parents: 36757
diff changeset
250 """Shorthand for 'print_exception(*sys.exc_info(), limit, file)'."""
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
251 print_exception(*sys.exc_info(), limit=limit, file=file, chain=chain)
30862
d45f8981fdea Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24844
diff changeset
252
48474
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
253 def format_exc(limit=None, chain=True):
30862
d45f8981fdea Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24844
diff changeset
254 """Like print_exc() but return a string."""
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
255 return "".join(format_exception(*sys.exc_info(), limit=limit, chain=chain))
30862
d45f8981fdea Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24844
diff changeset
256
48474
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
257 def print_last(limit=None, file=None, chain=True):
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
258 """This is a shorthand for 'print_exception(sys.last_type,
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
259 sys.last_value, sys.last_traceback, limit, file)'."""
52389
d4e7470bd660 Merged revisions 70554,70588-70589,70598,70605,70611-70621,70623-70624,70626-70627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50540
diff changeset
260 if not hasattr(sys, "last_type"):
d4e7470bd660 Merged revisions 70554,70588-70589,70598,70605,70611-70621,70623-70624,70626-70627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50540
diff changeset
261 raise ValueError("no last exception")
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
262 print_exception(sys.last_type, sys.last_value, sys.last_traceback,
48474
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 46962
diff changeset
263 limit, file, chain)
4004
d3a1f6088c28 Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents: 3640
diff changeset
264
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
265 #
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
266 # Printing and Extracting Stacks.
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
267 #
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
268
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
269 def _extract_stack_iter(f, limit=None):
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
270 return _extract_tb_or_stack_iter(
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
271 f, limit, lambda f: (f, f.f_lineno, f.f_back))
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
272
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
273 def _get_stack(f):
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
274 if f is None:
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
275 f = sys._getframe().f_back.f_back
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
276 return f
4004
d3a1f6088c28 Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents: 3640
diff changeset
277
d3a1f6088c28 Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents: 3640
diff changeset
278 def print_stack(f=None, limit=None, file=None):
17113
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
279 """Print a stack trace from its invocation point.
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17116
diff changeset
280
17113
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
281 The optional 'f' argument can be used to specify an alternate
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
282 stack frame at which to start. The optional 'limit' and 'file'
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
283 arguments have the same meaning as for print_exception().
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
284 """
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
285 print_list(extract_stack(_get_stack(f), limit=limit), file=file)
4004
d3a1f6088c28 Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents: 3640
diff changeset
286
d3a1f6088c28 Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents: 3640
diff changeset
287 def format_stack(f=None, limit=None):
17113
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
288 """Shorthand for 'format_list(extract_stack(f, limit))'."""
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
289 return format_list(extract_stack(_get_stack(f), limit=limit))
4004
d3a1f6088c28 Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents: 3640
diff changeset
290
56002
fa91270b6087 Merged revisions 74821,74828-74831,74833,74835 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54571
diff changeset
291 def extract_stack(f=None, limit=None):
17113
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
292 """Extract the raw traceback from the current stack frame.
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
293
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
294 The return value has the same format as for extract_tb(). The
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
295 optional 'f' and 'limit' arguments have the same meaning as for
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
296 print_stack(). Each item in the list is a quadruple (filename,
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
297 line number, function name, text), and the entries are in order
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
298 from oldest to newest stack frame.
4863b595bdae Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16889
diff changeset
299 """
83553
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
300 stack = list(_extract_stack_iter(_get_stack(f), limit=limit))
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
301 stack.reverse()
84cef4f1999a refactor traceback.py to reduce code duplication (closes #17646)
Benjamin Peterson <benjamin@python.org>
parents: 79378
diff changeset
302 return stack
85723
100606ef02cf #1565525: Add traceback.clear_frames() helper function to clear locals ref'd by a traceback
Andrew Kuchling <amk@amk.ca>
parents: 83553
diff changeset
303
100606ef02cf #1565525: Add traceback.clear_frames() helper function to clear locals ref'd by a traceback
Andrew Kuchling <amk@amk.ca>
parents: 83553
diff changeset
304 def clear_frames(tb):
100606ef02cf #1565525: Add traceback.clear_frames() helper function to clear locals ref'd by a traceback
Andrew Kuchling <amk@amk.ca>
parents: 83553
diff changeset
305 "Clear all references to local variables in the frames of a traceback."
100606ef02cf #1565525: Add traceback.clear_frames() helper function to clear locals ref'd by a traceback
Andrew Kuchling <amk@amk.ca>
parents: 83553
diff changeset
306 while tb is not None:
15873
3dd217fbdd59 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15848
diff changeset
307 try:
85723
100606ef02cf #1565525: Add traceback.clear_frames() helper function to clear locals ref'd by a traceback
Andrew Kuchling <amk@amk.ca>
parents: 83553
diff changeset
308 tb.tb_frame.clear()
100606ef02cf #1565525: Add traceback.clear_frames() helper function to clear locals ref'd by a traceback
Andrew Kuchling <amk@amk.ca>
parents: 83553
diff changeset
309 except RuntimeError:
100606ef02cf #1565525: Add traceback.clear_frames() helper function to clear locals ref'd by a traceback
Andrew Kuchling <amk@amk.ca>
parents: 83553
diff changeset
310 # Ignore the exception raised if the frame is still executing.
100606ef02cf #1565525: Add traceback.clear_frames() helper function to clear locals ref'd by a traceback
Andrew Kuchling <amk@amk.ca>
parents: 83553
diff changeset
311 pass
100606ef02cf #1565525: Add traceback.clear_frames() helper function to clear locals ref'd by a traceback
Andrew Kuchling <amk@amk.ca>
parents: 83553
diff changeset
312 tb = tb.tb_next