Show More
This diff has been collapsed as it changes many lines, (514 lines changed) Show them Hide them | |||||
@@ -1,1503 +1,1057 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | Verbose and colourful traceback formatting. |
|
3 | Verbose and colourful traceback formatting. | |
4 |
|
4 | |||
5 | **ColorTB** |
|
5 | **ColorTB** | |
6 |
|
6 | |||
7 | I've always found it a bit hard to visually parse tracebacks in Python. The |
|
7 | I've always found it a bit hard to visually parse tracebacks in Python. The | |
8 | ColorTB class is a solution to that problem. It colors the different parts of a |
|
8 | ColorTB class is a solution to that problem. It colors the different parts of a | |
9 | traceback in a manner similar to what you would expect from a syntax-highlighting |
|
9 | traceback in a manner similar to what you would expect from a syntax-highlighting | |
10 | text editor. |
|
10 | text editor. | |
11 |
|
11 | |||
12 | Installation instructions for ColorTB:: |
|
12 | Installation instructions for ColorTB:: | |
13 |
|
13 | |||
14 | import sys,ultratb |
|
14 | import sys,ultratb | |
15 | sys.excepthook = ultratb.ColorTB() |
|
15 | sys.excepthook = ultratb.ColorTB() | |
16 |
|
16 | |||
17 | **VerboseTB** |
|
17 | **VerboseTB** | |
18 |
|
18 | |||
19 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds |
|
19 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds | |
20 | of useful info when a traceback occurs. Ping originally had it spit out HTML |
|
20 | of useful info when a traceback occurs. Ping originally had it spit out HTML | |
21 | and intended it for CGI programmers, but why should they have all the fun? I |
|
21 | and intended it for CGI programmers, but why should they have all the fun? I | |
22 | altered it to spit out colored text to the terminal. It's a bit overwhelming, |
|
22 | altered it to spit out colored text to the terminal. It's a bit overwhelming, | |
23 | but kind of neat, and maybe useful for long-running programs that you believe |
|
23 | but kind of neat, and maybe useful for long-running programs that you believe | |
24 | are bug-free. If a crash *does* occur in that type of program you want details. |
|
24 | are bug-free. If a crash *does* occur in that type of program you want details. | |
25 | Give it a shot--you'll love it or you'll hate it. |
|
25 | Give it a shot--you'll love it or you'll hate it. | |
26 |
|
26 | |||
27 | .. note:: |
|
27 | .. note:: | |
28 |
|
28 | |||
29 | The Verbose mode prints the variables currently visible where the exception |
|
29 | The Verbose mode prints the variables currently visible where the exception | |
30 | happened (shortening their strings if too long). This can potentially be |
|
30 | happened (shortening their strings if too long). This can potentially be | |
31 | very slow, if you happen to have a huge data structure whose string |
|
31 | very slow, if you happen to have a huge data structure whose string | |
32 | representation is complex to compute. Your computer may appear to freeze for |
|
32 | representation is complex to compute. Your computer may appear to freeze for | |
33 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback |
|
33 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback | |
34 | with Ctrl-C (maybe hitting it more than once). |
|
34 | with Ctrl-C (maybe hitting it more than once). | |
35 |
|
35 | |||
36 | If you encounter this kind of situation often, you may want to use the |
|
36 | If you encounter this kind of situation often, you may want to use the | |
37 | Verbose_novars mode instead of the regular Verbose, which avoids formatting |
|
37 | Verbose_novars mode instead of the regular Verbose, which avoids formatting | |
38 | variables (but otherwise includes the information and context given by |
|
38 | variables (but otherwise includes the information and context given by | |
39 | Verbose). |
|
39 | Verbose). | |
40 |
|
40 | |||
41 | .. note:: |
|
41 | .. note:: | |
42 |
|
42 | |||
43 | The verbose mode print all variables in the stack, which means it can |
|
43 | The verbose mode print all variables in the stack, which means it can | |
44 | potentially leak sensitive information like access keys, or unencrypted |
|
44 | potentially leak sensitive information like access keys, or unencrypted | |
45 | password. |
|
45 | password. | |
46 |
|
46 | |||
47 | Installation instructions for VerboseTB:: |
|
47 | Installation instructions for VerboseTB:: | |
48 |
|
48 | |||
49 | import sys,ultratb |
|
49 | import sys,ultratb | |
50 | sys.excepthook = ultratb.VerboseTB() |
|
50 | sys.excepthook = ultratb.VerboseTB() | |
51 |
|
51 | |||
52 | Note: Much of the code in this module was lifted verbatim from the standard |
|
52 | Note: Much of the code in this module was lifted verbatim from the standard | |
53 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. |
|
53 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. | |
54 |
|
54 | |||
55 | Color schemes |
|
55 | Color schemes | |
56 | ------------- |
|
56 | ------------- | |
57 |
|
57 | |||
58 | The colors are defined in the class TBTools through the use of the |
|
58 | The colors are defined in the class TBTools through the use of the | |
59 | ColorSchemeTable class. Currently the following exist: |
|
59 | ColorSchemeTable class. Currently the following exist: | |
60 |
|
60 | |||
61 | - NoColor: allows all of this module to be used in any terminal (the color |
|
61 | - NoColor: allows all of this module to be used in any terminal (the color | |
62 | escapes are just dummy blank strings). |
|
62 | escapes are just dummy blank strings). | |
63 |
|
63 | |||
64 | - Linux: is meant to look good in a terminal like the Linux console (black |
|
64 | - Linux: is meant to look good in a terminal like the Linux console (black | |
65 | or very dark background). |
|
65 | or very dark background). | |
66 |
|
66 | |||
67 | - LightBG: similar to Linux but swaps dark/light colors to be more readable |
|
67 | - LightBG: similar to Linux but swaps dark/light colors to be more readable | |
68 | in light background terminals. |
|
68 | in light background terminals. | |
69 |
|
69 | |||
70 | - Neutral: a neutral color scheme that should be readable on both light and |
|
70 | - Neutral: a neutral color scheme that should be readable on both light and | |
71 | dark background |
|
71 | dark background | |
72 |
|
72 | |||
73 | You can implement other color schemes easily, the syntax is fairly |
|
73 | You can implement other color schemes easily, the syntax is fairly | |
74 | self-explanatory. Please send back new schemes you develop to the author for |
|
74 | self-explanatory. Please send back new schemes you develop to the author for | |
75 | possible inclusion in future releases. |
|
75 | possible inclusion in future releases. | |
76 |
|
76 | |||
77 | Inheritance diagram: |
|
77 | Inheritance diagram: | |
78 |
|
78 | |||
79 | .. inheritance-diagram:: IPython.core.ultratb |
|
79 | .. inheritance-diagram:: IPython.core.ultratb | |
80 | :parts: 3 |
|
80 | :parts: 3 | |
81 | """ |
|
81 | """ | |
82 |
|
82 | |||
83 | #***************************************************************************** |
|
83 | #***************************************************************************** | |
84 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> |
|
84 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> | |
85 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
85 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> | |
86 | # |
|
86 | # | |
87 | # Distributed under the terms of the BSD License. The full license is in |
|
87 | # Distributed under the terms of the BSD License. The full license is in | |
88 | # the file COPYING, distributed as part of this software. |
|
88 | # the file COPYING, distributed as part of this software. | |
89 | #***************************************************************************** |
|
89 | #***************************************************************************** | |
90 |
|
90 | |||
91 |
|
91 | |||
92 | import dis |
|
|||
93 | import inspect |
|
92 | import inspect | |
94 | import keyword |
|
|||
95 | import linecache |
|
93 | import linecache | |
96 | import os |
|
|||
97 | import pydoc |
|
94 | import pydoc | |
98 | import re |
|
|||
99 | import sys |
|
95 | import sys | |
100 | import time |
|
96 | import time | |
101 | import tokenize |
|
97 | import tokenize | |
102 | import traceback |
|
98 | import traceback | |
103 |
|
99 | |||
104 | from tokenize import generate_tokens |
|
100 | import stack_data | |
105 |
|
101 | |||
106 | # For purposes of monkeypatching inspect to fix a bug in it. |
|
102 | try: # Python 2 | |
107 | from inspect import getsourcefile, getfile, getmodule, \ |
|
103 | generate_tokens = tokenize.generate_tokens | |
108 | ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode |
|
104 | except AttributeError: # Python 3 | |
|
105 | generate_tokens = tokenize.tokenize | |||
109 |
|
106 | |||
110 | # IPython's own modules |
|
107 | # IPython's own modules | |
111 | from IPython import get_ipython |
|
108 | from IPython import get_ipython | |
112 | from IPython.core import debugger |
|
109 | from IPython.core import debugger | |
113 | from IPython.core.display_trap import DisplayTrap |
|
110 | from IPython.core.display_trap import DisplayTrap | |
114 | from IPython.core.excolors import exception_colors |
|
111 | from IPython.core.excolors import exception_colors | |
115 | from IPython.utils import PyColorize |
|
112 | from IPython.utils import PyColorize | |
116 | from IPython.utils import path as util_path |
|
113 | from IPython.utils import path as util_path | |
117 | from IPython.utils import py3compat |
|
114 | from IPython.utils import py3compat | |
118 | from IPython.utils.data import uniq_stable |
|
|||
119 | from IPython.utils.terminal import get_terminal_size |
|
115 | from IPython.utils.terminal import get_terminal_size | |
120 |
|
116 | |||
121 | from logging import info, error, debug |
|
|||
122 |
|
||||
123 | from importlib.util import source_from_cache |
|
|||
124 |
|
||||
125 | import IPython.utils.colorable as colorable |
|
117 | import IPython.utils.colorable as colorable | |
126 |
|
118 | |||
127 | # Globals |
|
119 | # Globals | |
128 | # amount of space to put line numbers before verbose tracebacks |
|
120 | # amount of space to put line numbers before verbose tracebacks | |
129 | INDENT_SIZE = 8 |
|
121 | INDENT_SIZE = 8 | |
130 |
|
122 | |||
131 | # Default color scheme. This is used, for example, by the traceback |
|
123 | # Default color scheme. This is used, for example, by the traceback | |
132 | # formatter. When running in an actual IPython instance, the user's rc.colors |
|
124 | # formatter. When running in an actual IPython instance, the user's rc.colors | |
133 | # value is used, but having a module global makes this functionality available |
|
125 | # value is used, but having a module global makes this functionality available | |
134 | # to users of ultratb who are NOT running inside ipython. |
|
126 | # to users of ultratb who are NOT running inside ipython. | |
135 | DEFAULT_SCHEME = 'NoColor' |
|
127 | DEFAULT_SCHEME = 'NoColor' | |
136 |
|
128 | |||
137 |
|
||||
138 | # Number of frame above which we are likely to have a recursion and will |
|
|||
139 | # **attempt** to detect it. Made modifiable mostly to speedup test suite |
|
|||
140 | # as detecting recursion is one of our slowest test |
|
|||
141 | _FRAME_RECURSION_LIMIT = 500 |
|
|||
142 |
|
||||
143 | # --------------------------------------------------------------------------- |
|
129 | # --------------------------------------------------------------------------- | |
144 | # Code begins |
|
130 | # Code begins | |
145 |
|
131 | |||
146 | # Utility functions |
|
|||
147 | def inspect_error(): |
|
|||
148 | """Print a message about internal inspect errors. |
|
|||
149 |
|
||||
150 | These are unfortunately quite common.""" |
|
|||
151 |
|
||||
152 | error('Internal Python error in the inspect module.\n' |
|
|||
153 | 'Below is the traceback from this internal error.\n') |
|
|||
154 |
|
||||
155 |
|
||||
156 | # This function is a monkeypatch we apply to the Python inspect module. We have |
|
|||
157 | # now found when it's needed (see discussion on issue gh-1456), and we have a |
|
|||
158 | # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if |
|
|||
159 | # the monkeypatch is not applied. TK, Aug 2012. |
|
|||
160 | def findsource(object): |
|
|||
161 | """Return the entire source file and starting line number for an object. |
|
|||
162 |
|
||||
163 | The argument may be a module, class, method, function, traceback, frame, |
|
|||
164 | or code object. The source code is returned as a list of all the lines |
|
|||
165 | in the file and the line number indexes a line in that list. An IOError |
|
|||
166 | is raised if the source code cannot be retrieved. |
|
|||
167 |
|
||||
168 | FIXED version with which we monkeypatch the stdlib to work around a bug.""" |
|
|||
169 |
|
||||
170 | file = getsourcefile(object) or getfile(object) |
|
|||
171 | # If the object is a frame, then trying to get the globals dict from its |
|
|||
172 | # module won't work. Instead, the frame object itself has the globals |
|
|||
173 | # dictionary. |
|
|||
174 | globals_dict = None |
|
|||
175 | if inspect.isframe(object): |
|
|||
176 | # XXX: can this ever be false? |
|
|||
177 | globals_dict = object.f_globals |
|
|||
178 | else: |
|
|||
179 | module = getmodule(object, file) |
|
|||
180 | if module: |
|
|||
181 | globals_dict = module.__dict__ |
|
|||
182 | lines = linecache.getlines(file, globals_dict) |
|
|||
183 | if not lines: |
|
|||
184 | raise IOError('could not get source code') |
|
|||
185 |
|
||||
186 | if ismodule(object): |
|
|||
187 | return lines, 0 |
|
|||
188 |
|
||||
189 | if isclass(object): |
|
|||
190 | name = object.__name__ |
|
|||
191 | pat = re.compile(r'^(\s*)class\s*' + name + r'\b') |
|
|||
192 | # make some effort to find the best matching class definition: |
|
|||
193 | # use the one with the least indentation, which is the one |
|
|||
194 | # that's most probably not inside a function definition. |
|
|||
195 | candidates = [] |
|
|||
196 | for i, line in enumerate(lines): |
|
|||
197 | match = pat.match(line) |
|
|||
198 | if match: |
|
|||
199 | # if it's at toplevel, it's already the best one |
|
|||
200 | if line[0] == 'c': |
|
|||
201 | return lines, i |
|
|||
202 | # else add whitespace to candidate list |
|
|||
203 | candidates.append((match.group(1), i)) |
|
|||
204 | if candidates: |
|
|||
205 | # this will sort by whitespace, and by line number, |
|
|||
206 | # less whitespace first |
|
|||
207 | candidates.sort() |
|
|||
208 | return lines, candidates[0][1] |
|
|||
209 | else: |
|
|||
210 | raise IOError('could not find class definition') |
|
|||
211 |
|
||||
212 | if ismethod(object): |
|
|||
213 | object = object.__func__ |
|
|||
214 | if isfunction(object): |
|
|||
215 | object = object.__code__ |
|
|||
216 | if istraceback(object): |
|
|||
217 | object = object.tb_frame |
|
|||
218 | if isframe(object): |
|
|||
219 | object = object.f_code |
|
|||
220 | if iscode(object): |
|
|||
221 | if not hasattr(object, 'co_firstlineno'): |
|
|||
222 | raise IOError('could not find function definition') |
|
|||
223 | pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') |
|
|||
224 | pmatch = pat.match |
|
|||
225 | # fperez - fix: sometimes, co_firstlineno can give a number larger than |
|
|||
226 | # the length of lines, which causes an error. Safeguard against that. |
|
|||
227 | lnum = min(object.co_firstlineno, len(lines)) - 1 |
|
|||
228 | while lnum > 0: |
|
|||
229 | if pmatch(lines[lnum]): |
|
|||
230 | break |
|
|||
231 | lnum -= 1 |
|
|||
232 |
|
||||
233 | return lines, lnum |
|
|||
234 | raise IOError('could not find code object') |
|
|||
235 |
|
||||
236 |
|
||||
237 | # This is a patched version of inspect.getargs that applies the (unmerged) |
|
|||
238 | # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes |
|
|||
239 | # https://github.com/ipython/ipython/issues/8205 and |
|
|||
240 | # https://github.com/ipython/ipython/issues/8293 |
|
|||
241 | def getargs(co): |
|
|||
242 | """Get information about the arguments accepted by a code object. |
|
|||
243 |
|
||||
244 | Three things are returned: (args, varargs, varkw), where 'args' is |
|
|||
245 | a list of argument names (possibly containing nested lists), and |
|
|||
246 | 'varargs' and 'varkw' are the names of the * and ** arguments or None.""" |
|
|||
247 | if not iscode(co): |
|
|||
248 | raise TypeError('{!r} is not a code object'.format(co)) |
|
|||
249 |
|
||||
250 | nargs = co.co_argcount |
|
|||
251 | names = co.co_varnames |
|
|||
252 | args = list(names[:nargs]) |
|
|||
253 | step = 0 |
|
|||
254 |
|
||||
255 | # The following acrobatics are for anonymous (tuple) arguments. |
|
|||
256 | for i in range(nargs): |
|
|||
257 | if args[i][:1] in ('', '.'): |
|
|||
258 | stack, remain, count = [], [], [] |
|
|||
259 | while step < len(co.co_code): |
|
|||
260 | op = ord(co.co_code[step]) |
|
|||
261 | step = step + 1 |
|
|||
262 | if op >= dis.HAVE_ARGUMENT: |
|
|||
263 | opname = dis.opname[op] |
|
|||
264 | value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256 |
|
|||
265 | step = step + 2 |
|
|||
266 | if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'): |
|
|||
267 | remain.append(value) |
|
|||
268 | count.append(value) |
|
|||
269 | elif opname in ('STORE_FAST', 'STORE_DEREF'): |
|
|||
270 | if op in dis.haslocal: |
|
|||
271 | stack.append(co.co_varnames[value]) |
|
|||
272 | elif op in dis.hasfree: |
|
|||
273 | stack.append((co.co_cellvars + co.co_freevars)[value]) |
|
|||
274 | # Special case for sublists of length 1: def foo((bar)) |
|
|||
275 | # doesn't generate the UNPACK_TUPLE bytecode, so if |
|
|||
276 | # `remain` is empty here, we have such a sublist. |
|
|||
277 | if not remain: |
|
|||
278 | stack[0] = [stack[0]] |
|
|||
279 | break |
|
|||
280 | else: |
|
|||
281 | remain[-1] = remain[-1] - 1 |
|
|||
282 | while remain[-1] == 0: |
|
|||
283 | remain.pop() |
|
|||
284 | size = count.pop() |
|
|||
285 | stack[-size:] = [stack[-size:]] |
|
|||
286 | if not remain: |
|
|||
287 | break |
|
|||
288 | remain[-1] = remain[-1] - 1 |
|
|||
289 | if not remain: |
|
|||
290 | break |
|
|||
291 | args[i] = stack[0] |
|
|||
292 |
|
||||
293 | varargs = None |
|
|||
294 | if co.co_flags & inspect.CO_VARARGS: |
|
|||
295 | varargs = co.co_varnames[nargs] |
|
|||
296 | nargs = nargs + 1 |
|
|||
297 | varkw = None |
|
|||
298 | if co.co_flags & inspect.CO_VARKEYWORDS: |
|
|||
299 | varkw = co.co_varnames[nargs] |
|
|||
300 | return inspect.Arguments(args, varargs, varkw) |
|
|||
301 |
|
||||
302 |
|
||||
303 | # Monkeypatch inspect to apply our bugfix. |
|
|||
304 | def with_patch_inspect(f): |
|
|||
305 | """ |
|
|||
306 | Deprecated since IPython 6.0 |
|
|||
307 | decorator for monkeypatching inspect.findsource |
|
|||
308 | """ |
|
|||
309 |
|
||||
310 | def wrapped(*args, **kwargs): |
|
|||
311 | save_findsource = inspect.findsource |
|
|||
312 | save_getargs = inspect.getargs |
|
|||
313 | inspect.findsource = findsource |
|
|||
314 | inspect.getargs = getargs |
|
|||
315 | try: |
|
|||
316 | return f(*args, **kwargs) |
|
|||
317 | finally: |
|
|||
318 | inspect.findsource = save_findsource |
|
|||
319 | inspect.getargs = save_getargs |
|
|||
320 |
|
||||
321 | return wrapped |
|
|||
322 |
|
||||
323 |
|
||||
324 | def fix_frame_records_filenames(records): |
|
|||
325 | """Try to fix the filenames in each record from inspect.getinnerframes(). |
|
|||
326 |
|
||||
327 | Particularly, modules loaded from within zip files have useless filenames |
|
|||
328 | attached to their code object, and inspect.getinnerframes() just uses it. |
|
|||
329 | """ |
|
|||
330 | fixed_records = [] |
|
|||
331 | for frame, filename, line_no, func_name, lines, index in records: |
|
|||
332 | # Look inside the frame's globals dictionary for __file__, |
|
|||
333 | # which should be better. However, keep Cython filenames since |
|
|||
334 | # we prefer the source filenames over the compiled .so file. |
|
|||
335 | if not filename.endswith(('.pyx', '.pxd', '.pxi')): |
|
|||
336 | better_fn = frame.f_globals.get('__file__', None) |
|
|||
337 | if isinstance(better_fn, str): |
|
|||
338 | # Check the type just in case someone did something weird with |
|
|||
339 | # __file__. It might also be None if the error occurred during |
|
|||
340 | # import. |
|
|||
341 | filename = better_fn |
|
|||
342 | fixed_records.append((frame, filename, line_no, func_name, lines, index)) |
|
|||
343 | return fixed_records |
|
|||
344 |
|
||||
345 |
|
||||
346 | @with_patch_inspect |
|
|||
347 | def _fixed_getinnerframes(etb, context=1, tb_offset=0): |
|
|||
348 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 |
|
|||
349 |
|
||||
350 | records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) |
|
|||
351 | # If the error is at the console, don't build any context, since it would |
|
|||
352 | # otherwise produce 5 blank lines printed out (there is no file at the |
|
|||
353 | # console) |
|
|||
354 | rec_check = records[tb_offset:] |
|
|||
355 | try: |
|
|||
356 | rname = rec_check[0][1] |
|
|||
357 | if rname == '<ipython console>' or rname.endswith('<string>'): |
|
|||
358 | return rec_check |
|
|||
359 | except IndexError: |
|
|||
360 | pass |
|
|||
361 |
|
||||
362 | aux = traceback.extract_tb(etb) |
|
|||
363 | assert len(records) == len(aux) |
|
|||
364 | for i, (file, lnum, _, _) in enumerate(aux): |
|
|||
365 | maybeStart = lnum - 1 - context // 2 |
|
|||
366 | start = max(maybeStart, 0) |
|
|||
367 | end = start + context |
|
|||
368 | lines = linecache.getlines(file)[start:end] |
|
|||
369 | buf = list(records[i]) |
|
|||
370 | buf[LNUM_POS] = lnum |
|
|||
371 | buf[INDEX_POS] = lnum - 1 - start |
|
|||
372 | buf[LINES_POS] = lines |
|
|||
373 | records[i] = tuple(buf) |
|
|||
374 | return records[tb_offset:] |
|
|||
375 |
|
||||
376 | # Helper function -- largely belongs to VerboseTB, but we need the same |
|
132 | # Helper function -- largely belongs to VerboseTB, but we need the same | |
377 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they |
|
133 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they | |
378 | # can be recognized properly by ipython.el's py-traceback-line-re |
|
134 | # can be recognized properly by ipython.el's py-traceback-line-re | |
379 | # (SyntaxErrors have to be treated specially because they have no traceback) |
|
135 | # (SyntaxErrors have to be treated specially because they have no traceback) | |
380 |
|
136 | |||
381 |
|
137 | |||
382 |
def _format_traceback_lines( |
|
138 | def _format_traceback_lines(lines, Colors, lvals, _line_format): | |
383 | """ |
|
139 | """ | |
384 | Format tracebacks lines with pointing arrow, leading numbers... |
|
140 | Format tracebacks lines with pointing arrow, leading numbers... | |
385 |
|
141 | |||
386 | Parameters |
|
142 | Parameters | |
387 | ========== |
|
143 | ========== | |
388 |
|
144 | |||
389 | lnum: int |
|
145 | lines: list[Line] | |
390 | index: int |
|
|||
391 | lines: list[string] |
|
|||
392 | Colors: |
|
146 | Colors: | |
393 | ColorScheme used. |
|
147 | ColorScheme used. | |
394 |
lvals: |
|
148 | lvals: str | |
395 | Values of local variables, already colored, to inject just after the error line. |
|
149 | Values of local variables, already colored, to inject just after the error line. | |
396 | _line_format: f (str) -> (str, bool) |
|
150 | _line_format: f (str) -> (str, bool) | |
397 | return (colorized version of str, failure to do so) |
|
151 | return (colorized version of str, failure to do so) | |
398 | """ |
|
152 | """ | |
399 | numbers_width = INDENT_SIZE - 1 |
|
153 | numbers_width = INDENT_SIZE - 1 | |
400 | res = [] |
|
154 | res = [] | |
401 |
|
155 | |||
402 | for i,line in enumerate(lines, lnum-index): |
|
156 | for stack_line in lines: | |
403 | line = py3compat.cast_unicode(line) |
|
157 | line = stack_line.text.rstrip('\n') + '\n' | |
404 |
|
158 | |||
405 | new_line, err = _line_format(line, 'str') |
|
159 | new_line, err = _line_format(line, 'str') | |
406 | if not err: |
|
160 | if not err: | |
407 | line = new_line |
|
161 | line = new_line | |
408 |
|
162 | |||
409 | if i == lnum: |
|
163 | lineno = stack_line.lineno | |
|
164 | if stack_line.is_current: | |||
410 | # This is the line with the error |
|
165 | # This is the line with the error | |
411 | pad = numbers_width - len(str(i)) |
|
166 | pad = numbers_width - len(str(lineno)) | |
412 |
num = '%s%s' % (debugger.make_arrow(pad), str(l |
|
167 | num = '%s%s' % (debugger.make_arrow(pad), str(lineno)) | |
413 | line = '%s%s%s %s%s' % (Colors.linenoEm, num, |
|
168 | line = '%s%s%s %s%s' % (Colors.linenoEm, num, | |
414 | Colors.line, line, Colors.Normal) |
|
169 | Colors.line, line, Colors.Normal) | |
415 | else: |
|
170 | else: | |
416 | num = '%*s' % (numbers_width, i) |
|
171 | num = '%*s' % (numbers_width, lineno) | |
417 | line = '%s%s%s %s' % (Colors.lineno, num, |
|
172 | line = '%s%s%s %s' % (Colors.lineno, num, | |
418 | Colors.Normal, line) |
|
173 | Colors.Normal, line) | |
419 |
|
174 | |||
420 | res.append(line) |
|
175 | res.append(line) | |
421 |
if lvals and |
|
176 | if lvals and stack_line.is_current: | |
422 | res.append(lvals + '\n') |
|
177 | res.append(lvals + '\n') | |
423 | return res |
|
178 | return res | |
424 |
|
179 | |||
425 | def is_recursion_error(etype, value, records): |
|
|||
426 | try: |
|
|||
427 | # RecursionError is new in Python 3.5 |
|
|||
428 | recursion_error_type = RecursionError |
|
|||
429 | except NameError: |
|
|||
430 | recursion_error_type = RuntimeError |
|
|||
431 |
|
||||
432 | # The default recursion limit is 1000, but some of that will be taken up |
|
|||
433 | # by stack frames in IPython itself. >500 frames probably indicates |
|
|||
434 | # a recursion error. |
|
|||
435 | return (etype is recursion_error_type) \ |
|
|||
436 | and "recursion" in str(value).lower() \ |
|
|||
437 | and len(records) > _FRAME_RECURSION_LIMIT |
|
|||
438 |
|
||||
439 | def find_recursion(etype, value, records): |
|
|||
440 | """Identify the repeating stack frames from a RecursionError traceback |
|
|||
441 |
|
||||
442 | 'records' is a list as returned by VerboseTB.get_records() |
|
|||
443 |
|
||||
444 | Returns (last_unique, repeat_length) |
|
|||
445 | """ |
|
|||
446 | # This involves a bit of guesswork - we want to show enough of the traceback |
|
|||
447 | # to indicate where the recursion is occurring. We guess that the innermost |
|
|||
448 | # quarter of the traceback (250 frames by default) is repeats, and find the |
|
|||
449 | # first frame (from in to out) that looks different. |
|
|||
450 | if not is_recursion_error(etype, value, records): |
|
|||
451 | return len(records), 0 |
|
|||
452 |
|
||||
453 | # Select filename, lineno, func_name to track frames with |
|
|||
454 | records = [r[1:4] for r in records] |
|
|||
455 | inner_frames = records[-(len(records)//4):] |
|
|||
456 | frames_repeated = set(inner_frames) |
|
|||
457 |
|
||||
458 | last_seen_at = {} |
|
|||
459 | longest_repeat = 0 |
|
|||
460 | i = len(records) |
|
|||
461 | for frame in reversed(records): |
|
|||
462 | i -= 1 |
|
|||
463 | if frame not in frames_repeated: |
|
|||
464 | last_unique = i |
|
|||
465 | break |
|
|||
466 |
|
||||
467 | if frame in last_seen_at: |
|
|||
468 | distance = last_seen_at[frame] - i |
|
|||
469 | longest_repeat = max(longest_repeat, distance) |
|
|||
470 |
|
||||
471 | last_seen_at[frame] = i |
|
|||
472 | else: |
|
|||
473 | last_unique = 0 # The whole traceback was recursion |
|
|||
474 |
|
||||
475 | return last_unique, longest_repeat |
|
|||
476 |
|
180 | |||
477 | #--------------------------------------------------------------------------- |
|
181 | #--------------------------------------------------------------------------- | |
478 | # Module classes |
|
182 | # Module classes | |
479 | class TBTools(colorable.Colorable): |
|
183 | class TBTools(colorable.Colorable): | |
480 | """Basic tools used by all traceback printer classes.""" |
|
184 | """Basic tools used by all traceback printer classes.""" | |
481 |
|
185 | |||
482 | # Number of frames to skip when reporting tracebacks |
|
186 | # Number of frames to skip when reporting tracebacks | |
483 | tb_offset = 0 |
|
187 | tb_offset = 0 | |
484 |
|
188 | |||
485 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None): |
|
189 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None): | |
486 | # Whether to call the interactive pdb debugger after printing |
|
190 | # Whether to call the interactive pdb debugger after printing | |
487 | # tracebacks or not |
|
191 | # tracebacks or not | |
488 | super(TBTools, self).__init__(parent=parent, config=config) |
|
192 | super(TBTools, self).__init__(parent=parent, config=config) | |
489 | self.call_pdb = call_pdb |
|
193 | self.call_pdb = call_pdb | |
490 |
|
194 | |||
491 | # Output stream to write to. Note that we store the original value in |
|
195 | # Output stream to write to. Note that we store the original value in | |
492 | # a private attribute and then make the public ostream a property, so |
|
196 | # a private attribute and then make the public ostream a property, so | |
493 | # that we can delay accessing sys.stdout until runtime. The way |
|
197 | # that we can delay accessing sys.stdout until runtime. The way | |
494 | # things are written now, the sys.stdout object is dynamically managed |
|
198 | # things are written now, the sys.stdout object is dynamically managed | |
495 | # so a reference to it should NEVER be stored statically. This |
|
199 | # so a reference to it should NEVER be stored statically. This | |
496 | # property approach confines this detail to a single location, and all |
|
200 | # property approach confines this detail to a single location, and all | |
497 | # subclasses can simply access self.ostream for writing. |
|
201 | # subclasses can simply access self.ostream for writing. | |
498 | self._ostream = ostream |
|
202 | self._ostream = ostream | |
499 |
|
203 | |||
500 | # Create color table |
|
204 | # Create color table | |
501 | self.color_scheme_table = exception_colors() |
|
205 | self.color_scheme_table = exception_colors() | |
502 |
|
206 | |||
503 | self.set_colors(color_scheme) |
|
207 | self.set_colors(color_scheme) | |
504 | self.old_scheme = color_scheme # save initial value for toggles |
|
208 | self.old_scheme = color_scheme # save initial value for toggles | |
505 |
|
209 | |||
506 | if call_pdb: |
|
210 | if call_pdb: | |
507 | self.pdb = debugger.Pdb() |
|
211 | self.pdb = debugger.Pdb() | |
508 | else: |
|
212 | else: | |
509 | self.pdb = None |
|
213 | self.pdb = None | |
510 |
|
214 | |||
511 | def _get_ostream(self): |
|
215 | def _get_ostream(self): | |
512 | """Output stream that exceptions are written to. |
|
216 | """Output stream that exceptions are written to. | |
513 |
|
217 | |||
514 | Valid values are: |
|
218 | Valid values are: | |
515 |
|
219 | |||
516 | - None: the default, which means that IPython will dynamically resolve |
|
220 | - None: the default, which means that IPython will dynamically resolve | |
517 | to sys.stdout. This ensures compatibility with most tools, including |
|
221 | to sys.stdout. This ensures compatibility with most tools, including | |
518 | Windows (where plain stdout doesn't recognize ANSI escapes). |
|
222 | Windows (where plain stdout doesn't recognize ANSI escapes). | |
519 |
|
223 | |||
520 | - Any object with 'write' and 'flush' attributes. |
|
224 | - Any object with 'write' and 'flush' attributes. | |
521 | """ |
|
225 | """ | |
522 | return sys.stdout if self._ostream is None else self._ostream |
|
226 | return sys.stdout if self._ostream is None else self._ostream | |
523 |
|
227 | |||
524 | def _set_ostream(self, val): |
|
228 | def _set_ostream(self, val): | |
525 | assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush')) |
|
229 | assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush')) | |
526 | self._ostream = val |
|
230 | self._ostream = val | |
527 |
|
231 | |||
528 | ostream = property(_get_ostream, _set_ostream) |
|
232 | ostream = property(_get_ostream, _set_ostream) | |
529 |
|
233 | |||
530 | def get_parts_of_chained_exception(self, evalue): |
|
234 | def get_parts_of_chained_exception(self, evalue): | |
531 | def get_chained_exception(exception_value): |
|
235 | def get_chained_exception(exception_value): | |
532 | cause = getattr(exception_value, '__cause__', None) |
|
236 | cause = getattr(exception_value, '__cause__', None) | |
533 | if cause: |
|
237 | if cause: | |
534 | return cause |
|
238 | return cause | |
535 | if getattr(exception_value, '__suppress_context__', False): |
|
239 | if getattr(exception_value, '__suppress_context__', False): | |
536 | return None |
|
240 | return None | |
537 | return getattr(exception_value, '__context__', None) |
|
241 | return getattr(exception_value, '__context__', None) | |
538 |
|
242 | |||
539 | chained_evalue = get_chained_exception(evalue) |
|
243 | chained_evalue = get_chained_exception(evalue) | |
540 |
|
244 | |||
541 | if chained_evalue: |
|
245 | if chained_evalue: | |
542 | return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__ |
|
246 | return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__ | |
543 |
|
247 | |||
544 | def prepare_chained_exception_message(self, cause): |
|
248 | def prepare_chained_exception_message(self, cause): | |
545 | direct_cause = "\nThe above exception was the direct cause of the following exception:\n" |
|
249 | direct_cause = "\nThe above exception was the direct cause of the following exception:\n" | |
546 | exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n" |
|
250 | exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n" | |
547 |
|
251 | |||
548 | if cause: |
|
252 | if cause: | |
549 | message = [[direct_cause]] |
|
253 | message = [[direct_cause]] | |
550 | else: |
|
254 | else: | |
551 | message = [[exception_during_handling]] |
|
255 | message = [[exception_during_handling]] | |
552 | return message |
|
256 | return message | |
553 |
|
257 | |||
554 | def set_colors(self, *args, **kw): |
|
258 | def set_colors(self, *args, **kw): | |
555 | """Shorthand access to the color table scheme selector method.""" |
|
259 | """Shorthand access to the color table scheme selector method.""" | |
556 |
|
260 | |||
557 | # Set own color table |
|
261 | # Set own color table | |
558 | self.color_scheme_table.set_active_scheme(*args, **kw) |
|
262 | self.color_scheme_table.set_active_scheme(*args, **kw) | |
559 | # for convenience, set Colors to the active scheme |
|
263 | # for convenience, set Colors to the active scheme | |
560 | self.Colors = self.color_scheme_table.active_colors |
|
264 | self.Colors = self.color_scheme_table.active_colors | |
561 | # Also set colors of debugger |
|
265 | # Also set colors of debugger | |
562 | if hasattr(self, 'pdb') and self.pdb is not None: |
|
266 | if hasattr(self, 'pdb') and self.pdb is not None: | |
563 | self.pdb.set_colors(*args, **kw) |
|
267 | self.pdb.set_colors(*args, **kw) | |
564 |
|
268 | |||
565 | def color_toggle(self): |
|
269 | def color_toggle(self): | |
566 | """Toggle between the currently active color scheme and NoColor.""" |
|
270 | """Toggle between the currently active color scheme and NoColor.""" | |
567 |
|
271 | |||
568 | if self.color_scheme_table.active_scheme_name == 'NoColor': |
|
272 | if self.color_scheme_table.active_scheme_name == 'NoColor': | |
569 | self.color_scheme_table.set_active_scheme(self.old_scheme) |
|
273 | self.color_scheme_table.set_active_scheme(self.old_scheme) | |
570 | self.Colors = self.color_scheme_table.active_colors |
|
274 | self.Colors = self.color_scheme_table.active_colors | |
571 | else: |
|
275 | else: | |
572 | self.old_scheme = self.color_scheme_table.active_scheme_name |
|
276 | self.old_scheme = self.color_scheme_table.active_scheme_name | |
573 | self.color_scheme_table.set_active_scheme('NoColor') |
|
277 | self.color_scheme_table.set_active_scheme('NoColor') | |
574 | self.Colors = self.color_scheme_table.active_colors |
|
278 | self.Colors = self.color_scheme_table.active_colors | |
575 |
|
279 | |||
576 | def stb2text(self, stb): |
|
280 | def stb2text(self, stb): | |
577 | """Convert a structured traceback (a list) to a string.""" |
|
281 | """Convert a structured traceback (a list) to a string.""" | |
578 | return '\n'.join(stb) |
|
282 | return '\n'.join(stb) | |
579 |
|
283 | |||
580 | def text(self, etype, value, tb, tb_offset=None, context=5): |
|
284 | def text(self, etype, value, tb, tb_offset=None, context=5): | |
581 | """Return formatted traceback. |
|
285 | """Return formatted traceback. | |
582 |
|
286 | |||
583 | Subclasses may override this if they add extra arguments. |
|
287 | Subclasses may override this if they add extra arguments. | |
584 | """ |
|
288 | """ | |
585 | tb_list = self.structured_traceback(etype, value, tb, |
|
289 | tb_list = self.structured_traceback(etype, value, tb, | |
586 | tb_offset, context) |
|
290 | tb_offset, context) | |
587 | return self.stb2text(tb_list) |
|
291 | return self.stb2text(tb_list) | |
588 |
|
292 | |||
589 | def structured_traceback(self, etype, evalue, tb, tb_offset=None, |
|
293 | def structured_traceback(self, etype, evalue, tb, tb_offset=None, | |
590 | context=5, mode=None): |
|
294 | context=5, mode=None): | |
591 | """Return a list of traceback frames. |
|
295 | """Return a list of traceback frames. | |
592 |
|
296 | |||
593 | Must be implemented by each class. |
|
297 | Must be implemented by each class. | |
594 | """ |
|
298 | """ | |
595 | raise NotImplementedError() |
|
299 | raise NotImplementedError() | |
596 |
|
300 | |||
597 |
|
301 | |||
598 | #--------------------------------------------------------------------------- |
|
302 | #--------------------------------------------------------------------------- | |
599 | class ListTB(TBTools): |
|
303 | class ListTB(TBTools): | |
600 | """Print traceback information from a traceback list, with optional color. |
|
304 | """Print traceback information from a traceback list, with optional color. | |
601 |
|
305 | |||
602 | Calling requires 3 arguments: (etype, evalue, elist) |
|
306 | Calling requires 3 arguments: (etype, evalue, elist) | |
603 | as would be obtained by:: |
|
307 | as would be obtained by:: | |
604 |
|
308 | |||
605 | etype, evalue, tb = sys.exc_info() |
|
309 | etype, evalue, tb = sys.exc_info() | |
606 | if tb: |
|
310 | if tb: | |
607 | elist = traceback.extract_tb(tb) |
|
311 | elist = traceback.extract_tb(tb) | |
608 | else: |
|
312 | else: | |
609 | elist = None |
|
313 | elist = None | |
610 |
|
314 | |||
611 | It can thus be used by programs which need to process the traceback before |
|
315 | It can thus be used by programs which need to process the traceback before | |
612 | printing (such as console replacements based on the code module from the |
|
316 | printing (such as console replacements based on the code module from the | |
613 | standard library). |
|
317 | standard library). | |
614 |
|
318 | |||
615 | Because they are meant to be called without a full traceback (only a |
|
319 | Because they are meant to be called without a full traceback (only a | |
616 | list), instances of this class can't call the interactive pdb debugger.""" |
|
320 | list), instances of this class can't call the interactive pdb debugger.""" | |
617 |
|
321 | |||
618 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None): |
|
322 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None): | |
619 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, |
|
323 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, | |
620 | ostream=ostream, parent=parent,config=config) |
|
324 | ostream=ostream, parent=parent,config=config) | |
621 |
|
325 | |||
622 | def __call__(self, etype, value, elist): |
|
326 | def __call__(self, etype, value, elist): | |
623 | self.ostream.flush() |
|
327 | self.ostream.flush() | |
624 | self.ostream.write(self.text(etype, value, elist)) |
|
328 | self.ostream.write(self.text(etype, value, elist)) | |
625 | self.ostream.write('\n') |
|
329 | self.ostream.write('\n') | |
626 |
|
330 | |||
627 | def _extract_tb(self, tb): |
|
331 | def _extract_tb(self, tb): | |
628 | if tb: |
|
332 | if tb: | |
629 | return traceback.extract_tb(tb) |
|
333 | return traceback.extract_tb(tb) | |
630 | else: |
|
334 | else: | |
631 | return None |
|
335 | return None | |
632 |
|
336 | |||
633 | def structured_traceback(self, etype, evalue, etb=None, tb_offset=None, |
|
337 | def structured_traceback(self, etype, evalue, etb=None, tb_offset=None, | |
634 | context=5): |
|
338 | context=5): | |
635 | """Return a color formatted string with the traceback info. |
|
339 | """Return a color formatted string with the traceback info. | |
636 |
|
340 | |||
637 | Parameters |
|
341 | Parameters | |
638 | ---------- |
|
342 | ---------- | |
639 | etype : exception type |
|
343 | etype : exception type | |
640 | Type of the exception raised. |
|
344 | Type of the exception raised. | |
641 |
|
345 | |||
642 | evalue : object |
|
346 | evalue : object | |
643 | Data stored in the exception |
|
347 | Data stored in the exception | |
644 |
|
348 | |||
645 | etb : object |
|
349 | etb : object | |
646 | If list: List of frames, see class docstring for details. |
|
350 | If list: List of frames, see class docstring for details. | |
647 | If Traceback: Traceback of the exception. |
|
351 | If Traceback: Traceback of the exception. | |
648 |
|
352 | |||
649 | tb_offset : int, optional |
|
353 | tb_offset : int, optional | |
650 | Number of frames in the traceback to skip. If not given, the |
|
354 | Number of frames in the traceback to skip. If not given, the | |
651 | instance evalue is used (set in constructor). |
|
355 | instance evalue is used (set in constructor). | |
652 |
|
356 | |||
653 | context : int, optional |
|
357 | context : int, optional | |
654 | Number of lines of context information to print. |
|
358 | Number of lines of context information to print. | |
655 |
|
359 | |||
656 | Returns |
|
360 | Returns | |
657 | ------- |
|
361 | ------- | |
658 | String with formatted exception. |
|
362 | String with formatted exception. | |
659 | """ |
|
363 | """ | |
660 | # This is a workaround to get chained_exc_ids in recursive calls |
|
364 | # This is a workaround to get chained_exc_ids in recursive calls | |
661 | # etb should not be a tuple if structured_traceback is not recursive |
|
365 | # etb should not be a tuple if structured_traceback is not recursive | |
662 | if isinstance(etb, tuple): |
|
366 | if isinstance(etb, tuple): | |
663 | etb, chained_exc_ids = etb |
|
367 | etb, chained_exc_ids = etb | |
664 | else: |
|
368 | else: | |
665 | chained_exc_ids = set() |
|
369 | chained_exc_ids = set() | |
666 |
|
370 | |||
667 | if isinstance(etb, list): |
|
371 | if isinstance(etb, list): | |
668 | elist = etb |
|
372 | elist = etb | |
669 | elif etb is not None: |
|
373 | elif etb is not None: | |
670 | elist = self._extract_tb(etb) |
|
374 | elist = self._extract_tb(etb) | |
671 | else: |
|
375 | else: | |
672 | elist = [] |
|
376 | elist = [] | |
673 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
377 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
674 | Colors = self.Colors |
|
378 | Colors = self.Colors | |
675 | out_list = [] |
|
379 | out_list = [] | |
676 | if elist: |
|
380 | if elist: | |
677 |
|
381 | |||
678 | if tb_offset and len(elist) > tb_offset: |
|
382 | if tb_offset and len(elist) > tb_offset: | |
679 | elist = elist[tb_offset:] |
|
383 | elist = elist[tb_offset:] | |
680 |
|
384 | |||
681 | out_list.append('Traceback %s(most recent call last)%s:' % |
|
385 | out_list.append('Traceback %s(most recent call last)%s:' % | |
682 | (Colors.normalEm, Colors.Normal) + '\n') |
|
386 | (Colors.normalEm, Colors.Normal) + '\n') | |
683 | out_list.extend(self._format_list(elist)) |
|
387 | out_list.extend(self._format_list(elist)) | |
684 | # The exception info should be a single entry in the list. |
|
388 | # The exception info should be a single entry in the list. | |
685 | lines = ''.join(self._format_exception_only(etype, evalue)) |
|
389 | lines = ''.join(self._format_exception_only(etype, evalue)) | |
686 | out_list.append(lines) |
|
390 | out_list.append(lines) | |
687 |
|
391 | |||
688 | exception = self.get_parts_of_chained_exception(evalue) |
|
392 | exception = self.get_parts_of_chained_exception(evalue) | |
689 |
|
393 | |||
690 | if exception and not id(exception[1]) in chained_exc_ids: |
|
394 | if exception and not id(exception[1]) in chained_exc_ids: | |
691 | chained_exception_message = self.prepare_chained_exception_message( |
|
395 | chained_exception_message = self.prepare_chained_exception_message( | |
692 | evalue.__cause__)[0] |
|
396 | evalue.__cause__)[0] | |
693 | etype, evalue, etb = exception |
|
397 | etype, evalue, etb = exception | |
694 | # Trace exception to avoid infinite 'cause' loop |
|
398 | # Trace exception to avoid infinite 'cause' loop | |
695 | chained_exc_ids.add(id(exception[1])) |
|
399 | chained_exc_ids.add(id(exception[1])) | |
696 | chained_exceptions_tb_offset = 0 |
|
400 | chained_exceptions_tb_offset = 0 | |
697 | out_list = ( |
|
401 | out_list = ( | |
698 | self.structured_traceback( |
|
402 | self.structured_traceback( | |
699 | etype, evalue, (etb, chained_exc_ids), |
|
403 | etype, evalue, (etb, chained_exc_ids), | |
700 | chained_exceptions_tb_offset, context) |
|
404 | chained_exceptions_tb_offset, context) | |
701 | + chained_exception_message |
|
405 | + chained_exception_message | |
702 | + out_list) |
|
406 | + out_list) | |
703 |
|
407 | |||
704 | return out_list |
|
408 | return out_list | |
705 |
|
409 | |||
706 | def _format_list(self, extracted_list): |
|
410 | def _format_list(self, extracted_list): | |
707 | """Format a list of traceback entry tuples for printing. |
|
411 | """Format a list of traceback entry tuples for printing. | |
708 |
|
412 | |||
709 | Given a list of tuples as returned by extract_tb() or |
|
413 | Given a list of tuples as returned by extract_tb() or | |
710 | extract_stack(), return a list of strings ready for printing. |
|
414 | extract_stack(), return a list of strings ready for printing. | |
711 | Each string in the resulting list corresponds to the item with the |
|
415 | Each string in the resulting list corresponds to the item with the | |
712 | same index in the argument list. Each string ends in a newline; |
|
416 | same index in the argument list. Each string ends in a newline; | |
713 | the strings may contain internal newlines as well, for those items |
|
417 | the strings may contain internal newlines as well, for those items | |
714 | whose source text line is not None. |
|
418 | whose source text line is not None. | |
715 |
|
419 | |||
716 | Lifted almost verbatim from traceback.py |
|
420 | Lifted almost verbatim from traceback.py | |
717 | """ |
|
421 | """ | |
718 |
|
422 | |||
719 | Colors = self.Colors |
|
423 | Colors = self.Colors | |
720 | list = [] |
|
424 | list = [] | |
721 | for filename, lineno, name, line in extracted_list[:-1]: |
|
425 | for filename, lineno, name, line in extracted_list[:-1]: | |
722 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ |
|
426 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ | |
723 | (Colors.filename, filename, Colors.Normal, |
|
427 | (Colors.filename, filename, Colors.Normal, | |
724 | Colors.lineno, lineno, Colors.Normal, |
|
428 | Colors.lineno, lineno, Colors.Normal, | |
725 | Colors.name, name, Colors.Normal) |
|
429 | Colors.name, name, Colors.Normal) | |
726 | if line: |
|
430 | if line: | |
727 | item += ' %s\n' % line.strip() |
|
431 | item += ' %s\n' % line.strip() | |
728 | list.append(item) |
|
432 | list.append(item) | |
729 | # Emphasize the last entry |
|
433 | # Emphasize the last entry | |
730 | filename, lineno, name, line = extracted_list[-1] |
|
434 | filename, lineno, name, line = extracted_list[-1] | |
731 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ |
|
435 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ | |
732 | (Colors.normalEm, |
|
436 | (Colors.normalEm, | |
733 | Colors.filenameEm, filename, Colors.normalEm, |
|
437 | Colors.filenameEm, filename, Colors.normalEm, | |
734 | Colors.linenoEm, lineno, Colors.normalEm, |
|
438 | Colors.linenoEm, lineno, Colors.normalEm, | |
735 | Colors.nameEm, name, Colors.normalEm, |
|
439 | Colors.nameEm, name, Colors.normalEm, | |
736 | Colors.Normal) |
|
440 | Colors.Normal) | |
737 | if line: |
|
441 | if line: | |
738 | item += '%s %s%s\n' % (Colors.line, line.strip(), |
|
442 | item += '%s %s%s\n' % (Colors.line, line.strip(), | |
739 | Colors.Normal) |
|
443 | Colors.Normal) | |
740 | list.append(item) |
|
444 | list.append(item) | |
741 | return list |
|
445 | return list | |
742 |
|
446 | |||
743 | def _format_exception_only(self, etype, value): |
|
447 | def _format_exception_only(self, etype, value): | |
744 | """Format the exception part of a traceback. |
|
448 | """Format the exception part of a traceback. | |
745 |
|
449 | |||
746 | The arguments are the exception type and value such as given by |
|
450 | The arguments are the exception type and value such as given by | |
747 | sys.exc_info()[:2]. The return value is a list of strings, each ending |
|
451 | sys.exc_info()[:2]. The return value is a list of strings, each ending | |
748 | in a newline. Normally, the list contains a single string; however, |
|
452 | in a newline. Normally, the list contains a single string; however, | |
749 | for SyntaxError exceptions, it contains several lines that (when |
|
453 | for SyntaxError exceptions, it contains several lines that (when | |
750 | printed) display detailed information about where the syntax error |
|
454 | printed) display detailed information about where the syntax error | |
751 | occurred. The message indicating which exception occurred is the |
|
455 | occurred. The message indicating which exception occurred is the | |
752 | always last string in the list. |
|
456 | always last string in the list. | |
753 |
|
457 | |||
754 | Also lifted nearly verbatim from traceback.py |
|
458 | Also lifted nearly verbatim from traceback.py | |
755 | """ |
|
459 | """ | |
756 | have_filedata = False |
|
460 | have_filedata = False | |
757 | Colors = self.Colors |
|
461 | Colors = self.Colors | |
758 | list = [] |
|
462 | list = [] | |
759 | stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal) |
|
463 | stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal) | |
760 | if value is None: |
|
464 | if value is None: | |
761 | # Not sure if this can still happen in Python 2.6 and above |
|
465 | # Not sure if this can still happen in Python 2.6 and above | |
762 | list.append(stype + '\n') |
|
466 | list.append(stype + '\n') | |
763 | else: |
|
467 | else: | |
764 | if issubclass(etype, SyntaxError): |
|
468 | if issubclass(etype, SyntaxError): | |
765 | have_filedata = True |
|
469 | have_filedata = True | |
766 | if not value.filename: value.filename = "<string>" |
|
470 | if not value.filename: value.filename = "<string>" | |
767 | if value.lineno: |
|
471 | if value.lineno: | |
768 | lineno = value.lineno |
|
472 | lineno = value.lineno | |
769 | textline = linecache.getline(value.filename, value.lineno) |
|
473 | textline = linecache.getline(value.filename, value.lineno) | |
770 | else: |
|
474 | else: | |
771 | lineno = 'unknown' |
|
475 | lineno = 'unknown' | |
772 | textline = '' |
|
476 | textline = '' | |
773 | list.append('%s File %s"%s"%s, line %s%s%s\n' % \ |
|
477 | list.append('%s File %s"%s"%s, line %s%s%s\n' % \ | |
774 | (Colors.normalEm, |
|
478 | (Colors.normalEm, | |
775 | Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm, |
|
479 | Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm, | |
776 | Colors.linenoEm, lineno, Colors.Normal )) |
|
480 | Colors.linenoEm, lineno, Colors.Normal )) | |
777 | if textline == '': |
|
481 | if textline == '': | |
778 | textline = py3compat.cast_unicode(value.text, "utf-8") |
|
482 | textline = py3compat.cast_unicode(value.text, "utf-8") | |
779 |
|
483 | |||
780 | if textline is not None: |
|
484 | if textline is not None: | |
781 | i = 0 |
|
485 | i = 0 | |
782 | while i < len(textline) and textline[i].isspace(): |
|
486 | while i < len(textline) and textline[i].isspace(): | |
783 | i += 1 |
|
487 | i += 1 | |
784 | list.append('%s %s%s\n' % (Colors.line, |
|
488 | list.append('%s %s%s\n' % (Colors.line, | |
785 | textline.strip(), |
|
489 | textline.strip(), | |
786 | Colors.Normal)) |
|
490 | Colors.Normal)) | |
787 | if value.offset is not None: |
|
491 | if value.offset is not None: | |
788 | s = ' ' |
|
492 | s = ' ' | |
789 | for c in textline[i:value.offset - 1]: |
|
493 | for c in textline[i:value.offset - 1]: | |
790 | if c.isspace(): |
|
494 | if c.isspace(): | |
791 | s += c |
|
495 | s += c | |
792 | else: |
|
496 | else: | |
793 | s += ' ' |
|
497 | s += ' ' | |
794 | list.append('%s%s^%s\n' % (Colors.caret, s, |
|
498 | list.append('%s%s^%s\n' % (Colors.caret, s, | |
795 | Colors.Normal)) |
|
499 | Colors.Normal)) | |
796 |
|
500 | |||
797 | try: |
|
501 | try: | |
798 | s = value.msg |
|
502 | s = value.msg | |
799 | except Exception: |
|
503 | except Exception: | |
800 | s = self._some_str(value) |
|
504 | s = self._some_str(value) | |
801 | if s: |
|
505 | if s: | |
802 | list.append('%s%s:%s %s\n' % (stype, Colors.excName, |
|
506 | list.append('%s%s:%s %s\n' % (stype, Colors.excName, | |
803 | Colors.Normal, s)) |
|
507 | Colors.Normal, s)) | |
804 | else: |
|
508 | else: | |
805 | list.append('%s\n' % stype) |
|
509 | list.append('%s\n' % stype) | |
806 |
|
510 | |||
807 | # sync with user hooks |
|
511 | # sync with user hooks | |
808 | if have_filedata: |
|
512 | if have_filedata: | |
809 | ipinst = get_ipython() |
|
513 | ipinst = get_ipython() | |
810 | if ipinst is not None: |
|
514 | if ipinst is not None: | |
811 | ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0) |
|
515 | ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0) | |
812 |
|
516 | |||
813 | return list |
|
517 | return list | |
814 |
|
518 | |||
815 | def get_exception_only(self, etype, value): |
|
519 | def get_exception_only(self, etype, value): | |
816 | """Only print the exception type and message, without a traceback. |
|
520 | """Only print the exception type and message, without a traceback. | |
817 |
|
521 | |||
818 | Parameters |
|
522 | Parameters | |
819 | ---------- |
|
523 | ---------- | |
820 | etype : exception type |
|
524 | etype : exception type | |
821 | value : exception value |
|
525 | value : exception value | |
822 | """ |
|
526 | """ | |
823 | return ListTB.structured_traceback(self, etype, value) |
|
527 | return ListTB.structured_traceback(self, etype, value) | |
824 |
|
528 | |||
825 | def show_exception_only(self, etype, evalue): |
|
529 | def show_exception_only(self, etype, evalue): | |
826 | """Only print the exception type and message, without a traceback. |
|
530 | """Only print the exception type and message, without a traceback. | |
827 |
|
531 | |||
828 | Parameters |
|
532 | Parameters | |
829 | ---------- |
|
533 | ---------- | |
830 | etype : exception type |
|
534 | etype : exception type | |
831 | value : exception value |
|
535 | value : exception value | |
832 | """ |
|
536 | """ | |
833 | # This method needs to use __call__ from *this* class, not the one from |
|
537 | # This method needs to use __call__ from *this* class, not the one from | |
834 | # a subclass whose signature or behavior may be different |
|
538 | # a subclass whose signature or behavior may be different | |
835 | ostream = self.ostream |
|
539 | ostream = self.ostream | |
836 | ostream.flush() |
|
540 | ostream.flush() | |
837 | ostream.write('\n'.join(self.get_exception_only(etype, evalue))) |
|
541 | ostream.write('\n'.join(self.get_exception_only(etype, evalue))) | |
838 | ostream.flush() |
|
542 | ostream.flush() | |
839 |
|
543 | |||
840 | def _some_str(self, value): |
|
544 | def _some_str(self, value): | |
841 | # Lifted from traceback.py |
|
545 | # Lifted from traceback.py | |
842 | try: |
|
546 | try: | |
843 | return py3compat.cast_unicode(str(value)) |
|
547 | return py3compat.cast_unicode(str(value)) | |
844 | except: |
|
548 | except: | |
845 | return u'<unprintable %s object>' % type(value).__name__ |
|
549 | return u'<unprintable %s object>' % type(value).__name__ | |
846 |
|
550 | |||
847 |
|
551 | |||
848 | #---------------------------------------------------------------------------- |
|
552 | #---------------------------------------------------------------------------- | |
849 | class VerboseTB(TBTools): |
|
553 | class VerboseTB(TBTools): | |
850 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead |
|
554 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead | |
851 | of HTML. Requires inspect and pydoc. Crazy, man. |
|
555 | of HTML. Requires inspect and pydoc. Crazy, man. | |
852 |
|
556 | |||
853 | Modified version which optionally strips the topmost entries from the |
|
557 | Modified version which optionally strips the topmost entries from the | |
854 | traceback, to be used with alternate interpreters (because their own code |
|
558 | traceback, to be used with alternate interpreters (because their own code | |
855 | would appear in the traceback).""" |
|
559 | would appear in the traceback).""" | |
856 |
|
560 | |||
857 | def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None, |
|
561 | def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None, | |
858 | tb_offset=0, long_header=False, include_vars=True, |
|
562 | tb_offset=0, long_header=False, include_vars=True, | |
859 | check_cache=None, debugger_cls = None, |
|
563 | check_cache=None, debugger_cls = None, | |
860 | parent=None, config=None): |
|
564 | parent=None, config=None): | |
861 | """Specify traceback offset, headers and color scheme. |
|
565 | """Specify traceback offset, headers and color scheme. | |
862 |
|
566 | |||
863 | Define how many frames to drop from the tracebacks. Calling it with |
|
567 | Define how many frames to drop from the tracebacks. Calling it with | |
864 | tb_offset=1 allows use of this handler in interpreters which will have |
|
568 | tb_offset=1 allows use of this handler in interpreters which will have | |
865 | their own code at the top of the traceback (VerboseTB will first |
|
569 | their own code at the top of the traceback (VerboseTB will first | |
866 | remove that frame before printing the traceback info).""" |
|
570 | remove that frame before printing the traceback info).""" | |
867 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, |
|
571 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, | |
868 | ostream=ostream, parent=parent, config=config) |
|
572 | ostream=ostream, parent=parent, config=config) | |
869 | self.tb_offset = tb_offset |
|
573 | self.tb_offset = tb_offset | |
870 | self.long_header = long_header |
|
574 | self.long_header = long_header | |
871 | self.include_vars = include_vars |
|
575 | self.include_vars = include_vars | |
872 | # By default we use linecache.checkcache, but the user can provide a |
|
576 | # By default we use linecache.checkcache, but the user can provide a | |
873 | # different check_cache implementation. This is used by the IPython |
|
577 | # different check_cache implementation. This is used by the IPython | |
874 | # kernel to provide tracebacks for interactive code that is cached, |
|
578 | # kernel to provide tracebacks for interactive code that is cached, | |
875 | # by a compiler instance that flushes the linecache but preserves its |
|
579 | # by a compiler instance that flushes the linecache but preserves its | |
876 | # own code cache. |
|
580 | # own code cache. | |
877 | if check_cache is None: |
|
581 | if check_cache is None: | |
878 | check_cache = linecache.checkcache |
|
582 | check_cache = linecache.checkcache | |
879 | self.check_cache = check_cache |
|
583 | self.check_cache = check_cache | |
880 |
|
584 | |||
881 | self.debugger_cls = debugger_cls or debugger.Pdb |
|
585 | self.debugger_cls = debugger_cls or debugger.Pdb | |
882 |
|
586 | |||
883 |
def format_record |
|
587 | def format_record(self, frame_info): | |
884 | """Format the stack frames of the traceback""" |
|
|||
885 | frames = [] |
|
|||
886 | for r in records[:last_unique+recursion_repeat+1]: |
|
|||
887 | #print '*** record:',file,lnum,func,lines,index # dbg |
|
|||
888 | frames.append(self.format_record(*r)) |
|
|||
889 |
|
||||
890 | if recursion_repeat: |
|
|||
891 | frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat) |
|
|||
892 | frames.append(self.format_record(*records[last_unique+recursion_repeat+1])) |
|
|||
893 |
|
||||
894 | return frames |
|
|||
895 |
|
||||
896 | def format_record(self, frame, file, lnum, func, lines, index): |
|
|||
897 | """Format a single stack frame""" |
|
588 | """Format a single stack frame""" | |
898 | Colors = self.Colors # just a shorthand + quicker name lookup |
|
589 | Colors = self.Colors # just a shorthand + quicker name lookup | |
899 | ColorsNormal = Colors.Normal # used a lot |
|
590 | ColorsNormal = Colors.Normal # used a lot | |
900 | col_scheme = self.color_scheme_table.active_scheme_name |
|
591 | col_scheme = self.color_scheme_table.active_scheme_name | |
901 | indent = ' ' * INDENT_SIZE |
|
592 | indent = ' ' * INDENT_SIZE | |
902 | em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal) |
|
593 | em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal) | |
903 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) |
|
|||
904 | tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal) |
|
594 | tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal) | |
905 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, |
|
595 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, | |
906 | ColorsNormal) |
|
596 | ColorsNormal) | |
907 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ |
|
597 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ | |
908 | (Colors.vName, Colors.valEm, ColorsNormal) |
|
598 | (Colors.vName, Colors.valEm, ColorsNormal) | |
909 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) |
|
599 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) | |
910 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, |
|
|||
911 | Colors.vName, ColorsNormal) |
|
|||
912 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) |
|
600 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) | |
913 |
|
601 | |||
914 | if not file: |
|
602 | file = frame_info.filename | |
915 | file = '?' |
|
|||
916 | elif file.startswith(str("<")) and file.endswith(str(">")): |
|
|||
917 | # Not a real filename, no problem... |
|
|||
918 | pass |
|
|||
919 | elif not os.path.isabs(file): |
|
|||
920 | # Try to make the filename absolute by trying all |
|
|||
921 | # sys.path entries (which is also what linecache does) |
|
|||
922 | for dirname in sys.path: |
|
|||
923 | try: |
|
|||
924 | fullname = os.path.join(dirname, file) |
|
|||
925 | if os.path.isfile(fullname): |
|
|||
926 | file = os.path.abspath(fullname) |
|
|||
927 | break |
|
|||
928 | except Exception: |
|
|||
929 | # Just in case that sys.path contains very |
|
|||
930 | # strange entries... |
|
|||
931 | pass |
|
|||
932 |
|
||||
933 | file = py3compat.cast_unicode(file, util_path.fs_encoding) |
|
603 | file = py3compat.cast_unicode(file, util_path.fs_encoding) | |
934 | link = tpl_link % util_path.compress_user(file) |
|
604 | link = tpl_link % util_path.compress_user(file) | |
935 | args, varargs, varkw, locals_ = inspect.getargvalues(frame) |
|
605 | args, varargs, varkw, locals_ = inspect.getargvalues(frame_info.frame) | |
936 |
|
606 | |||
937 | if func == '?': |
|
607 | func = frame_info.executing.code_qualname() | |
938 | call = '' |
|
608 | if func == '<module>': | |
939 | elif func == '<module>': |
|
|||
940 | call = tpl_call % (func, '') |
|
609 | call = tpl_call % (func, '') | |
941 | else: |
|
610 | else: | |
942 | # Decide whether to include variable details or not |
|
611 | # Decide whether to include variable details or not | |
943 | var_repr = eqrepr if self.include_vars else nullrepr |
|
612 | var_repr = eqrepr if self.include_vars else nullrepr | |
944 | try: |
|
613 | try: | |
945 | call = tpl_call % (func, inspect.formatargvalues(args, |
|
614 | call = tpl_call % (func, inspect.formatargvalues(args, | |
946 | varargs, varkw, |
|
615 | varargs, varkw, | |
947 | locals_, formatvalue=var_repr)) |
|
616 | locals_, formatvalue=var_repr)) | |
948 | except KeyError: |
|
617 | except KeyError: | |
949 | # This happens in situations like errors inside generator |
|
618 | # This happens in situations like errors inside generator | |
950 | # expressions, where local variables are listed in the |
|
619 | # expressions, where local variables are listed in the | |
951 | # line, but can't be extracted from the frame. I'm not |
|
620 | # line, but can't be extracted from the frame. I'm not | |
952 | # 100% sure this isn't actually a bug in inspect itself, |
|
621 | # 100% sure this isn't actually a bug in inspect itself, | |
953 | # but since there's no info for us to compute with, the |
|
622 | # but since there's no info for us to compute with, the | |
954 | # best we can do is report the failure and move on. Here |
|
623 | # best we can do is report the failure and move on. Here | |
955 | # we must *not* call any traceback construction again, |
|
624 | # we must *not* call any traceback construction again, | |
956 | # because that would mess up use of %debug later on. So we |
|
625 | # because that would mess up use of %debug later on. So we | |
957 | # simply report the failure and move on. The only |
|
626 | # simply report the failure and move on. The only | |
958 | # limitation will be that this frame won't have locals |
|
627 | # limitation will be that this frame won't have locals | |
959 | # listed in the call signature. Quite subtle problem... |
|
628 | # listed in the call signature. Quite subtle problem... | |
960 | # I can't think of a good way to validate this in a unit |
|
629 | # I can't think of a good way to validate this in a unit | |
961 | # test, but running a script consisting of: |
|
630 | # test, but running a script consisting of: | |
962 | # dict( (k,v.strip()) for (k,v) in range(10) ) |
|
631 | # dict( (k,v.strip()) for (k,v) in range(10) ) | |
963 | # will illustrate the error, if this exception catch is |
|
632 | # will illustrate the error, if this exception catch is | |
964 | # disabled. |
|
633 | # disabled. | |
965 | call = tpl_call_fail % func |
|
634 | call = tpl_call_fail % func | |
966 |
|
635 | |||
967 | # Don't attempt to tokenize binary files. |
|
|||
968 | if file.endswith(('.so', '.pyd', '.dll')): |
|
|||
969 | return '%s %s\n' % (link, call) |
|
|||
970 |
|
||||
971 | elif file.endswith(('.pyc', '.pyo')): |
|
|||
972 | # Look up the corresponding source file. |
|
|||
973 | try: |
|
|||
974 | file = source_from_cache(file) |
|
|||
975 | except ValueError: |
|
|||
976 | # Failed to get the source file for some reason |
|
|||
977 | # E.g. https://github.com/ipython/ipython/issues/9486 |
|
|||
978 | return '%s %s\n' % (link, call) |
|
|||
979 |
|
||||
980 | def linereader(file=file, lnum=[lnum], getline=linecache.getline): |
|
|||
981 | line = getline(file, lnum[0]) |
|
|||
982 | lnum[0] += 1 |
|
|||
983 | return line |
|
|||
984 |
|
||||
985 | # Build the list of names on this line of code where the exception |
|
|||
986 | # occurred. |
|
|||
987 | try: |
|
|||
988 | names = [] |
|
|||
989 | name_cont = False |
|
|||
990 |
|
||||
991 | for token_type, token, start, end, line in generate_tokens(linereader): |
|
|||
992 | # build composite names |
|
|||
993 | if token_type == tokenize.NAME and token not in keyword.kwlist: |
|
|||
994 | if name_cont: |
|
|||
995 | # Continuation of a dotted name |
|
|||
996 | try: |
|
|||
997 | names[-1].append(token) |
|
|||
998 | except IndexError: |
|
|||
999 | names.append([token]) |
|
|||
1000 | name_cont = False |
|
|||
1001 | else: |
|
|||
1002 | # Regular new names. We append everything, the caller |
|
|||
1003 | # will be responsible for pruning the list later. It's |
|
|||
1004 | # very tricky to try to prune as we go, b/c composite |
|
|||
1005 | # names can fool us. The pruning at the end is easy |
|
|||
1006 | # to do (or the caller can print a list with repeated |
|
|||
1007 | # names if so desired. |
|
|||
1008 | names.append([token]) |
|
|||
1009 | elif token == '.': |
|
|||
1010 | name_cont = True |
|
|||
1011 | elif token_type == tokenize.NEWLINE: |
|
|||
1012 | break |
|
|||
1013 |
|
||||
1014 | except (IndexError, UnicodeDecodeError, SyntaxError): |
|
|||
1015 | # signals exit of tokenizer |
|
|||
1016 | # SyntaxError can occur if the file is not actually Python |
|
|||
1017 | # - see gh-6300 |
|
|||
1018 | pass |
|
|||
1019 | except tokenize.TokenError as msg: |
|
|||
1020 | # Tokenizing may fail for various reasons, many of which are |
|
|||
1021 | # harmless. (A good example is when the line in question is the |
|
|||
1022 | # close of a triple-quoted string, cf gh-6864). We don't want to |
|
|||
1023 | # show this to users, but want make it available for debugging |
|
|||
1024 | # purposes. |
|
|||
1025 | _m = ("An unexpected error occurred while tokenizing input\n" |
|
|||
1026 | "The following traceback may be corrupted or invalid\n" |
|
|||
1027 | "The error message is: %s\n" % msg) |
|
|||
1028 | debug(_m) |
|
|||
1029 |
|
||||
1030 | # Join composite names (e.g. "dict.fromkeys") |
|
|||
1031 | names = ['.'.join(n) for n in names] |
|
|||
1032 | # prune names list of duplicates, but keep the right order |
|
|||
1033 | unique_names = uniq_stable(names) |
|
|||
1034 |
|
||||
1035 | # Start loop over vars |
|
|||
1036 | lvals = '' |
|
636 | lvals = '' | |
1037 | lvals_list = [] |
|
637 | lvals_list = [] | |
1038 | if self.include_vars: |
|
638 | if self.include_vars: | |
1039 | for name_full in unique_names: |
|
639 | for var in frame_info.variables_in_executing_piece: | |
1040 | name_base = name_full.split('.', 1)[0] |
|
640 | lvals_list.append(tpl_name_val % (var.name, var.value)) | |
1041 | if name_base in frame.f_code.co_varnames: |
|
|||
1042 | if name_base in locals_: |
|
|||
1043 | try: |
|
|||
1044 | value = repr(eval(name_full, locals_)) |
|
|||
1045 | except: |
|
|||
1046 | value = undefined |
|
|||
1047 | else: |
|
|||
1048 | value = undefined |
|
|||
1049 | name = tpl_local_var % name_full |
|
|||
1050 | else: |
|
|||
1051 | if name_base in frame.f_globals: |
|
|||
1052 | try: |
|
|||
1053 | value = repr(eval(name_full, frame.f_globals)) |
|
|||
1054 | except: |
|
|||
1055 | value = undefined |
|
|||
1056 | else: |
|
|||
1057 | value = undefined |
|
|||
1058 | name = tpl_global_var % name_full |
|
|||
1059 | lvals_list.append(tpl_name_val % (name, value)) |
|
|||
1060 | if lvals_list: |
|
641 | if lvals_list: | |
1061 | lvals = '%s%s' % (indent, em_normal.join(lvals_list)) |
|
642 | lvals = '%s%s' % (indent, em_normal.join(lvals_list)) | |
1062 |
|
643 | |||
1063 |
|
|
644 | result = '%s %s\n' % (link, call) | |
1064 |
|
645 | |||
1065 | if index is None: |
|
|||
1066 | return level |
|
|||
1067 | else: |
|
|||
1068 |
|
|
646 | _line_format = PyColorize.Parser(style=col_scheme, parent=self).format2 | |
1069 | return '%s%s' % (level, ''.join( |
|
647 | result += ''.join(_format_traceback_lines(frame_info.lines, Colors, lvals, _line_format)) | |
1070 | _format_traceback_lines(lnum, index, lines, Colors, lvals, |
|
648 | return result | |
1071 | _line_format))) |
|
|||
1072 |
|
649 | |||
1073 | def prepare_header(self, etype, long_version=False): |
|
650 | def prepare_header(self, etype, long_version=False): | |
1074 | colors = self.Colors # just a shorthand + quicker name lookup |
|
651 | colors = self.Colors # just a shorthand + quicker name lookup | |
1075 | colorsnormal = colors.Normal # used a lot |
|
652 | colorsnormal = colors.Normal # used a lot | |
1076 | exc = '%s%s%s' % (colors.excName, etype, colorsnormal) |
|
653 | exc = '%s%s%s' % (colors.excName, etype, colorsnormal) | |
1077 | width = min(75, get_terminal_size()[0]) |
|
654 | width = min(75, get_terminal_size()[0]) | |
1078 | if long_version: |
|
655 | if long_version: | |
1079 | # Header with the exception type, python version, and date |
|
656 | # Header with the exception type, python version, and date | |
1080 | pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable |
|
657 | pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable | |
1081 | date = time.ctime(time.time()) |
|
658 | date = time.ctime(time.time()) | |
1082 |
|
659 | |||
1083 | head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal, |
|
660 | head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal, | |
1084 | exc, ' ' * (width - len(str(etype)) - len(pyver)), |
|
661 | exc, ' ' * (width - len(str(etype)) - len(pyver)), | |
1085 | pyver, date.rjust(width) ) |
|
662 | pyver, date.rjust(width) ) | |
1086 | head += "\nA problem occurred executing Python code. Here is the sequence of function" \ |
|
663 | head += "\nA problem occurred executing Python code. Here is the sequence of function" \ | |
1087 | "\ncalls leading up to the error, with the most recent (innermost) call last." |
|
664 | "\ncalls leading up to the error, with the most recent (innermost) call last." | |
1088 | else: |
|
665 | else: | |
1089 | # Simplified header |
|
666 | # Simplified header | |
1090 | head = '%s%s' % (exc, 'Traceback (most recent call last)'. \ |
|
667 | head = '%s%s' % (exc, 'Traceback (most recent call last)'. \ | |
1091 | rjust(width - len(str(etype))) ) |
|
668 | rjust(width - len(str(etype))) ) | |
1092 |
|
669 | |||
1093 | return head |
|
670 | return head | |
1094 |
|
671 | |||
1095 | def format_exception(self, etype, evalue): |
|
672 | def format_exception(self, etype, evalue): | |
1096 | colors = self.Colors # just a shorthand + quicker name lookup |
|
673 | colors = self.Colors # just a shorthand + quicker name lookup | |
1097 | colorsnormal = colors.Normal # used a lot |
|
674 | colorsnormal = colors.Normal # used a lot | |
1098 | # Get (safely) a string form of the exception info |
|
675 | # Get (safely) a string form of the exception info | |
1099 | try: |
|
676 | try: | |
1100 | etype_str, evalue_str = map(str, (etype, evalue)) |
|
677 | etype_str, evalue_str = map(str, (etype, evalue)) | |
1101 | except: |
|
678 | except: | |
1102 | # User exception is improperly defined. |
|
679 | # User exception is improperly defined. | |
1103 | etype, evalue = str, sys.exc_info()[:2] |
|
680 | etype, evalue = str, sys.exc_info()[:2] | |
1104 | etype_str, evalue_str = map(str, (etype, evalue)) |
|
681 | etype_str, evalue_str = map(str, (etype, evalue)) | |
1105 | # ... and format it |
|
682 | # ... and format it | |
1106 | return ['%s%s%s: %s' % (colors.excName, etype_str, |
|
683 | return ['%s%s%s: %s' % (colors.excName, etype_str, | |
1107 | colorsnormal, py3compat.cast_unicode(evalue_str))] |
|
684 | colorsnormal, py3compat.cast_unicode(evalue_str))] | |
1108 |
|
685 | |||
1109 | def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset): |
|
686 | def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset): | |
1110 | """Formats the header, traceback and exception message for a single exception. |
|
687 | """Formats the header, traceback and exception message for a single exception. | |
1111 |
|
688 | |||
1112 | This may be called multiple times by Python 3 exception chaining |
|
689 | This may be called multiple times by Python 3 exception chaining | |
1113 | (PEP 3134). |
|
690 | (PEP 3134). | |
1114 | """ |
|
691 | """ | |
1115 | # some locals |
|
692 | # some locals | |
1116 | orig_etype = etype |
|
693 | orig_etype = etype | |
1117 | try: |
|
694 | try: | |
1118 | etype = etype.__name__ |
|
695 | etype = etype.__name__ | |
1119 | except AttributeError: |
|
696 | except AttributeError: | |
1120 | pass |
|
697 | pass | |
1121 |
|
698 | |||
1122 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
699 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
1123 | head = self.prepare_header(etype, self.long_header) |
|
700 | head = self.prepare_header(etype, self.long_header) | |
1124 | records = self.get_records(etb, number_of_lines_of_context, tb_offset) |
|
701 | records = self.get_records(etb, number_of_lines_of_context, tb_offset) | |
1125 |
|
702 | |||
1126 | if records is None: |
|
703 | frames = list(map(self.format_record, records)) | |
1127 | return "" |
|
|||
1128 |
|
||||
1129 | last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records) |
|
|||
1130 |
|
||||
1131 | frames = self.format_records(records, last_unique, recursion_repeat) |
|
|||
1132 |
|
704 | |||
1133 | formatted_exception = self.format_exception(etype, evalue) |
|
705 | formatted_exception = self.format_exception(etype, evalue) | |
1134 | if records: |
|
706 | if records: | |
1135 |
f |
|
707 | frame_info = records[-1] | |
1136 | filepath = os.path.abspath(filepath) |
|
|||
1137 | ipinst = get_ipython() |
|
708 | ipinst = get_ipython() | |
1138 | if ipinst is not None: |
|
709 | if ipinst is not None: | |
1139 |
ipinst.hooks.synchronize_with_editor(f |
|
710 | ipinst.hooks.synchronize_with_editor(frame_info.filename, frame_info.lineno, 0) | |
1140 |
|
711 | |||
1141 | return [[head] + frames + [''.join(formatted_exception[0])]] |
|
712 | return [[head] + frames + [''.join(formatted_exception[0])]] | |
1142 |
|
713 | |||
1143 | def get_records(self, etb, number_of_lines_of_context, tb_offset): |
|
714 | def get_records(self, etb, number_of_lines_of_context, tb_offset): | |
1144 | try: |
|
715 | context = number_of_lines_of_context - 1 | |
1145 | # Try the default getinnerframes and Alex's: Alex's fixes some |
|
716 | after = context // 2 | |
1146 | # problems, but it generates empty tracebacks for console errors |
|
717 | before = context - after | |
1147 | # (5 blanks lines) where none should be returned. |
|
718 | options = stack_data.Options(before=before, after=after) | |
1148 | return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset) |
|
719 | return list(stack_data.FrameInfo.stack_data(etb, options=options))[tb_offset:] | |
1149 | except UnicodeDecodeError: |
|
|||
1150 | # This can occur if a file's encoding magic comment is wrong. |
|
|||
1151 | # I can't see a way to recover without duplicating a bunch of code |
|
|||
1152 | # from the stdlib traceback module. --TK |
|
|||
1153 | error('\nUnicodeDecodeError while processing traceback.\n') |
|
|||
1154 | return None |
|
|||
1155 | except: |
|
|||
1156 | # FIXME: I've been getting many crash reports from python 2.3 |
|
|||
1157 | # users, traceable to inspect.py. If I can find a small test-case |
|
|||
1158 | # to reproduce this, I should either write a better workaround or |
|
|||
1159 | # file a bug report against inspect (if that's the real problem). |
|
|||
1160 | # So far, I haven't been able to find an isolated example to |
|
|||
1161 | # reproduce the problem. |
|
|||
1162 | inspect_error() |
|
|||
1163 | traceback.print_exc(file=self.ostream) |
|
|||
1164 | info('\nUnfortunately, your original traceback can not be constructed.\n') |
|
|||
1165 | return None |
|
|||
1166 |
|
720 | |||
1167 | def structured_traceback(self, etype, evalue, etb, tb_offset=None, |
|
721 | def structured_traceback(self, etype, evalue, etb, tb_offset=None, | |
1168 | number_of_lines_of_context=5): |
|
722 | number_of_lines_of_context=5): | |
1169 | """Return a nice text document describing the traceback.""" |
|
723 | """Return a nice text document describing the traceback.""" | |
1170 |
|
724 | |||
1171 | formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context, |
|
725 | formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context, | |
1172 | tb_offset) |
|
726 | tb_offset) | |
1173 |
|
727 | |||
1174 | colors = self.Colors # just a shorthand + quicker name lookup |
|
728 | colors = self.Colors # just a shorthand + quicker name lookup | |
1175 | colorsnormal = colors.Normal # used a lot |
|
729 | colorsnormal = colors.Normal # used a lot | |
1176 | head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal) |
|
730 | head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal) | |
1177 | structured_traceback_parts = [head] |
|
731 | structured_traceback_parts = [head] | |
1178 | chained_exceptions_tb_offset = 0 |
|
732 | chained_exceptions_tb_offset = 0 | |
1179 | lines_of_context = 3 |
|
733 | lines_of_context = 3 | |
1180 | formatted_exceptions = formatted_exception |
|
734 | formatted_exceptions = formatted_exception | |
1181 | exception = self.get_parts_of_chained_exception(evalue) |
|
735 | exception = self.get_parts_of_chained_exception(evalue) | |
1182 | if exception: |
|
736 | if exception: | |
1183 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) |
|
737 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) | |
1184 | etype, evalue, etb = exception |
|
738 | etype, evalue, etb = exception | |
1185 | else: |
|
739 | else: | |
1186 | evalue = None |
|
740 | evalue = None | |
1187 | chained_exc_ids = set() |
|
741 | chained_exc_ids = set() | |
1188 | while evalue: |
|
742 | while evalue: | |
1189 | formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context, |
|
743 | formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context, | |
1190 | chained_exceptions_tb_offset) |
|
744 | chained_exceptions_tb_offset) | |
1191 | exception = self.get_parts_of_chained_exception(evalue) |
|
745 | exception = self.get_parts_of_chained_exception(evalue) | |
1192 |
|
746 | |||
1193 | if exception and not id(exception[1]) in chained_exc_ids: |
|
747 | if exception and not id(exception[1]) in chained_exc_ids: | |
1194 | chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop |
|
748 | chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop | |
1195 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) |
|
749 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) | |
1196 | etype, evalue, etb = exception |
|
750 | etype, evalue, etb = exception | |
1197 | else: |
|
751 | else: | |
1198 | evalue = None |
|
752 | evalue = None | |
1199 |
|
753 | |||
1200 | # we want to see exceptions in a reversed order: |
|
754 | # we want to see exceptions in a reversed order: | |
1201 | # the first exception should be on top |
|
755 | # the first exception should be on top | |
1202 | for formatted_exception in reversed(formatted_exceptions): |
|
756 | for formatted_exception in reversed(formatted_exceptions): | |
1203 | structured_traceback_parts += formatted_exception |
|
757 | structured_traceback_parts += formatted_exception | |
1204 |
|
758 | |||
1205 | return structured_traceback_parts |
|
759 | return structured_traceback_parts | |
1206 |
|
760 | |||
1207 | def debugger(self, force=False): |
|
761 | def debugger(self, force=False): | |
1208 | """Call up the pdb debugger if desired, always clean up the tb |
|
762 | """Call up the pdb debugger if desired, always clean up the tb | |
1209 | reference. |
|
763 | reference. | |
1210 |
|
764 | |||
1211 | Keywords: |
|
765 | Keywords: | |
1212 |
|
766 | |||
1213 | - force(False): by default, this routine checks the instance call_pdb |
|
767 | - force(False): by default, this routine checks the instance call_pdb | |
1214 | flag and does not actually invoke the debugger if the flag is false. |
|
768 | flag and does not actually invoke the debugger if the flag is false. | |
1215 | The 'force' option forces the debugger to activate even if the flag |
|
769 | The 'force' option forces the debugger to activate even if the flag | |
1216 | is false. |
|
770 | is false. | |
1217 |
|
771 | |||
1218 | If the call_pdb flag is set, the pdb interactive debugger is |
|
772 | If the call_pdb flag is set, the pdb interactive debugger is | |
1219 | invoked. In all cases, the self.tb reference to the current traceback |
|
773 | invoked. In all cases, the self.tb reference to the current traceback | |
1220 | is deleted to prevent lingering references which hamper memory |
|
774 | is deleted to prevent lingering references which hamper memory | |
1221 | management. |
|
775 | management. | |
1222 |
|
776 | |||
1223 | Note that each call to pdb() does an 'import readline', so if your app |
|
777 | Note that each call to pdb() does an 'import readline', so if your app | |
1224 | requires a special setup for the readline completers, you'll have to |
|
778 | requires a special setup for the readline completers, you'll have to | |
1225 | fix that by hand after invoking the exception handler.""" |
|
779 | fix that by hand after invoking the exception handler.""" | |
1226 |
|
780 | |||
1227 | if force or self.call_pdb: |
|
781 | if force or self.call_pdb: | |
1228 | if self.pdb is None: |
|
782 | if self.pdb is None: | |
1229 | self.pdb = self.debugger_cls() |
|
783 | self.pdb = self.debugger_cls() | |
1230 | # the system displayhook may have changed, restore the original |
|
784 | # the system displayhook may have changed, restore the original | |
1231 | # for pdb |
|
785 | # for pdb | |
1232 | display_trap = DisplayTrap(hook=sys.__displayhook__) |
|
786 | display_trap = DisplayTrap(hook=sys.__displayhook__) | |
1233 | with display_trap: |
|
787 | with display_trap: | |
1234 | self.pdb.reset() |
|
788 | self.pdb.reset() | |
1235 | # Find the right frame so we don't pop up inside ipython itself |
|
789 | # Find the right frame so we don't pop up inside ipython itself | |
1236 | if hasattr(self, 'tb') and self.tb is not None: |
|
790 | if hasattr(self, 'tb') and self.tb is not None: | |
1237 | etb = self.tb |
|
791 | etb = self.tb | |
1238 | else: |
|
792 | else: | |
1239 | etb = self.tb = sys.last_traceback |
|
793 | etb = self.tb = sys.last_traceback | |
1240 | while self.tb is not None and self.tb.tb_next is not None: |
|
794 | while self.tb is not None and self.tb.tb_next is not None: | |
1241 | self.tb = self.tb.tb_next |
|
795 | self.tb = self.tb.tb_next | |
1242 | if etb and etb.tb_next: |
|
796 | if etb and etb.tb_next: | |
1243 | etb = etb.tb_next |
|
797 | etb = etb.tb_next | |
1244 | self.pdb.botframe = etb.tb_frame |
|
798 | self.pdb.botframe = etb.tb_frame | |
1245 | self.pdb.interaction(None, etb) |
|
799 | self.pdb.interaction(None, etb) | |
1246 |
|
800 | |||
1247 | if hasattr(self, 'tb'): |
|
801 | if hasattr(self, 'tb'): | |
1248 | del self.tb |
|
802 | del self.tb | |
1249 |
|
803 | |||
1250 | def handler(self, info=None): |
|
804 | def handler(self, info=None): | |
1251 | (etype, evalue, etb) = info or sys.exc_info() |
|
805 | (etype, evalue, etb) = info or sys.exc_info() | |
1252 | self.tb = etb |
|
806 | self.tb = etb | |
1253 | ostream = self.ostream |
|
807 | ostream = self.ostream | |
1254 | ostream.flush() |
|
808 | ostream.flush() | |
1255 | ostream.write(self.text(etype, evalue, etb)) |
|
809 | ostream.write(self.text(etype, evalue, etb)) | |
1256 | ostream.write('\n') |
|
810 | ostream.write('\n') | |
1257 | ostream.flush() |
|
811 | ostream.flush() | |
1258 |
|
812 | |||
1259 | # Changed so an instance can just be called as VerboseTB_inst() and print |
|
813 | # Changed so an instance can just be called as VerboseTB_inst() and print | |
1260 | # out the right info on its own. |
|
814 | # out the right info on its own. | |
1261 | def __call__(self, etype=None, evalue=None, etb=None): |
|
815 | def __call__(self, etype=None, evalue=None, etb=None): | |
1262 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" |
|
816 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" | |
1263 | if etb is None: |
|
817 | if etb is None: | |
1264 | self.handler() |
|
818 | self.handler() | |
1265 | else: |
|
819 | else: | |
1266 | self.handler((etype, evalue, etb)) |
|
820 | self.handler((etype, evalue, etb)) | |
1267 | try: |
|
821 | try: | |
1268 | self.debugger() |
|
822 | self.debugger() | |
1269 | except KeyboardInterrupt: |
|
823 | except KeyboardInterrupt: | |
1270 | print("\nKeyboardInterrupt") |
|
824 | print("\nKeyboardInterrupt") | |
1271 |
|
825 | |||
1272 |
|
826 | |||
1273 | #---------------------------------------------------------------------------- |
|
827 | #---------------------------------------------------------------------------- | |
1274 | class FormattedTB(VerboseTB, ListTB): |
|
828 | class FormattedTB(VerboseTB, ListTB): | |
1275 | """Subclass ListTB but allow calling with a traceback. |
|
829 | """Subclass ListTB but allow calling with a traceback. | |
1276 |
|
830 | |||
1277 | It can thus be used as a sys.excepthook for Python > 2.1. |
|
831 | It can thus be used as a sys.excepthook for Python > 2.1. | |
1278 |
|
832 | |||
1279 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. |
|
833 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. | |
1280 |
|
834 | |||
1281 | Allows a tb_offset to be specified. This is useful for situations where |
|
835 | Allows a tb_offset to be specified. This is useful for situations where | |
1282 | one needs to remove a number of topmost frames from the traceback (such as |
|
836 | one needs to remove a number of topmost frames from the traceback (such as | |
1283 | occurs with python programs that themselves execute other python code, |
|
837 | occurs with python programs that themselves execute other python code, | |
1284 | like Python shells). """ |
|
838 | like Python shells). """ | |
1285 |
|
839 | |||
1286 | def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False, |
|
840 | def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False, | |
1287 | ostream=None, |
|
841 | ostream=None, | |
1288 | tb_offset=0, long_header=False, include_vars=False, |
|
842 | tb_offset=0, long_header=False, include_vars=False, | |
1289 | check_cache=None, debugger_cls=None, |
|
843 | check_cache=None, debugger_cls=None, | |
1290 | parent=None, config=None): |
|
844 | parent=None, config=None): | |
1291 |
|
845 | |||
1292 | # NEVER change the order of this list. Put new modes at the end: |
|
846 | # NEVER change the order of this list. Put new modes at the end: | |
1293 | self.valid_modes = ['Plain', 'Context', 'Verbose', 'Minimal'] |
|
847 | self.valid_modes = ['Plain', 'Context', 'Verbose', 'Minimal'] | |
1294 | self.verbose_modes = self.valid_modes[1:3] |
|
848 | self.verbose_modes = self.valid_modes[1:3] | |
1295 |
|
849 | |||
1296 | VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, |
|
850 | VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, | |
1297 | ostream=ostream, tb_offset=tb_offset, |
|
851 | ostream=ostream, tb_offset=tb_offset, | |
1298 | long_header=long_header, include_vars=include_vars, |
|
852 | long_header=long_header, include_vars=include_vars, | |
1299 | check_cache=check_cache, debugger_cls=debugger_cls, |
|
853 | check_cache=check_cache, debugger_cls=debugger_cls, | |
1300 | parent=parent, config=config) |
|
854 | parent=parent, config=config) | |
1301 |
|
855 | |||
1302 | # Different types of tracebacks are joined with different separators to |
|
856 | # Different types of tracebacks are joined with different separators to | |
1303 | # form a single string. They are taken from this dict |
|
857 | # form a single string. They are taken from this dict | |
1304 | self._join_chars = dict(Plain='', Context='\n', Verbose='\n', |
|
858 | self._join_chars = dict(Plain='', Context='\n', Verbose='\n', | |
1305 | Minimal='') |
|
859 | Minimal='') | |
1306 | # set_mode also sets the tb_join_char attribute |
|
860 | # set_mode also sets the tb_join_char attribute | |
1307 | self.set_mode(mode) |
|
861 | self.set_mode(mode) | |
1308 |
|
862 | |||
1309 | def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5): |
|
863 | def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5): | |
1310 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
864 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
1311 | mode = self.mode |
|
865 | mode = self.mode | |
1312 | if mode in self.verbose_modes: |
|
866 | if mode in self.verbose_modes: | |
1313 | # Verbose modes need a full traceback |
|
867 | # Verbose modes need a full traceback | |
1314 | return VerboseTB.structured_traceback( |
|
868 | return VerboseTB.structured_traceback( | |
1315 | self, etype, value, tb, tb_offset, number_of_lines_of_context |
|
869 | self, etype, value, tb, tb_offset, number_of_lines_of_context | |
1316 | ) |
|
870 | ) | |
1317 | elif mode == 'Minimal': |
|
871 | elif mode == 'Minimal': | |
1318 | return ListTB.get_exception_only(self, etype, value) |
|
872 | return ListTB.get_exception_only(self, etype, value) | |
1319 | else: |
|
873 | else: | |
1320 | # We must check the source cache because otherwise we can print |
|
874 | # We must check the source cache because otherwise we can print | |
1321 | # out-of-date source code. |
|
875 | # out-of-date source code. | |
1322 | self.check_cache() |
|
876 | self.check_cache() | |
1323 | # Now we can extract and format the exception |
|
877 | # Now we can extract and format the exception | |
1324 | return ListTB.structured_traceback( |
|
878 | return ListTB.structured_traceback( | |
1325 | self, etype, value, tb, tb_offset, number_of_lines_of_context |
|
879 | self, etype, value, tb, tb_offset, number_of_lines_of_context | |
1326 | ) |
|
880 | ) | |
1327 |
|
881 | |||
1328 | def stb2text(self, stb): |
|
882 | def stb2text(self, stb): | |
1329 | """Convert a structured traceback (a list) to a string.""" |
|
883 | """Convert a structured traceback (a list) to a string.""" | |
1330 | return self.tb_join_char.join(stb) |
|
884 | return self.tb_join_char.join(stb) | |
1331 |
|
885 | |||
1332 |
|
886 | |||
1333 | def set_mode(self, mode=None): |
|
887 | def set_mode(self, mode=None): | |
1334 | """Switch to the desired mode. |
|
888 | """Switch to the desired mode. | |
1335 |
|
889 | |||
1336 | If mode is not specified, cycles through the available modes.""" |
|
890 | If mode is not specified, cycles through the available modes.""" | |
1337 |
|
891 | |||
1338 | if not mode: |
|
892 | if not mode: | |
1339 | new_idx = (self.valid_modes.index(self.mode) + 1 ) % \ |
|
893 | new_idx = (self.valid_modes.index(self.mode) + 1 ) % \ | |
1340 | len(self.valid_modes) |
|
894 | len(self.valid_modes) | |
1341 | self.mode = self.valid_modes[new_idx] |
|
895 | self.mode = self.valid_modes[new_idx] | |
1342 | elif mode not in self.valid_modes: |
|
896 | elif mode not in self.valid_modes: | |
1343 | raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n' |
|
897 | raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n' | |
1344 | 'Valid modes: ' + str(self.valid_modes)) |
|
898 | 'Valid modes: ' + str(self.valid_modes)) | |
1345 | else: |
|
899 | else: | |
1346 | self.mode = mode |
|
900 | self.mode = mode | |
1347 | # include variable details only in 'Verbose' mode |
|
901 | # include variable details only in 'Verbose' mode | |
1348 | self.include_vars = (self.mode == self.valid_modes[2]) |
|
902 | self.include_vars = (self.mode == self.valid_modes[2]) | |
1349 | # Set the join character for generating text tracebacks |
|
903 | # Set the join character for generating text tracebacks | |
1350 | self.tb_join_char = self._join_chars[self.mode] |
|
904 | self.tb_join_char = self._join_chars[self.mode] | |
1351 |
|
905 | |||
1352 | # some convenient shortcuts |
|
906 | # some convenient shortcuts | |
1353 | def plain(self): |
|
907 | def plain(self): | |
1354 | self.set_mode(self.valid_modes[0]) |
|
908 | self.set_mode(self.valid_modes[0]) | |
1355 |
|
909 | |||
1356 | def context(self): |
|
910 | def context(self): | |
1357 | self.set_mode(self.valid_modes[1]) |
|
911 | self.set_mode(self.valid_modes[1]) | |
1358 |
|
912 | |||
1359 | def verbose(self): |
|
913 | def verbose(self): | |
1360 | self.set_mode(self.valid_modes[2]) |
|
914 | self.set_mode(self.valid_modes[2]) | |
1361 |
|
915 | |||
1362 | def minimal(self): |
|
916 | def minimal(self): | |
1363 | self.set_mode(self.valid_modes[3]) |
|
917 | self.set_mode(self.valid_modes[3]) | |
1364 |
|
918 | |||
1365 |
|
919 | |||
1366 | #---------------------------------------------------------------------------- |
|
920 | #---------------------------------------------------------------------------- | |
1367 | class AutoFormattedTB(FormattedTB): |
|
921 | class AutoFormattedTB(FormattedTB): | |
1368 | """A traceback printer which can be called on the fly. |
|
922 | """A traceback printer which can be called on the fly. | |
1369 |
|
923 | |||
1370 | It will find out about exceptions by itself. |
|
924 | It will find out about exceptions by itself. | |
1371 |
|
925 | |||
1372 | A brief example:: |
|
926 | A brief example:: | |
1373 |
|
927 | |||
1374 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') |
|
928 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') | |
1375 | try: |
|
929 | try: | |
1376 | ... |
|
930 | ... | |
1377 | except: |
|
931 | except: | |
1378 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object |
|
932 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object | |
1379 | """ |
|
933 | """ | |
1380 |
|
934 | |||
1381 | def __call__(self, etype=None, evalue=None, etb=None, |
|
935 | def __call__(self, etype=None, evalue=None, etb=None, | |
1382 | out=None, tb_offset=None): |
|
936 | out=None, tb_offset=None): | |
1383 | """Print out a formatted exception traceback. |
|
937 | """Print out a formatted exception traceback. | |
1384 |
|
938 | |||
1385 | Optional arguments: |
|
939 | Optional arguments: | |
1386 | - out: an open file-like object to direct output to. |
|
940 | - out: an open file-like object to direct output to. | |
1387 |
|
941 | |||
1388 | - tb_offset: the number of frames to skip over in the stack, on a |
|
942 | - tb_offset: the number of frames to skip over in the stack, on a | |
1389 | per-call basis (this overrides temporarily the instance's tb_offset |
|
943 | per-call basis (this overrides temporarily the instance's tb_offset | |
1390 | given at initialization time. """ |
|
944 | given at initialization time. """ | |
1391 |
|
945 | |||
1392 | if out is None: |
|
946 | if out is None: | |
1393 | out = self.ostream |
|
947 | out = self.ostream | |
1394 | out.flush() |
|
948 | out.flush() | |
1395 | out.write(self.text(etype, evalue, etb, tb_offset)) |
|
949 | out.write(self.text(etype, evalue, etb, tb_offset)) | |
1396 | out.write('\n') |
|
950 | out.write('\n') | |
1397 | out.flush() |
|
951 | out.flush() | |
1398 | # FIXME: we should remove the auto pdb behavior from here and leave |
|
952 | # FIXME: we should remove the auto pdb behavior from here and leave | |
1399 | # that to the clients. |
|
953 | # that to the clients. | |
1400 | try: |
|
954 | try: | |
1401 | self.debugger() |
|
955 | self.debugger() | |
1402 | except KeyboardInterrupt: |
|
956 | except KeyboardInterrupt: | |
1403 | print("\nKeyboardInterrupt") |
|
957 | print("\nKeyboardInterrupt") | |
1404 |
|
958 | |||
1405 | def structured_traceback(self, etype=None, value=None, tb=None, |
|
959 | def structured_traceback(self, etype=None, value=None, tb=None, | |
1406 | tb_offset=None, number_of_lines_of_context=5): |
|
960 | tb_offset=None, number_of_lines_of_context=5): | |
1407 | if etype is None: |
|
961 | if etype is None: | |
1408 | etype, value, tb = sys.exc_info() |
|
962 | etype, value, tb = sys.exc_info() | |
1409 | if isinstance(tb, tuple): |
|
963 | if isinstance(tb, tuple): | |
1410 | # tb is a tuple if this is a chained exception. |
|
964 | # tb is a tuple if this is a chained exception. | |
1411 | self.tb = tb[0] |
|
965 | self.tb = tb[0] | |
1412 | else: |
|
966 | else: | |
1413 | self.tb = tb |
|
967 | self.tb = tb | |
1414 | return FormattedTB.structured_traceback( |
|
968 | return FormattedTB.structured_traceback( | |
1415 | self, etype, value, tb, tb_offset, number_of_lines_of_context) |
|
969 | self, etype, value, tb, tb_offset, number_of_lines_of_context) | |
1416 |
|
970 | |||
1417 |
|
971 | |||
1418 | #--------------------------------------------------------------------------- |
|
972 | #--------------------------------------------------------------------------- | |
1419 |
|
973 | |||
1420 | # A simple class to preserve Nathan's original functionality. |
|
974 | # A simple class to preserve Nathan's original functionality. | |
1421 | class ColorTB(FormattedTB): |
|
975 | class ColorTB(FormattedTB): | |
1422 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" |
|
976 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" | |
1423 |
|
977 | |||
1424 | def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs): |
|
978 | def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs): | |
1425 | FormattedTB.__init__(self, color_scheme=color_scheme, |
|
979 | FormattedTB.__init__(self, color_scheme=color_scheme, | |
1426 | call_pdb=call_pdb, **kwargs) |
|
980 | call_pdb=call_pdb, **kwargs) | |
1427 |
|
981 | |||
1428 |
|
982 | |||
1429 | class SyntaxTB(ListTB): |
|
983 | class SyntaxTB(ListTB): | |
1430 | """Extension which holds some state: the last exception value""" |
|
984 | """Extension which holds some state: the last exception value""" | |
1431 |
|
985 | |||
1432 | def __init__(self, color_scheme='NoColor', parent=None, config=None): |
|
986 | def __init__(self, color_scheme='NoColor', parent=None, config=None): | |
1433 | ListTB.__init__(self, color_scheme, parent=parent, config=config) |
|
987 | ListTB.__init__(self, color_scheme, parent=parent, config=config) | |
1434 | self.last_syntax_error = None |
|
988 | self.last_syntax_error = None | |
1435 |
|
989 | |||
1436 | def __call__(self, etype, value, elist): |
|
990 | def __call__(self, etype, value, elist): | |
1437 | self.last_syntax_error = value |
|
991 | self.last_syntax_error = value | |
1438 |
|
992 | |||
1439 | ListTB.__call__(self, etype, value, elist) |
|
993 | ListTB.__call__(self, etype, value, elist) | |
1440 |
|
994 | |||
1441 | def structured_traceback(self, etype, value, elist, tb_offset=None, |
|
995 | def structured_traceback(self, etype, value, elist, tb_offset=None, | |
1442 | context=5): |
|
996 | context=5): | |
1443 | # If the source file has been edited, the line in the syntax error can |
|
997 | # If the source file has been edited, the line in the syntax error can | |
1444 | # be wrong (retrieved from an outdated cache). This replaces it with |
|
998 | # be wrong (retrieved from an outdated cache). This replaces it with | |
1445 | # the current value. |
|
999 | # the current value. | |
1446 | if isinstance(value, SyntaxError) \ |
|
1000 | if isinstance(value, SyntaxError) \ | |
1447 | and isinstance(value.filename, str) \ |
|
1001 | and isinstance(value.filename, str) \ | |
1448 | and isinstance(value.lineno, int): |
|
1002 | and isinstance(value.lineno, int): | |
1449 | linecache.checkcache(value.filename) |
|
1003 | linecache.checkcache(value.filename) | |
1450 | newtext = linecache.getline(value.filename, value.lineno) |
|
1004 | newtext = linecache.getline(value.filename, value.lineno) | |
1451 | if newtext: |
|
1005 | if newtext: | |
1452 | value.text = newtext |
|
1006 | value.text = newtext | |
1453 | self.last_syntax_error = value |
|
1007 | self.last_syntax_error = value | |
1454 | return super(SyntaxTB, self).structured_traceback(etype, value, elist, |
|
1008 | return super(SyntaxTB, self).structured_traceback(etype, value, elist, | |
1455 | tb_offset=tb_offset, context=context) |
|
1009 | tb_offset=tb_offset, context=context) | |
1456 |
|
1010 | |||
1457 | def clear_err_state(self): |
|
1011 | def clear_err_state(self): | |
1458 | """Return the current error state and clear it""" |
|
1012 | """Return the current error state and clear it""" | |
1459 | e = self.last_syntax_error |
|
1013 | e = self.last_syntax_error | |
1460 | self.last_syntax_error = None |
|
1014 | self.last_syntax_error = None | |
1461 | return e |
|
1015 | return e | |
1462 |
|
1016 | |||
1463 | def stb2text(self, stb): |
|
1017 | def stb2text(self, stb): | |
1464 | """Convert a structured traceback (a list) to a string.""" |
|
1018 | """Convert a structured traceback (a list) to a string.""" | |
1465 | return ''.join(stb) |
|
1019 | return ''.join(stb) | |
1466 |
|
1020 | |||
1467 |
|
1021 | |||
1468 | # some internal-use functions |
|
1022 | # some internal-use functions | |
1469 | def text_repr(value): |
|
1023 | def text_repr(value): | |
1470 | """Hopefully pretty robust repr equivalent.""" |
|
1024 | """Hopefully pretty robust repr equivalent.""" | |
1471 | # this is pretty horrible but should always return *something* |
|
1025 | # this is pretty horrible but should always return *something* | |
1472 | try: |
|
1026 | try: | |
1473 | return pydoc.text.repr(value) |
|
1027 | return pydoc.text.repr(value) | |
1474 | except KeyboardInterrupt: |
|
1028 | except KeyboardInterrupt: | |
1475 | raise |
|
1029 | raise | |
1476 | except: |
|
1030 | except: | |
1477 | try: |
|
1031 | try: | |
1478 | return repr(value) |
|
1032 | return repr(value) | |
1479 | except KeyboardInterrupt: |
|
1033 | except KeyboardInterrupt: | |
1480 | raise |
|
1034 | raise | |
1481 | except: |
|
1035 | except: | |
1482 | try: |
|
1036 | try: | |
1483 | # all still in an except block so we catch |
|
1037 | # all still in an except block so we catch | |
1484 | # getattr raising |
|
1038 | # getattr raising | |
1485 | name = getattr(value, '__name__', None) |
|
1039 | name = getattr(value, '__name__', None) | |
1486 | if name: |
|
1040 | if name: | |
1487 | # ick, recursion |
|
1041 | # ick, recursion | |
1488 | return text_repr(name) |
|
1042 | return text_repr(name) | |
1489 | klass = getattr(value, '__class__', None) |
|
1043 | klass = getattr(value, '__class__', None) | |
1490 | if klass: |
|
1044 | if klass: | |
1491 | return '%s instance' % text_repr(klass) |
|
1045 | return '%s instance' % text_repr(klass) | |
1492 | except KeyboardInterrupt: |
|
1046 | except KeyboardInterrupt: | |
1493 | raise |
|
1047 | raise | |
1494 | except: |
|
1048 | except: | |
1495 | return 'UNRECOVERABLE REPR FAILURE' |
|
1049 | return 'UNRECOVERABLE REPR FAILURE' | |
1496 |
|
1050 | |||
1497 |
|
1051 | |||
1498 | def eqrepr(value, repr=text_repr): |
|
1052 | def eqrepr(value, repr=text_repr): | |
1499 | return '=%s' % repr(value) |
|
1053 | return '=%s' % repr(value) | |
1500 |
|
1054 | |||
1501 |
|
1055 | |||
1502 | def nullrepr(value, repr=text_repr): |
|
1056 | def nullrepr(value, repr=text_repr): | |
1503 | return '' |
|
1057 | return '' |
General Comments 0
You need to be logged in to leave comments.
Login now