Mercurial > cpython-lru_cache-weakref
annotate Lib/traceback.py @ 15842:f6cd36498a50 legacy-trunk
Document extensions to .pth files.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Sat, 13 Jan 2001 09:54:41 +0000 |
| parents | 92980920d5ed |
| children | 405a59735841 |
| 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 string |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 import sys |
|
2440
d577d749918d
handle class exceptions
Guido van Rossum <guido@python.org>
parents:
1810
diff
changeset
|
6 import types |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
8 def _print(file, str='', terminator='\n'): |
|
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
9 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
|
10 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
11 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
12 def print_list(extracted_list, file=None): |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
13 """Print the list of tuples as returned by extract_tb() or |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
14 extract_stack() as a formatted stack trace to the given file.""" |
|
4004
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
15 if not file: |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
16 file = sys.stderr |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
17 for filename, lineno, name, line in extracted_list: |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
18 _print(file, |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
19 ' File "%s", line %d, in %s' % (filename,lineno,name)) |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
20 if line: |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
21 _print(file, ' %s' % string.strip(line)) |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
22 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
23 def format_list(extracted_list): |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
24 """Given a list of tuples as returned by extract_tb() or |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
25 extract_stack(), return a list of strings ready for printing. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
26 Each string in the resulting list corresponds to the item with |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
27 the same index in the argument list. Each string ends in a |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
28 newline; the strings may contain internal newlines as well, for |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
29 those items whose source text line is not None.""" |
|
4004
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
30 list = [] |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
31 for filename, lineno, name, line in extracted_list: |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
32 item = ' File "%s", line %d, in %s\n' % (filename,lineno,name) |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
33 if line: |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
34 item = item + ' %s\n' % string.strip(line) |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
35 list.append(item) |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
36 return list |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
37 |
|
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
38 |
|
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
39 def print_tb(tb, limit=None, file=None): |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
40 """Print up to 'limit' stack trace entries from the traceback 'tb'. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
41 If 'limit' is omitted or None, all entries are printed. If 'file' is |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
42 omitted or None, the output goes to sys.stderr; otherwise 'file' |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
43 should be an open file or file-like object with a write() method.""" |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
44 if not file: |
|
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
45 file = sys.stderr |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 if limit is None: |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 if hasattr(sys, 'tracebacklimit'): |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 limit = sys.tracebacklimit |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 n = 0 |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 while tb is not None and (limit is None or n < limit): |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 f = tb.tb_frame |
|
6337
9e429518cb20
Use Marc Lemburg's tb_lineno() to calculate the correct line number.
Guido van Rossum <guido@python.org>
parents:
5832
diff
changeset
|
52 lineno = tb_lineno(tb) |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 co = f.f_code |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 filename = co.co_filename |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 name = co.co_name |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
56 _print(file, |
|
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
57 ' File "%s", line %d, in %s' % (filename,lineno,name)) |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 line = linecache.getline(filename, lineno) |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
59 if line: _print(file, ' ' + string.strip(line)) |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 tb = tb.tb_next |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 n = n+1 |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
63 def format_tb(tb, limit = None): |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
64 """A shorthand for '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
|
65 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
|
66 |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 def extract_tb(tb, limit = None): |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
68 """Return a list of up to 'limit' pre-processed stack trace entries |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
69 extracted from the traceback object 'traceback'. This is useful for |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
70 alternate formatting of stack traces. If 'limit' is omitted or None, |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
71 all entries are extracted. A pre-processed stack trace entry is a |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
72 quadruple (filename, line number, function name, text) representing |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
73 the information that is usually printed for a stack trace. The text |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
74 is a string with leading and trailing whitespace stripped; if the |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
75 source is not available it is None.""" |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 if limit is None: |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 if hasattr(sys, 'tracebacklimit'): |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 limit = sys.tracebacklimit |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 list = [] |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 n = 0 |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 while tb is not None and (limit is None or n < limit): |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 f = tb.tb_frame |
|
6337
9e429518cb20
Use Marc Lemburg's tb_lineno() to calculate the correct line number.
Guido van Rossum <guido@python.org>
parents:
5832
diff
changeset
|
83 lineno = tb_lineno(tb) |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 co = f.f_code |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 filename = co.co_filename |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 name = co.co_name |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 line = linecache.getline(filename, lineno) |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 if line: line = string.strip(line) |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 else: line = None |
|
4364
34182a6408e0
Changes to always call list.append with a single argument.
Guido van Rossum <guido@python.org>
parents:
4004
diff
changeset
|
90 list.append((filename, lineno, name, line)) |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 tb = tb.tb_next |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 n = n+1 |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 return list |
|
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
95 |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
96 def print_exception(etype, value, tb, limit=None, file=None): |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
97 """Print exception information and up to 'limit' stack trace entries |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
98 from the traceback 'tb' to 'file'. This differs from print_tb() in |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
99 the following ways: (1) if traceback is not None, it prints a header |
|
12335
e69eef692612
Match the error messages to say "(most recent call last)" like the
Guido van Rossum <guido@python.org>
parents:
11707
diff
changeset
|
100 "Traceback (most recent call last):"; (2) it prints the exception type and |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
101 value after the stack trace; (3) if type is SyntaxError and value has |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
102 the appropriate format, it prints the line where the syntax error |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
103 occurred with a caret on the next line indicating the approximate |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
104 position of the error.""" |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
105 if not file: |
|
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
106 file = sys.stderr |
|
1810
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
107 if tb: |
|
12335
e69eef692612
Match the error messages to say "(most recent call last)" like the
Guido van Rossum <guido@python.org>
parents:
11707
diff
changeset
|
108 _print(file, 'Traceback (most recent call last):') |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
109 print_tb(tb, limit, file) |
|
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
110 lines = format_exception_only(etype, value) |
|
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
111 for line in lines[:-1]: |
|
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
112 _print(file, line, ' ') |
|
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
113 _print(file, lines[-1], '') |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
114 |
|
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
115 def format_exception(etype, value, tb, limit = None): |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
116 """Format a stack trace and the exception information. The arguments |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
117 have the same meaning as the corresponding arguments to |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
118 print_exception(). The return value is a list of strings, each |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
119 ending in a newline and some containing internal newlines. When |
|
13620
5b727ab37092
Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents:
12335
diff
changeset
|
120 these lines are concatenated and printed, exactly the same text is |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
121 printed as does print_exception().""" |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
122 if tb: |
|
12335
e69eef692612
Match the error messages to say "(most recent call last)" like the
Guido van Rossum <guido@python.org>
parents:
11707
diff
changeset
|
123 list = ['Traceback (most recent call last):\n'] |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
124 list = list + format_tb(tb, limit) |
|
7718
f83425cc7e96
Add obvious needed else clause to format_exception().
Guido van Rossum <guido@python.org>
parents:
7489
diff
changeset
|
125 else: |
|
f83425cc7e96
Add obvious needed else clause to format_exception().
Guido van Rossum <guido@python.org>
parents:
7489
diff
changeset
|
126 list = [] |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
127 list = list + format_exception_only(etype, value) |
|
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
128 return list |
|
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
129 |
|
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
130 def format_exception_only(etype, value): |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
131 """Format the exception part of a traceback. The arguments are the |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
132 exception type and value such as given by sys.last_type and |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
133 sys.last_value. The return value is a list of strings, each ending |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
134 in a newline. Normally, the list contains a single string; |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
135 however, for SyntaxError exceptions, it contains several lines that |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
136 (when printed) display detailed information about where the syntax |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
137 error occurred. The message indicating which exception occurred is |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
138 the always last string in the list.""" |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
139 list = [] |
|
2440
d577d749918d
handle class exceptions
Guido van Rossum <guido@python.org>
parents:
1810
diff
changeset
|
140 if type(etype) == types.ClassType: |
|
d577d749918d
handle class exceptions
Guido van Rossum <guido@python.org>
parents:
1810
diff
changeset
|
141 stype = etype.__name__ |
|
d577d749918d
handle class exceptions
Guido van Rossum <guido@python.org>
parents:
1810
diff
changeset
|
142 else: |
|
d577d749918d
handle class exceptions
Guido van Rossum <guido@python.org>
parents:
1810
diff
changeset
|
143 stype = etype |
|
1810
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
144 if value is None: |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
145 list.append(str(stype) + '\n') |
|
1810
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
146 else: |
|
2440
d577d749918d
handle class exceptions
Guido van Rossum <guido@python.org>
parents:
1810
diff
changeset
|
147 if etype is SyntaxError: |
|
1810
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
148 try: |
|
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
149 msg, (filename, lineno, offset, line) = value |
|
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
150 except: |
|
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
151 pass |
|
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
152 else: |
|
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
153 if not filename: filename = "<string>" |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
154 list.append(' File "%s", line %d\n' % |
|
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
155 (filename, lineno)) |
|
1810
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
156 i = 0 |
|
2440
d577d749918d
handle class exceptions
Guido van Rossum <guido@python.org>
parents:
1810
diff
changeset
|
157 while i < len(line) and \ |
|
d577d749918d
handle class exceptions
Guido van Rossum <guido@python.org>
parents:
1810
diff
changeset
|
158 line[i] in string.whitespace: |
|
1810
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
159 i = i+1 |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
160 list.append(' %s\n' % string.strip(line)) |
|
1810
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
161 s = ' ' |
|
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
162 for c in line[i:offset-1]: |
|
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
163 if c in string.whitespace: |
|
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
164 s = s + c |
|
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
165 else: |
|
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
166 s = s + ' ' |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
167 list.append('%s^\n' % s) |
|
1810
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1699
diff
changeset
|
168 value = msg |
|
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
|
169 list.append('%s: %s\n' % (str(stype), _some_str(value))) |
|
2901
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
170 return list |
|
57ec24e0f35a
added format_* functions (suggestion by Ken M)
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
171 |
|
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
|
172 def _some_str(value): |
|
92980920d5ed
Patch by Toby Dickenson: don't die when an error occurs during string
Guido van Rossum <guido@python.org>
parents:
13620
diff
changeset
|
173 try: |
|
92980920d5ed
Patch by Toby Dickenson: don't die when an error occurs during string
Guido van Rossum <guido@python.org>
parents:
13620
diff
changeset
|
174 return str(value) |
|
92980920d5ed
Patch by Toby Dickenson: don't die when an error occurs during string
Guido van Rossum <guido@python.org>
parents:
13620
diff
changeset
|
175 except: |
|
92980920d5ed
Patch by Toby Dickenson: don't die when an error occurs during string
Guido van Rossum <guido@python.org>
parents:
13620
diff
changeset
|
176 return '<unprintable %s object>' % type(value).__name__ |
|
92980920d5ed
Patch by Toby Dickenson: don't die when an error occurs during string
Guido van Rossum <guido@python.org>
parents:
13620
diff
changeset
|
177 |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
179 def print_exc(limit=None, file=None): |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
180 """This is a shorthand for 'print_exception(sys.exc_type, |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
181 sys.exc_value, sys.exc_traceback, limit, file)'. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
182 (In fact, it uses sys.exc_info() to retrieve the same information |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
183 in a thread-safe way.)""" |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
184 if not file: |
|
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
185 file = sys.stderr |
|
5832
285fb6a99e5f
Use sys.exc_info() instead of sys.exc_{type,value,traceback}.
Guido van Rossum <guido@python.org>
parents:
4364
diff
changeset
|
186 try: |
|
285fb6a99e5f
Use sys.exc_info() instead of sys.exc_{type,value,traceback}.
Guido van Rossum <guido@python.org>
parents:
4364
diff
changeset
|
187 etype, value, tb = sys.exc_info() |
|
285fb6a99e5f
Use sys.exc_info() instead of sys.exc_{type,value,traceback}.
Guido van Rossum <guido@python.org>
parents:
4364
diff
changeset
|
188 print_exception(etype, value, tb, limit, file) |
|
285fb6a99e5f
Use sys.exc_info() instead of sys.exc_{type,value,traceback}.
Guido van Rossum <guido@python.org>
parents:
4364
diff
changeset
|
189 finally: |
|
285fb6a99e5f
Use sys.exc_info() instead of sys.exc_{type,value,traceback}.
Guido van Rossum <guido@python.org>
parents:
4364
diff
changeset
|
190 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
|
191 |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
192 def print_last(limit=None, file=None): |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
193 """This is a shorthand for 'print_exception(sys.last_type, |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
194 sys.last_value, sys.last_traceback, limit, file)'.""" |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
195 if not file: |
|
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
196 file = sys.stderr |
|
1699
93df83022be8
New module, formats traceback just like the C code does
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 print_exception(sys.last_type, sys.last_value, sys.last_traceback, |
|
3115
9497cdcdb6aa
add file parameter to all printing fns, default stderr
Guido van Rossum <guido@python.org>
parents:
2901
diff
changeset
|
198 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
|
199 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
200 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
201 def print_stack(f=None, limit=None, file=None): |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
202 """This function prints a stack trace from its invocation point. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
203 The optional 'f' argument can be used to specify an alternate stack |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
204 frame at which to start. The optional 'limit' and 'file' arguments |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
205 have the same meaning as for print_exception().""" |
|
4004
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
206 if f is None: |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
207 try: |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
208 raise ZeroDivisionError |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
209 except ZeroDivisionError: |
|
5832
285fb6a99e5f
Use sys.exc_info() instead of sys.exc_{type,value,traceback}.
Guido van Rossum <guido@python.org>
parents:
4364
diff
changeset
|
210 f = sys.exc_info()[2].tb_frame.f_back |
|
4004
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
211 print_list(extract_stack(f, limit), file) |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
212 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
213 def format_stack(f=None, limit=None): |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
214 """A shorthand for '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
|
215 if f is None: |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
216 try: |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
217 raise ZeroDivisionError |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
218 except ZeroDivisionError: |
|
5832
285fb6a99e5f
Use sys.exc_info() instead of sys.exc_{type,value,traceback}.
Guido van Rossum <guido@python.org>
parents:
4364
diff
changeset
|
219 f = sys.exc_info()[2].tb_frame.f_back |
|
285fb6a99e5f
Use sys.exc_info() instead of sys.exc_{type,value,traceback}.
Guido van Rossum <guido@python.org>
parents:
4364
diff
changeset
|
220 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
|
221 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
222 def extract_stack(f=None, limit = None): |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
223 """Extract the raw traceback from the current stack frame. The |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
224 return value has the same format as for extract_tb(). The optional |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
225 'f' and 'limit' arguments have the same meaning as for print_stack(). |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
226 Each item in the list is a quadruple (filename, line number, |
|
12335
e69eef692612
Match the error messages to say "(most recent call last)" like the
Guido van Rossum <guido@python.org>
parents:
11707
diff
changeset
|
227 function name, text), and the entries are in order from oldest |
|
e69eef692612
Match the error messages to say "(most recent call last)" like the
Guido van Rossum <guido@python.org>
parents:
11707
diff
changeset
|
228 to newest stack frame.""" |
|
4004
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
229 if f is None: |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
230 try: |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
231 raise ZeroDivisionError |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
232 except ZeroDivisionError: |
|
5832
285fb6a99e5f
Use sys.exc_info() instead of sys.exc_{type,value,traceback}.
Guido van Rossum <guido@python.org>
parents:
4364
diff
changeset
|
233 f = sys.exc_info()[2].tb_frame.f_back |
|
4004
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
234 if limit is None: |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
235 if hasattr(sys, 'tracebacklimit'): |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
236 limit = sys.tracebacklimit |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
237 list = [] |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
238 n = 0 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
239 while f is not None and (limit is None or n < limit): |
|
6337
9e429518cb20
Use Marc Lemburg's tb_lineno() to calculate the correct line number.
Guido van Rossum <guido@python.org>
parents:
5832
diff
changeset
|
240 lineno = f.f_lineno # XXX Too bad if -O is used |
|
4004
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
241 co = f.f_code |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
242 filename = co.co_filename |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
243 name = co.co_name |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
244 line = linecache.getline(filename, lineno) |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
245 if line: line = string.strip(line) |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
246 else: line = None |
|
4364
34182a6408e0
Changes to always call list.append with a single argument.
Guido van Rossum <guido@python.org>
parents:
4004
diff
changeset
|
247 list.append((filename, lineno, name, line)) |
|
4004
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
248 f = f.f_back |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
249 n = n+1 |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
250 list.reverse() |
|
d3a1f6088c28
Added routines to print, format and extract the current, ``live'' stack.
Guido van Rossum <guido@python.org>
parents:
3640
diff
changeset
|
251 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
|
252 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
253 def tb_lineno(tb): |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
254 """Calculate the correct line number of the traceback given in tb |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
255 (even with -O on).""" |
|
6337
9e429518cb20
Use Marc Lemburg's tb_lineno() to calculate the correct line number.
Guido van Rossum <guido@python.org>
parents:
5832
diff
changeset
|
256 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
257 # Coded by Marc-Andre Lemburg from the example of PyCode_Addr2Line() |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
258 # in compile.c. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
259 # Revised version by Jim Hugunin to work with JPython too. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
7718
diff
changeset
|
260 |
|
7489
427b81c2452f
New version of tb_lineno(), this time *not* using try-except, to avoid
Guido van Rossum <guido@python.org>
parents:
7479
diff
changeset
|
261 c = tb.tb_frame.f_code |
|
427b81c2452f
New version of tb_lineno(), this time *not* using try-except, to avoid
Guido van Rossum <guido@python.org>
parents:
7479
diff
changeset
|
262 if not hasattr(c, 'co_lnotab'): |
|
427b81c2452f
New version of tb_lineno(), this time *not* using try-except, to avoid
Guido van Rossum <guido@python.org>
parents:
7479
diff
changeset
|
263 return tb.tb_lineno |
|
427b81c2452f
New version of tb_lineno(), this time *not* using try-except, to avoid
Guido van Rossum <guido@python.org>
parents:
7479
diff
changeset
|
264 |
|
427b81c2452f
New version of tb_lineno(), this time *not* using try-except, to avoid
Guido van Rossum <guido@python.org>
parents:
7479
diff
changeset
|
265 tab = c.co_lnotab |
|
427b81c2452f
New version of tb_lineno(), this time *not* using try-except, to avoid
Guido van Rossum <guido@python.org>
parents:
7479
diff
changeset
|
266 line = c.co_firstlineno |
|
427b81c2452f
New version of tb_lineno(), this time *not* using try-except, to avoid
Guido van Rossum <guido@python.org>
parents:
7479
diff
changeset
|
267 stopat = tb.tb_lasti |
|
6337
9e429518cb20
Use Marc Lemburg's tb_lineno() to calculate the correct line number.
Guido van Rossum <guido@python.org>
parents:
5832
diff
changeset
|
268 addr = 0 |
|
9e429518cb20
Use Marc Lemburg's tb_lineno() to calculate the correct line number.
Guido van Rossum <guido@python.org>
parents:
5832
diff
changeset
|
269 for i in range(0, len(tab), 2): |
|
9e429518cb20
Use Marc Lemburg's tb_lineno() to calculate the correct line number.
Guido van Rossum <guido@python.org>
parents:
5832
diff
changeset
|
270 addr = addr + ord(tab[i]) |
|
9e429518cb20
Use Marc Lemburg's tb_lineno() to calculate the correct line number.
Guido van Rossum <guido@python.org>
parents:
5832
diff
changeset
|
271 if addr > stopat: |
|
9e429518cb20
Use Marc Lemburg's tb_lineno() to calculate the correct line number.
Guido van Rossum <guido@python.org>
parents:
5832
diff
changeset
|
272 break |
|
9e429518cb20
Use Marc Lemburg's tb_lineno() to calculate the correct line number.
Guido van Rossum <guido@python.org>
parents:
5832
diff
changeset
|
273 line = line + ord(tab[i+1]) |
|
9e429518cb20
Use Marc Lemburg's tb_lineno() to calculate the correct line number.
Guido van Rossum <guido@python.org>
parents:
5832
diff
changeset
|
274 return line |
