Mercurial > cpython-lru_cache-weakref
annotate Lib/traceback.py @ 39042:c02fa187e0f4 legacy-trunk
Part of SF patch #1484695. This removes dead code. The chksum was
already verified in .frombuf() on the lines above. If there was
a problem an exception is raised, so there was no way this condition
could have been true.
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Mon, 10 Jul 2006 00:23:17 +0000 |
| parents | bb3e0c889632 |
| children | 89e090fef985 |
| 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, |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
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) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
127 for line in lines[:-1]: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
128 _print(file, line, ' ') |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
129 _print(file, lines[-1], '') |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
130 |
|
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
131 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
|
132 """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
|
133 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
134 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
|
135 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
|
136 ending in a newline and some containing internal newlines. When |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
137 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
|
138 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
|
139 """ |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
140 if tb: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
141 list = ['Traceback (most recent call last):\n'] |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
142 list = list + format_tb(tb, limit) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
143 else: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
144 list = [] |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
145 list = list + format_exception_only(etype, value) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
146 return list |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
147 |
|
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
148 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
|
149 """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
|
150 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
151 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
|
152 sys.last_type and sys.last_value. The return value is a list of |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
153 strings, each ending in a newline. Normally, the list contains a |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
154 single string; however, for SyntaxError exceptions, it contains |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
155 several lines that (when printed) display detailed information |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
156 about where the syntax error occurred. The message indicating |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
157 which exception occurred is the always last string in the list. |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
158 """ |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
159 list = [] |
|
36751
8e04de2480e4
PEP 352 implementation. Creates a new base class, BaseException, which has an
Brett Cannon <bcannon@gmail.com>
parents:
33839
diff
changeset
|
160 if (type(etype) == types.ClassType |
|
38083
bb3e0c889632
Fix the formatting of KeyboardInterrupt -- a bad issubclass() call.
Guido van Rossum <guido@python.org>
parents:
37652
diff
changeset
|
161 or (isinstance(etype, type) and issubclass(etype, BaseException))): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
162 stype = etype.__name__ |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
163 else: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
164 stype = etype |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
165 if value is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
166 list.append(str(stype) + '\n') |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
167 else: |
|
37334
a4950ea556b5
traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents:
37199
diff
changeset
|
168 if issubclass(etype, SyntaxError): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
169 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
170 msg, (filename, lineno, offset, line) = value |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
171 except: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
172 pass |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
173 else: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
174 if not filename: filename = "<string>" |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
175 list.append(' File "%s", line %d\n' % |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
176 (filename, lineno)) |
|
18067
10992198c9d6
SF bug 431772: traceback.print_exc() causes traceback
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
177 if line is not None: |
|
10992198c9d6
SF bug 431772: traceback.print_exc() causes traceback
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
178 i = 0 |
|
10992198c9d6
SF bug 431772: traceback.print_exc() causes traceback
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
179 while i < len(line) and line[i].isspace(): |
|
10992198c9d6
SF bug 431772: traceback.print_exc() causes traceback
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
180 i = i+1 |
|
10992198c9d6
SF bug 431772: traceback.print_exc() causes traceback
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
181 list.append(' %s\n' % line.strip()) |
|
10992198c9d6
SF bug 431772: traceback.print_exc() causes traceback
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
182 if offset is not None: |
|
10992198c9d6
SF bug 431772: traceback.print_exc() causes traceback
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
183 s = ' ' |
|
10992198c9d6
SF bug 431772: traceback.print_exc() causes traceback
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
184 for c in line[i:offset-1]: |
|
10992198c9d6
SF bug 431772: traceback.print_exc() causes traceback
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
185 if c.isspace(): |
|
10992198c9d6
SF bug 431772: traceback.print_exc() causes traceback
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
186 s = s + c |
|
10992198c9d6
SF bug 431772: traceback.print_exc() causes traceback
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
187 else: |
|
10992198c9d6
SF bug 431772: traceback.print_exc() causes traceback
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
188 s = s + ' ' |
|
10992198c9d6
SF bug 431772: traceback.print_exc() causes traceback
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
189 list.append('%s^\n' % s) |
|
10992198c9d6
SF bug 431772: traceback.print_exc() causes traceback
Tim Peters <tim.peters@gmail.com>
parents:
17245
diff
changeset
|
190 value = msg |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
191 s = _some_str(value) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
192 if s: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
193 list.append('%s: %s\n' % (str(stype), s)) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
194 else: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
195 list.append('%s\n' % str(stype)) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
196 return list |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
197 |
|
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
|
198 def _some_str(value): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
199 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
200 return str(value) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
201 except: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
202 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
|
203 |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
205 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
|
206 """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
|
207 (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
|
208 in a thread-safe way.)""" |
|
23628
ea979c5206b7
Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents:
18067
diff
changeset
|
209 if file is None: |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
210 file = sys.stderr |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
211 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
212 etype, value, tb = sys.exc_info() |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
213 print_exception(etype, value, tb, limit, file) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
214 finally: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
215 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
|
216 |
|
30862
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
217 |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
218 def format_exc(limit=None): |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
219 """Like print_exc() but return a string.""" |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
220 try: |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
221 etype, value, tb = sys.exc_info() |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
222 return ''.join(format_exception(etype, value, tb, limit)) |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
223 finally: |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
224 etype = value = tb = None |
|
31389
aca1ce1b4b61
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
30862
diff
changeset
|
225 |
|
30862
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
226 |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
227 def print_last(limit=None, file=None): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
228 """This is a shorthand for 'print_exception(sys.last_type, |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
229 sys.last_value, sys.last_traceback, limit, file)'.""" |
|
23628
ea979c5206b7
Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents:
18067
diff
changeset
|
230 if file is None: |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
231 file = sys.stderr |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
232 print_exception(sys.last_type, sys.last_value, sys.last_traceback, |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
233 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
|
234 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
235 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
236 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
|
237 """Print a stack trace from its invocation point. |
|
17245
0377f927cafa
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17116
diff
changeset
|
238 |
|
17113
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
239 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
|
240 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
|
241 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
|
242 """ |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
243 if f is None: |
|
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 raise ZeroDivisionError |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
246 except ZeroDivisionError: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
247 f = sys.exc_info()[2].tb_frame.f_back |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
248 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
|
249 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
250 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
|
251 """Shorthand for 'format_list(extract_stack(f, limit))'.""" |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
252 if f is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
253 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
254 raise ZeroDivisionError |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
255 except ZeroDivisionError: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
256 f = sys.exc_info()[2].tb_frame.f_back |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
257 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
|
258 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
259 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
|
260 """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
|
261 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
262 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
|
263 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
|
264 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
|
265 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
|
266 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
|
267 """ |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
268 if f is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
269 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
270 raise ZeroDivisionError |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
271 except ZeroDivisionError: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
272 f = sys.exc_info()[2].tb_frame.f_back |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
273 if limit is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
274 if hasattr(sys, 'tracebacklimit'): |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
275 limit = sys.tracebacklimit |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
276 list = [] |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
277 n = 0 |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
278 while f is not None and (limit is None or n < limit): |
| 24844 | 279 lineno = f.f_lineno |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
280 co = f.f_code |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
281 filename = co.co_filename |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
282 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
|
283 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
|
284 line = linecache.getline(filename, lineno, f.f_globals) |
|
16499
58de2829cfdd
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15873
diff
changeset
|
285 if line: line = line.strip() |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
286 else: line = None |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
287 list.append((filename, lineno, name, line)) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
288 f = f.f_back |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
289 n = n+1 |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
290 list.reverse() |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
291 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
|
292 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
293 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
|
294 """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
|
295 |
| 24844 | 296 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
|
297 """ |
| 24844 | 298 return tb.tb_lineno |
