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