Mercurial > cpython-lru_cache-weakref
annotate Lib/traceback.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | b36cb4602e21 |
| children | 8c2635afbfe1 |
| 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 |
|
2440
d577d749918d
handle class exceptions
Guido van Rossum <guido@python.org>
parents:
1810
diff
changeset
|
5 import types |
|
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', |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
10 'print_last', 'print_stack', 'print_tb', 'tb_lineno'] |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16548
diff
changeset
|
11 |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
12 def _print(file, str='', terminator='\n'): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
13 file.write(str+terminator) |
|
4004
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
14 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
15 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
16 def print_list(extracted_list, file=None): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
17 """Print the list of tuples as returned by extract_tb() or |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
18 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
|
19 if file is None: |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
20 file = sys.stderr |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
21 for filename, lineno, name, line in extracted_list: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
22 _print(file, |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
23 ' File "%s", line %d, in %s' % (filename,lineno,name)) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
24 if line: |
|
16499
58de2829cfdd
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15873
diff
changeset
|
25 _print(file, ' %s' % line.strip()) |
|
4004
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
26 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
27 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
|
28 """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
|
29 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
30 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 """ |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
37 list = [] |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
38 for filename, lineno, name, line in extracted_list: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
39 item = ' File "%s", line %d, in %s\n' % (filename,lineno,name) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
40 if line: |
|
16499
58de2829cfdd
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15873
diff
changeset
|
41 item = item + ' %s\n' % line.strip() |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
42 list.append(item) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
43 return list |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
44 |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
45 |
|
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
46 def print_tb(tb, limit=None, file=None): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
47 """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
|
48 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
49 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
|
50 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
|
51 '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
|
52 method. |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
53 """ |
|
23628
ea979c5206b7
Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents:
18067
diff
changeset
|
54 if file is None: |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
55 file = sys.stderr |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
56 if limit is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
57 if hasattr(sys, 'tracebacklimit'): |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
58 limit = sys.tracebacklimit |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
59 n = 0 |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
60 while tb is not None and (limit is None or n < limit): |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
61 f = tb.tb_frame |
| 24844 | 62 lineno = tb.tb_lineno |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
63 co = f.f_code |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
64 filename = co.co_filename |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
65 name = co.co_name |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
66 _print(file, |
|
52875
976b305aadd2
#1326077: fix traceback formatting of SyntaxErrors. This fixes two differences with formatting coming from Python: a) the reproduction of location details in the error message if no line text is given, b) the prefixing of the last line by one space.
Georg Brandl <georg@python.org>
parents:
52296
diff
changeset
|
67 ' File "%s", line %d, in %s' % (filename, lineno, name)) |
|
33839
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31389
diff
changeset
|
68 linecache.checkcache(filename) |
|
37576
d11366f7e1e7
Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents:
37334
diff
changeset
|
69 line = linecache.getline(filename, lineno, f.f_globals) |
|
16499
58de2829cfdd
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15873
diff
changeset
|
70 if line: _print(file, ' ' + line.strip()) |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
71 tb = tb.tb_next |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
72 n = n+1 |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
74 def format_tb(tb, limit = None): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
75 """A shorthand for 'format_list(extract_stack(f, limit)).""" |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
76 return format_list(extract_tb(tb, limit)) |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
77 |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 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
|
79 """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
|
80 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
81 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
|
82 '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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 available it is None. |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
88 """ |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
89 if limit is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
90 if hasattr(sys, 'tracebacklimit'): |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
91 limit = sys.tracebacklimit |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
92 list = [] |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
93 n = 0 |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
94 while tb is not None and (limit is None or n < limit): |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
95 f = tb.tb_frame |
| 24844 | 96 lineno = tb.tb_lineno |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
97 co = f.f_code |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
98 filename = co.co_filename |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
99 name = co.co_name |
|
33839
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31389
diff
changeset
|
100 linecache.checkcache(filename) |
|
37576
d11366f7e1e7
Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents:
37334
diff
changeset
|
101 line = linecache.getline(filename, lineno, f.f_globals) |
|
16499
58de2829cfdd
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15873
diff
changeset
|
102 if line: line = line.strip() |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
103 else: line = None |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
104 list.append((filename, lineno, name, line)) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
105 tb = tb.tb_next |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
106 n = n+1 |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
107 return list |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
109 |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
110 def print_exception(etype, value, tb, 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
|
111 """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
|
112 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
113 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
|
114 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
|
115 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
|
116 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
|
117 appropriate format, it prints the line where the syntax error |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
118 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
|
119 position of the error. |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
120 """ |
|
23628
ea979c5206b7
Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents:
18067
diff
changeset
|
121 if file is None: |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
122 file = sys.stderr |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
123 if tb: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
124 _print(file, 'Traceback (most recent call last):') |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
125 print_tb(tb, limit, file) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
126 lines = format_exception_only(etype, value) |
|
52875
976b305aadd2
#1326077: fix traceback formatting of SyntaxErrors. This fixes two differences with formatting coming from Python: a) the reproduction of location details in the error message if no line text is given, b) the prefixing of the last line by one space.
Georg Brandl <georg@python.org>
parents:
52296
diff
changeset
|
127 for line in lines: |
|
976b305aadd2
#1326077: fix traceback formatting of SyntaxErrors. This fixes two differences with formatting coming from Python: a) the reproduction of location details in the error message if no line text is given, b) the prefixing of the last line by one space.
Georg Brandl <georg@python.org>
parents:
52296
diff
changeset
|
128 _print(file, line, '') |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
129 |
|
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
130 def format_exception(etype, value, tb, limit = None): |
|
17113
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
131 """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
|
132 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
133 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
|
134 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
|
135 ending in a newline and some containing internal newlines. When |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
136 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
|
137 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
|
138 """ |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
139 if tb: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
140 list = ['Traceback (most recent call last):\n'] |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
141 list = list + format_tb(tb, limit) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
142 else: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
143 list = [] |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
144 list = list + format_exception_only(etype, value) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
145 return list |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
146 |
|
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
147 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
|
148 """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
|
149 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
150 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
|
151 sys.last_type and sys.last_value. The return value is a list of |
|
39163
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
152 strings, each ending in a newline. |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
153 |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
154 Normally, the list contains a single string; however, for |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
155 SyntaxError exceptions, it contains several lines that (when |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
156 printed) display detailed information about where the syntax |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
157 error occurred. |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
158 |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
159 The message indicating which exception occurred is always the last |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
160 string in the list. |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
161 |
|
17113
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
162 """ |
|
39163
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
163 |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
164 # An instance should not have a meaningful value parameter, but |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
165 # sometimes does, particularly for string exceptions, such as |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
166 # >>> raise string1, string2 # deprecated |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
167 # |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
168 # Clear these out first because issubtype(string1, SyntaxError) |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
169 # would throw another exception and mask the original problem. |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
170 if (isinstance(etype, BaseException) or |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
171 isinstance(etype, types.InstanceType) or |
|
39883
ecaafc32c500
Fix a bug in traceback.format_exception_only() that led to an error
Georg Brandl <georg@python.org>
parents:
39372
diff
changeset
|
172 etype is None or type(etype) is str): |
|
39163
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
173 return [_format_final_exc_line(etype, value)] |
|
39167
07cee01a4fcc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39166
diff
changeset
|
174 |
|
39163
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
175 stype = etype.__name__ |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
176 |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
177 if not issubclass(etype, SyntaxError): |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
178 return [_format_final_exc_line(stype, value)] |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
179 |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
180 # It was a syntax error; show exactly where the problem was found. |
|
39166
eaedccc0283d
Repair accidental NameError.
Georg Brandl <georg@python.org>
parents:
39163
diff
changeset
|
181 lines = [] |
|
39163
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
182 try: |
|
48118
c59e90d8d1c7
Fix a DeprecationWarning about __getitem__() and exceptions in the 'traceback' module.
Brett Cannon <bcannon@gmail.com>
parents:
39883
diff
changeset
|
183 msg, (filename, lineno, offset, badline) = value.args |
|
39163
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
184 except Exception: |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
185 pass |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
186 else: |
|
39163
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
187 filename = filename or "<string>" |
|
39166
eaedccc0283d
Repair accidental NameError.
Georg Brandl <georg@python.org>
parents:
39163
diff
changeset
|
188 lines.append(' File "%s", line %d\n' % (filename, lineno)) |
|
39163
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
189 if badline is not None: |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
190 lines.append(' %s\n' % badline.strip()) |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
191 if offset is not None: |
|
56830
97d7cd365dd5
Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52953
diff
changeset
|
192 caretspace = badline.rstrip('\n')[:offset].lstrip() |
|
39163
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
193 # non-space whitespace (likes tabs) must be kept for alignment |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
194 caretspace = ((c.isspace() and c or ' ') for c in caretspace) |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
195 # only three spaces to account for offset1 == pos 0 |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
196 lines.append(' %s^\n' % ''.join(caretspace)) |
|
52875
976b305aadd2
#1326077: fix traceback formatting of SyntaxErrors. This fixes two differences with formatting coming from Python: a) the reproduction of location details in the error message if no line text is given, b) the prefixing of the last line by one space.
Georg Brandl <georg@python.org>
parents:
52296
diff
changeset
|
197 value = msg |
|
39167
07cee01a4fcc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39166
diff
changeset
|
198 |
|
39163
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
199 lines.append(_format_final_exc_line(stype, value)) |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
200 return lines |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
201 |
|
39163
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
202 def _format_final_exc_line(etype, value): |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
203 """Return a list of a single line -- normal case for format_exception_only""" |
|
39372
0238166f07b7
Better fix for bug #1531405, not executing str(value) twice.
Georg Brandl <georg@python.org>
parents:
39362
diff
changeset
|
204 valuestr = _some_str(value) |
|
0238166f07b7
Better fix for bug #1531405, not executing str(value) twice.
Georg Brandl <georg@python.org>
parents:
39362
diff
changeset
|
205 if value is None or not valuestr: |
|
39163
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
206 line = "%s\n" % etype |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
207 else: |
|
39372
0238166f07b7
Better fix for bug #1531405, not executing str(value) twice.
Georg Brandl <georg@python.org>
parents:
39362
diff
changeset
|
208 line = "%s: %s\n" % (etype, valuestr) |
|
39163
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
209 return line |
|
39167
07cee01a4fcc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39166
diff
changeset
|
210 |
|
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
|
211 def _some_str(value): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
212 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
213 return str(value) |
|
61234
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
214 except Exception: |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
215 pass |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
216 try: |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
217 value = unicode(value) |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
218 return value.encode("ascii", "backslashreplace") |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
219 except Exception: |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
220 pass |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
221 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
|
222 |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
224 def print_exc(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
|
225 """Shorthand for 'print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file)'. |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
226 (In fact, it uses sys.exc_info() to retrieve the same information |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
227 in a thread-safe way.)""" |
|
23628
ea979c5206b7
Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents:
18067
diff
changeset
|
228 if file is None: |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
229 file = sys.stderr |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
230 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
231 etype, value, tb = sys.exc_info() |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
232 print_exception(etype, value, tb, limit, file) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
233 finally: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
234 etype = value = tb = None |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 |
|
30862
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
236 |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
237 def format_exc(limit=None): |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
238 """Like print_exc() but return a string.""" |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
239 try: |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
240 etype, value, tb = sys.exc_info() |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
241 return ''.join(format_exception(etype, value, tb, limit)) |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
242 finally: |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
243 etype = value = tb = None |
|
31389
aca1ce1b4b61
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
30862
diff
changeset
|
244 |
|
30862
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
245 |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
246 def print_last(limit=None, file=None): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
247 """This is a shorthand for 'print_exception(sys.last_type, |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
248 sys.last_value, sys.last_traceback, limit, file)'.""" |
|
52296
dca3d672758e
complain when there's no last exception
Benjamin Peterson <benjamin@python.org>
parents:
52295
diff
changeset
|
249 if not hasattr(sys, "last_type"): |
|
dca3d672758e
complain when there's no last exception
Benjamin Peterson <benjamin@python.org>
parents:
52295
diff
changeset
|
250 raise ValueError("no last exception") |
|
23628
ea979c5206b7
Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents:
18067
diff
changeset
|
251 if file is None: |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
252 file = sys.stderr |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
253 print_exception(sys.last_type, sys.last_value, sys.last_traceback, |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
254 limit, file) |
|
4004
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
255 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
256 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
257 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
|
258 """Print a stack trace from its invocation point. |
|
17245
0377f927cafa
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17116
diff
changeset
|
259 |
|
17113
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
260 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
|
261 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
|
262 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
|
263 """ |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
264 if f is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
265 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
266 raise ZeroDivisionError |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
267 except ZeroDivisionError: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
268 f = sys.exc_info()[2].tb_frame.f_back |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
269 print_list(extract_stack(f, limit), file) |
|
4004
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
270 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
271 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
|
272 """Shorthand for 'format_list(extract_stack(f, limit))'.""" |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
273 if f is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
274 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
275 raise ZeroDivisionError |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
276 except ZeroDivisionError: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
277 f = sys.exc_info()[2].tb_frame.f_back |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
278 return format_list(extract_stack(f, limit)) |
|
4004
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
279 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
280 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
|
281 """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
|
282 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
283 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
|
284 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
|
285 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
|
286 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
|
287 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
|
288 """ |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
289 if f is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
290 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
291 raise ZeroDivisionError |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
292 except ZeroDivisionError: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
293 f = sys.exc_info()[2].tb_frame.f_back |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
294 if limit is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
295 if hasattr(sys, 'tracebacklimit'): |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
296 limit = sys.tracebacklimit |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
297 list = [] |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
298 n = 0 |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
299 while f is not None and (limit is None or n < limit): |
| 24844 | 300 lineno = f.f_lineno |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
301 co = f.f_code |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
302 filename = co.co_filename |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
303 name = co.co_name |
|
33839
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31389
diff
changeset
|
304 linecache.checkcache(filename) |
|
37576
d11366f7e1e7
Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents:
37334
diff
changeset
|
305 line = linecache.getline(filename, lineno, f.f_globals) |
|
16499
58de2829cfdd
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15873
diff
changeset
|
306 if line: line = line.strip() |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
307 else: line = None |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
308 list.append((filename, lineno, name, line)) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
309 f = f.f_back |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
310 n = n+1 |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
311 list.reverse() |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
312 return list |
|
6337
9e429518cb20
Use Marc Lemburg's tb_lineno() to calculate the correct line number.
Guido van Rossum <guido@python.org>
parents:
5832
diff
changeset
|
313 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
314 def tb_lineno(tb): |
|
17113
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
315 """Calculate correct line number of traceback given in tb. |
|
6337
9e429518cb20
Use Marc Lemburg's tb_lineno() to calculate the correct line number.
Guido van Rossum <guido@python.org>
parents:
5832
diff
changeset
|
316 |
| 24844 | 317 Obsolete in 2.3. |
|
17113
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
318 """ |
| 24844 | 319 return tb.tb_lineno |
