Mercurial > cpython-lru_cache-weakref
annotate Lib/traceback.py @ 84007:dbfbdf2b5c19 3.3
Fix #17967: For ftp urls CWD to target instead of hopping to each directory
towards target. This fixes a bug where target is accessible, but parent
directories are restricted.
| author | Senthil Kumaran <senthil@uthcode.com> |
|---|---|
| date | Sat, 01 Jun 2013 08:27:06 -0700 |
| parents | b0eb7d2a9542 |
| children | dcded8c7af89 fb90e2ff95b7 |
| 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 |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16548
diff
changeset
|
6 __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
|
7 'format_exception_only', 'format_list', 'format_stack', |
|
30862
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
8 'format_tb', 'print_exc', 'format_exc', 'print_exception', |
|
46962
894144b173c9
Remove function obsolete since Python 2.3 from traceback module.
Georg Brandl <georg@python.org>
parents:
44098
diff
changeset
|
9 'print_last', 'print_stack', 'print_tb'] |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16548
diff
changeset
|
10 |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
11 def _print(file, str='', terminator='\n'): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
12 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
|
13 |
|
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 def print_list(extracted_list, file=None): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
16 """Print the list of tuples as returned by extract_tb() or |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
17 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
|
18 if file is None: |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
19 file = sys.stderr |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
20 for filename, lineno, name, line in extracted_list: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
21 _print(file, |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
22 ' File "%s", line %d, in %s' % (filename,lineno,name)) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
23 if line: |
|
16499
58de2829cfdd
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15873
diff
changeset
|
24 _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
|
25 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
26 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
|
27 """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
|
28 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
29 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 """ |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
36 list = [] |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
37 for filename, lineno, name, line in extracted_list: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
38 item = ' File "%s", line %d, in %s\n' % (filename,lineno,name) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
39 if line: |
|
16499
58de2829cfdd
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15873
diff
changeset
|
40 item = item + ' %s\n' % line.strip() |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
41 list.append(item) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
42 return list |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
43 |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
44 |
|
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
45 def print_tb(tb, limit=None, file=None): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
46 """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
|
47 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
48 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
|
49 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
|
50 '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
|
51 method. |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
52 """ |
|
23628
ea979c5206b7
Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents:
18067
diff
changeset
|
53 if file is None: |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
54 file = sys.stderr |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
55 if limit is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
56 if hasattr(sys, 'tracebacklimit'): |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
57 limit = sys.tracebacklimit |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
58 n = 0 |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
59 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
|
60 f = tb.tb_frame |
| 24844 | 61 lineno = tb.tb_lineno |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
62 co = f.f_code |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
63 filename = co.co_filename |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
64 name = co.co_name |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
65 _print(file, |
|
52877
37d6af6f85aa
Merged revisions 71237-71238 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52389
diff
changeset
|
66 ' 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
|
67 linecache.checkcache(filename) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37042
diff
changeset
|
68 line = linecache.getline(filename, lineno, f.f_globals) |
|
16499
58de2829cfdd
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15873
diff
changeset
|
69 if line: _print(file, ' ' + line.strip()) |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
70 tb = tb.tb_next |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
71 n = n+1 |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 |
|
56002
fa91270b6087
Merged revisions 74821,74828-74831,74833,74835 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54571
diff
changeset
|
73 def format_tb(tb, limit=None): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
74 """A shorthand for 'format_list(extract_stack(f, limit)).""" |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
75 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
|
76 |
|
56002
fa91270b6087
Merged revisions 74821,74828-74831,74833,74835 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54571
diff
changeset
|
77 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
|
78 """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
|
79 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
80 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
|
81 '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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 available it is None. |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
87 """ |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
88 if limit is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
89 if hasattr(sys, 'tracebacklimit'): |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
90 limit = sys.tracebacklimit |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
91 list = [] |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
92 n = 0 |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
93 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
|
94 f = tb.tb_frame |
| 24844 | 95 lineno = tb.tb_lineno |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
96 co = f.f_code |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
97 filename = co.co_filename |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
98 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
|
99 linecache.checkcache(filename) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37042
diff
changeset
|
100 line = linecache.getline(filename, lineno, f.f_globals) |
|
16499
58de2829cfdd
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15873
diff
changeset
|
101 if line: line = line.strip() |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
102 else: line = None |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
103 list.append((filename, lineno, name, line)) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
104 tb = tb.tb_next |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
105 n = n+1 |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
106 return list |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
108 |
|
48474
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
109 _cause_message = ( |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
110 "\nThe above exception was the direct cause " |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
111 "of the following exception:\n") |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
112 |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
113 _context_message = ( |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
114 "\nDuring handling of the above exception, " |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
115 "another exception occurred:\n") |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
116 |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
117 def _iter_chain(exc, custom_tb=None, seen=None): |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
118 if seen is None: |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
119 seen = set() |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
120 seen.add(exc) |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
121 its = [] |
|
76942
b0eb7d2a9542
PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents:
75292
diff
changeset
|
122 context = exc.__context__ |
|
48474
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
123 cause = exc.__cause__ |
|
76942
b0eb7d2a9542
PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents:
75292
diff
changeset
|
124 if cause is not None and cause not in seen: |
|
75292
b299c4f31ff2
Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents:
57498
diff
changeset
|
125 its.append(_iter_chain(cause, False, seen)) |
|
b299c4f31ff2
Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents:
57498
diff
changeset
|
126 its.append([(_cause_message, None)]) |
|
76942
b0eb7d2a9542
PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents:
75292
diff
changeset
|
127 elif (context is not None and |
|
b0eb7d2a9542
PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents:
75292
diff
changeset
|
128 not exc.__suppress_context__ and |
|
b0eb7d2a9542
PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents:
75292
diff
changeset
|
129 context not in seen): |
|
b0eb7d2a9542
PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents:
75292
diff
changeset
|
130 its.append(_iter_chain(context, None, seen)) |
|
b0eb7d2a9542
PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents:
75292
diff
changeset
|
131 its.append([(_context_message, None)]) |
|
48474
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
132 its.append([(exc, custom_tb or exc.__traceback__)]) |
|
49112
a90b285458ab
Issue #3812: Failed to build python if configure --without-threads.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48474
diff
changeset
|
133 # itertools.chain is in an extension module and may be unavailable |
|
a90b285458ab
Issue #3812: Failed to build python if configure --without-threads.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48474
diff
changeset
|
134 for it in its: |
|
a90b285458ab
Issue #3812: Failed to build python if configure --without-threads.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48474
diff
changeset
|
135 for x in it: |
|
a90b285458ab
Issue #3812: Failed to build python if configure --without-threads.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
48474
diff
changeset
|
136 yield x |
|
48474
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
137 |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
138 |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
139 def print_exception(etype, value, tb, limit=None, file=None, chain=True): |
|
17113
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
140 """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
|
141 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
142 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
|
143 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
|
144 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
|
145 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
|
146 appropriate format, it prints the line where the syntax error |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
147 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
|
148 position of the error. |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
149 """ |
|
23628
ea979c5206b7
Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents:
18067
diff
changeset
|
150 if file is None: |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
151 file = sys.stderr |
|
48474
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
152 if chain: |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
153 values = _iter_chain(value, tb) |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
154 else: |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
155 values = [(value, tb)] |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
156 for value, tb in values: |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
157 if isinstance(value, str): |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
158 _print(file, value) |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
159 continue |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
160 if tb: |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
161 _print(file, 'Traceback (most recent call last):') |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
162 print_tb(tb, limit, file) |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
163 lines = format_exception_only(type(value), value) |
|
52877
37d6af6f85aa
Merged revisions 71237-71238 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52389
diff
changeset
|
164 for line in lines: |
|
37d6af6f85aa
Merged revisions 71237-71238 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52389
diff
changeset
|
165 _print(file, line, '') |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
166 |
|
48474
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
167 def format_exception(etype, value, tb, limit=None, chain=True): |
|
17113
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
168 """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
|
169 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
170 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
|
171 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
|
172 ending in a newline and some containing internal newlines. When |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
173 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
|
174 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
|
175 """ |
|
48474
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
176 list = [] |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
177 if chain: |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
178 values = _iter_chain(value, tb) |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
179 else: |
|
48474
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
180 values = [(value, tb)] |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
181 for value, tb in values: |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
182 if isinstance(value, str): |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
183 list.append(value + '\n') |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
184 continue |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
185 if tb: |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
186 list.append('Traceback (most recent call last):\n') |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
187 list.extend(format_tb(tb, limit)) |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
188 list.extend(format_exception_only(type(value), value)) |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
189 return list |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
190 |
|
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
191 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
|
192 """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
|
193 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
194 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
|
195 sys.last_type and sys.last_value. The return value is a list of |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
196 strings, each ending in a newline. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
197 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
198 Normally, the list contains a single string; however, for |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
199 SyntaxError exceptions, it contains several lines that (when |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
200 printed) display detailed information about where the syntax |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
201 error occurred. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
202 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
203 The message indicating which exception occurred is always the last |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
204 string in the list. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
205 |
|
17113
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
206 """ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39690
diff
changeset
|
207 # Gracefully handle (the way Python 2.4 and earlier did) the case of |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39690
diff
changeset
|
208 # being called with (None, None). |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39690
diff
changeset
|
209 if etype is None: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39690
diff
changeset
|
210 return [_format_final_exc_line(etype, value)] |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39690
diff
changeset
|
211 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
212 stype = etype.__name__ |
|
39690
0cb9e57ec406
Inspired by SF patch #860326, make the exception formatting by
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
213 smod = etype.__module__ |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
42455
diff
changeset
|
214 if smod not in ("__main__", "builtins"): |
|
39690
0cb9e57ec406
Inspired by SF patch #860326, make the exception formatting by
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
215 stype = smod + '.' + stype |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
216 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
217 if not issubclass(etype, SyntaxError): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
218 return [_format_final_exc_line(stype, value)] |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
219 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
220 # It was a syntax error; show exactly where the problem was found. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
221 lines = [] |
|
42455
dd74e020a132
Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40959
diff
changeset
|
222 filename = value.filename or "<string>" |
|
dd74e020a132
Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40959
diff
changeset
|
223 lineno = str(value.lineno) or '?' |
|
dd74e020a132
Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40959
diff
changeset
|
224 lines.append(' File "%s", line %s\n' % (filename, lineno)) |
|
dd74e020a132
Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40959
diff
changeset
|
225 badline = value.text |
|
dd74e020a132
Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40959
diff
changeset
|
226 offset = value.offset |
|
dd74e020a132
Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40959
diff
changeset
|
227 if badline is not None: |
|
dd74e020a132
Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40959
diff
changeset
|
228 lines.append(' %s\n' % badline.strip()) |
|
dd74e020a132
Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40959
diff
changeset
|
229 if offset is not None: |
|
54571
e9b0db553d01
Merged revisions 73186,73206,73212 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52877
diff
changeset
|
230 caretspace = badline.rstrip('\n')[:offset].lstrip() |
|
42455
dd74e020a132
Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40959
diff
changeset
|
231 # non-space whitespace (likes tabs) must be kept for alignment |
|
dd74e020a132
Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40959
diff
changeset
|
232 caretspace = ((c.isspace() and c or ' ') for c in caretspace) |
|
dd74e020a132
Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40959
diff
changeset
|
233 # only three spaces to account for offset1 == pos 0 |
|
dd74e020a132
Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40959
diff
changeset
|
234 lines.append(' %s^\n' % ''.join(caretspace)) |
|
dd74e020a132
Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40959
diff
changeset
|
235 msg = value.msg or "<no detail available>" |
|
dd74e020a132
Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40959
diff
changeset
|
236 lines.append("%s: %s\n" % (stype, msg)) |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
237 return lines |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
238 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
239 def _format_final_exc_line(etype, value): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
240 valuestr = _some_str(value) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
241 if value is None or not valuestr: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
242 line = "%s\n" % etype |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
243 else: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
244 line = "%s: %s\n" % (etype, valuestr) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
245 return line |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
246 |
|
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
|
247 def _some_str(value): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
248 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
249 return str(value) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
250 except: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
251 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
|
252 |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 |
|
48474
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
254 def print_exc(limit=None, file=None, chain=True): |
|
37042
7ada16a14968
Remove sys.exc_type, sys.exc_value, sys.exc_traceback
Neal Norwitz <nnorwitz@gmail.com>
parents:
36757
diff
changeset
|
255 """Shorthand for 'print_exception(*sys.exc_info(), limit, file)'.""" |
|
23628
ea979c5206b7
Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents:
18067
diff
changeset
|
256 if file is None: |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
257 file = sys.stderr |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
258 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
259 etype, value, tb = sys.exc_info() |
|
48474
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
260 print_exception(etype, value, tb, limit, file, chain) |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
261 finally: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
262 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
|
263 |
|
30862
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
264 |
|
48474
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
265 def format_exc(limit=None, chain=True): |
|
30862
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
266 """Like print_exc() but return a string.""" |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
267 try: |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
268 etype, value, tb = sys.exc_info() |
|
48474
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
269 return ''.join( |
|
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
270 format_exception(etype, value, tb, limit, chain)) |
|
30862
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
271 finally: |
|
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
272 etype = value = tb = None |
|
31389
aca1ce1b4b61
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
30862
diff
changeset
|
273 |
|
30862
d45f8981fdea
Add traceback.format_exc().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
24844
diff
changeset
|
274 |
|
48474
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
275 def print_last(limit=None, file=None, chain=True): |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
276 """This is a shorthand for 'print_exception(sys.last_type, |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
277 sys.last_value, sys.last_traceback, limit, file)'.""" |
|
52389
d4e7470bd660
Merged revisions 70554,70588-70589,70598,70605,70611-70621,70623-70624,70626-70627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50540
diff
changeset
|
278 if not hasattr(sys, "last_type"): |
|
d4e7470bd660
Merged revisions 70554,70588-70589,70598,70605,70611-70621,70623-70624,70626-70627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50540
diff
changeset
|
279 raise ValueError("no last exception") |
|
23628
ea979c5206b7
Replaced boolean tests with is None.
Raymond Hettinger <python@rcn.com>
parents:
18067
diff
changeset
|
280 if file is None: |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
281 file = sys.stderr |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
282 print_exception(sys.last_type, sys.last_value, sys.last_traceback, |
|
48474
2ee09afee126
implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents:
46962
diff
changeset
|
283 limit, file, chain) |
|
4004
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
284 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
285 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
286 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
|
287 """Print a stack trace from its invocation point. |
|
17245
0377f927cafa
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17116
diff
changeset
|
288 |
|
17113
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
289 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
|
290 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
|
291 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
|
292 """ |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
293 if f is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
294 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
295 raise ZeroDivisionError |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
296 except ZeroDivisionError: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
297 f = sys.exc_info()[2].tb_frame.f_back |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
298 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
|
299 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
300 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
|
301 """Shorthand for 'format_list(extract_stack(f, limit))'.""" |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
302 if f is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
303 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
304 raise ZeroDivisionError |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
305 except ZeroDivisionError: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
306 f = sys.exc_info()[2].tb_frame.f_back |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
307 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
|
308 |
|
56002
fa91270b6087
Merged revisions 74821,74828-74831,74833,74835 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54571
diff
changeset
|
309 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
|
310 """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
|
311 |
|
4863b595bdae
Reformat and edit docstrings to follow modern conventions. Single
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16889
diff
changeset
|
312 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
|
313 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
|
314 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
|
315 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
|
316 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
|
317 """ |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
318 if f is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
319 try: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
320 raise ZeroDivisionError |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
321 except ZeroDivisionError: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
322 f = sys.exc_info()[2].tb_frame.f_back |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
323 if limit is None: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
324 if hasattr(sys, 'tracebacklimit'): |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
325 limit = sys.tracebacklimit |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
326 list = [] |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
327 n = 0 |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
328 while f is not None and (limit is None or n < limit): |
| 24844 | 329 lineno = f.f_lineno |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
330 co = f.f_code |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
331 filename = co.co_filename |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
332 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
|
333 linecache.checkcache(filename) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37042
diff
changeset
|
334 line = linecache.getline(filename, lineno, f.f_globals) |
|
16499
58de2829cfdd
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15873
diff
changeset
|
335 if line: line = line.strip() |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
336 else: line = None |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
337 list.append((filename, lineno, name, line)) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
338 f = f.f_back |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
339 n = n+1 |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
340 list.reverse() |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15848
diff
changeset
|
341 return list |
