##// END OF EJS Templates
fix syntax error
Quentin Peter -
Show More
@@ -1,1502 +1,1505 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):
533 def get_parts_of_chained_exception(self, evalue):
534 def get_chained_exception(exception_value):
534 def get_chained_exception(exception_value):
535 cause = getattr(exception_value, '__cause__', None)
535 cause = getattr(exception_value, '__cause__', None)
536 if cause:
536 if cause:
537 return cause
537 return cause
538 if getattr(exception_value, '__suppress_context__', False):
538 if getattr(exception_value, '__suppress_context__', False):
539 return None
539 return None
540 return getattr(exception_value, '__context__', None)
540 return getattr(exception_value, '__context__', None)
541
541
542 chained_evalue = get_chained_exception(evalue)
542 chained_evalue = get_chained_exception(evalue)
543
543
544 if chained_evalue:
544 if chained_evalue:
545 return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__
545 return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__
546
546
547 def prepare_chained_exception_message(self, cause):
547 def prepare_chained_exception_message(self, cause):
548 direct_cause = "\nThe above exception was the direct cause of the following exception:\n"
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"
549 exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n"
550
550
551 if cause:
551 if cause:
552 message = [[direct_cause]]
552 message = [[direct_cause]]
553 else:
553 else:
554 message = [[exception_during_handling]]
554 message = [[exception_during_handling]]
555 return message
555 return message
556
556
557 def set_colors(self, *args, **kw):
557 def set_colors(self, *args, **kw):
558 """Shorthand access to the color table scheme selector method."""
558 """Shorthand access to the color table scheme selector method."""
559
559
560 # Set own color table
560 # Set own color table
561 self.color_scheme_table.set_active_scheme(*args, **kw)
561 self.color_scheme_table.set_active_scheme(*args, **kw)
562 # for convenience, set Colors to the active scheme
562 # for convenience, set Colors to the active scheme
563 self.Colors = self.color_scheme_table.active_colors
563 self.Colors = self.color_scheme_table.active_colors
564 # Also set colors of debugger
564 # Also set colors of debugger
565 if hasattr(self, 'pdb') and self.pdb is not None:
565 if hasattr(self, 'pdb') and self.pdb is not None:
566 self.pdb.set_colors(*args, **kw)
566 self.pdb.set_colors(*args, **kw)
567
567
568 def color_toggle(self):
568 def color_toggle(self):
569 """Toggle between the currently active color scheme and NoColor."""
569 """Toggle between the currently active color scheme and NoColor."""
570
570
571 if self.color_scheme_table.active_scheme_name == 'NoColor':
571 if self.color_scheme_table.active_scheme_name == 'NoColor':
572 self.color_scheme_table.set_active_scheme(self.old_scheme)
572 self.color_scheme_table.set_active_scheme(self.old_scheme)
573 self.Colors = self.color_scheme_table.active_colors
573 self.Colors = self.color_scheme_table.active_colors
574 else:
574 else:
575 self.old_scheme = self.color_scheme_table.active_scheme_name
575 self.old_scheme = self.color_scheme_table.active_scheme_name
576 self.color_scheme_table.set_active_scheme('NoColor')
576 self.color_scheme_table.set_active_scheme('NoColor')
577 self.Colors = self.color_scheme_table.active_colors
577 self.Colors = self.color_scheme_table.active_colors
578
578
579 def stb2text(self, stb):
579 def stb2text(self, stb):
580 """Convert a structured traceback (a list) to a string."""
580 """Convert a structured traceback (a list) to a string."""
581 return '\n'.join(stb)
581 return '\n'.join(stb)
582
582
583 def text(self, etype, value, tb, tb_offset=None, context=5):
583 def text(self, etype, value, tb, tb_offset=None, context=5):
584 """Return formatted traceback.
584 """Return formatted traceback.
585
585
586 Subclasses may override this if they add extra arguments.
586 Subclasses may override this if they add extra arguments.
587 """
587 """
588 tb_list = self.structured_traceback(etype, value, tb,
588 tb_list = self.structured_traceback(etype, value, tb,
589 tb_offset, context)
589 tb_offset, context)
590 return self.stb2text(tb_list)
590 return self.stb2text(tb_list)
591
591
592 def structured_traceback(self, etype, evalue, tb, tb_offset=None,
592 def structured_traceback(self, etype, evalue, tb, tb_offset=None,
593 context=5, mode=None):
593 context=5, mode=None):
594 """Return a list of traceback frames.
594 """Return a list of traceback frames.
595
595
596 Must be implemented by each class.
596 Must be implemented by each class.
597 """
597 """
598 raise NotImplementedError()
598 raise NotImplementedError()
599
599
600
600
601 #---------------------------------------------------------------------------
601 #---------------------------------------------------------------------------
602 class ListTB(TBTools):
602 class ListTB(TBTools):
603 """Print traceback information from a traceback list, with optional color.
603 """Print traceback information from a traceback list, with optional color.
604
604
605 Calling requires 3 arguments: (etype, evalue, elist)
605 Calling requires 3 arguments: (etype, evalue, elist)
606 as would be obtained by::
606 as would be obtained by::
607
607
608 etype, evalue, tb = sys.exc_info()
608 etype, evalue, tb = sys.exc_info()
609 if tb:
609 if tb:
610 elist = traceback.extract_tb(tb)
610 elist = traceback.extract_tb(tb)
611 else:
611 else:
612 elist = None
612 elist = None
613
613
614 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
615 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
616 standard library).
616 standard library).
617
617
618 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
619 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."""
620
620
621 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):
622 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
622 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
623 ostream=ostream, parent=parent,config=config)
623 ostream=ostream, parent=parent,config=config)
624
624
625 def __call__(self, etype, value, elist):
625 def __call__(self, etype, value, elist):
626 self.ostream.flush()
626 self.ostream.flush()
627 self.ostream.write(self.text(etype, value, elist))
627 self.ostream.write(self.text(etype, value, elist))
628 self.ostream.write('\n')
628 self.ostream.write('\n')
629
629
630 def _extract_tb(self, tb):
631 if tb:
632 return traceback.extract_tb(tb)
633 else:
634 return None
635
630 def structured_traceback(self, etype, evalue, etb=None, tb_offset=None,
636 def structured_traceback(self, etype, evalue, etb=None, tb_offset=None,
631 context=5):
637 context=5):
632 """Return a color formatted string with the traceback info.
638 """Return a color formatted string with the traceback info.
633
639
634 Parameters
640 Parameters
635 ----------
641 ----------
636 etype : exception type
642 etype : exception type
637 Type of the exception raised.
643 Type of the exception raised.
638
644
639 evalue : object
645 evalue : object
640 Data stored in the exception
646 Data stored in the exception
641
647
642 etb : object
648 etb : object
643 If list: List of frames, see class docstring for details.
649 If list: List of frames, see class docstring for details.
644 If Traceback: Traceback of the exception.
650 If Traceback: Traceback of the exception.
645
651
646 tb_offset : int, optional
652 tb_offset : int, optional
647 Number of frames in the traceback to skip. If not given, the
653 Number of frames in the traceback to skip. If not given, the
648 instance evalue is used (set in constructor).
654 instance evalue is used (set in constructor).
649
655
650 context : int, optional
656 context : int, optional
651 Number of lines of context information to print.
657 Number of lines of context information to print.
652
658
653 Returns
659 Returns
654 -------
660 -------
655 String with formatted exception.
661 String with formatted exception.
656 """
662 """
657 # This is a workaround to get chained_exc_ids in recursive calls
663 # This is a workaround to get chained_exc_ids in recursive calls
658 # etb should not be a tuple if structured_traceback is not recursive
664 # etb should not be a tuple if structured_traceback is not recursive
659 if isinstance(etb, tuple):
665 if isinstance(etb, tuple):
660 etb, chained_exc_ids = etb
666 etb, chained_exc_ids = etb
661 else:
667 else:
662 chained_exc_ids = set()
668 chained_exc_ids = set()
663
669
664 if isinstance(etb, list):
670 if isinstance(etb, list):
665 elist = etb
671 elist = etb
666 elif etb is not None:
672 elif etb is not None:
667 elist = self._extract_tb(etb)
673 elist = self._extract_tb(etb)
668 else:
674 else:
669 elist = []
675 elist = []
670 tb_offset = self.tb_offset if tb_offset is None else tb_offset
676 tb_offset = self.tb_offset if tb_offset is None else tb_offset
671 Colors = self.Colors
677 Colors = self.Colors
672 out_list = []
678 out_list = []
673 if elist:
679 if elist:
674
680
675 if tb_offset and len(elist) > tb_offset:
681 if tb_offset and len(elist) > tb_offset:
676 elist = elist[tb_offset:]
682 elist = elist[tb_offset:]
677
683
678 out_list.append('Traceback %s(most recent call last)%s:' %
684 out_list.append('Traceback %s(most recent call last)%s:' %
679 (Colors.normalEm, Colors.Normal) + '\n')
685 (Colors.normalEm, Colors.Normal) + '\n')
680 out_list.extend(self._format_list(elist))
686 out_list.extend(self._format_list(elist))
681 # The exception info should be a single entry in the list.
687 # The exception info should be a single entry in the list.
682 lines = ''.join(self._format_exception_only(etype, evalue))
688 lines = ''.join(self._format_exception_only(etype, evalue))
683 out_list.append(lines)
689 out_list.append(lines)
684
690
685 exception = self.get_parts_of_chained_exception(evalue)
691 exception = self.get_parts_of_chained_exception(evalue)
686
692
687 if exception and not id(exception[1]) in chained_exc_ids:
693 if exception and not id(exception[1]) in chained_exc_ids:
688 chained_exception_message = self.prepare_chained_exception_message(
694 chained_exception_message = self.prepare_chained_exception_message(
689 evalue.__cause__)[0]
695 evalue.__cause__)[0]
690 etype, evalue, etb = exception
696 etype, evalue, etb = exception
691 # Trace exception to avoid infinite 'cause' loop
697 # Trace exception to avoid infinite 'cause' loop
692 chained_exc_ids.add(id(exception[1]))
698 chained_exc_ids.add(id(exception[1]))
693 chained_exceptions_tb_offset = 0
699 chained_exceptions_tb_offset = 0
694 out_list = (
700 out_list = (
695 self.structured_traceback(
701 self.structured_traceback(
696 etype, evalue, (etb, chained_exc_ids),
702 etype, evalue, (etb, chained_exc_ids),
697 chained_exceptions_tb_offset, context)
703 chained_exceptions_tb_offset, context)
698 + chained_exception_message
704 + chained_exception_message
699 + out_list)
705 + out_list)
700
706
701 return out_list
707 return out_list
702
708
703 def _format_list(self, extracted_list):
709 def _format_list(self, extracted_list):
704 """Format a list of traceback entry tuples for printing.
710 """Format a list of traceback entry tuples for printing.
705
711
706 Given a list of tuples as returned by extract_tb() or
712 Given a list of tuples as returned by extract_tb() or
707 extract_stack(), return a list of strings ready for printing.
713 extract_stack(), return a list of strings ready for printing.
708 Each string in the resulting list corresponds to the item with the
714 Each string in the resulting list corresponds to the item with the
709 same index in the argument list. Each string ends in a newline;
715 same index in the argument list. Each string ends in a newline;
710 the strings may contain internal newlines as well, for those items
716 the strings may contain internal newlines as well, for those items
711 whose source text line is not None.
717 whose source text line is not None.
712
718
713 Lifted almost verbatim from traceback.py
719 Lifted almost verbatim from traceback.py
714 """
720 """
715
721
716 Colors = self.Colors
722 Colors = self.Colors
717 list = []
723 list = []
718 for filename, lineno, name, line in extracted_list[:-1]:
724 for filename, lineno, name, line in extracted_list[:-1]:
719 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
725 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
720 (Colors.filename, filename, Colors.Normal,
726 (Colors.filename, filename, Colors.Normal,
721 Colors.lineno, lineno, Colors.Normal,
727 Colors.lineno, lineno, Colors.Normal,
722 Colors.name, name, Colors.Normal)
728 Colors.name, name, Colors.Normal)
723 if line:
729 if line:
724 item += ' %s\n' % line.strip()
730 item += ' %s\n' % line.strip()
725 list.append(item)
731 list.append(item)
726 # Emphasize the last entry
732 # Emphasize the last entry
727 filename, lineno, name, line = extracted_list[-1]
733 filename, lineno, name, line = extracted_list[-1]
728 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
734 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
729 (Colors.normalEm,
735 (Colors.normalEm,
730 Colors.filenameEm, filename, Colors.normalEm,
736 Colors.filenameEm, filename, Colors.normalEm,
731 Colors.linenoEm, lineno, Colors.normalEm,
737 Colors.linenoEm, lineno, Colors.normalEm,
732 Colors.nameEm, name, Colors.normalEm,
738 Colors.nameEm, name, Colors.normalEm,
733 Colors.Normal)
739 Colors.Normal)
734 if line:
740 if line:
735 item += '%s %s%s\n' % (Colors.line, line.strip(),
741 item += '%s %s%s\n' % (Colors.line, line.strip(),
736 Colors.Normal)
742 Colors.Normal)
737 list.append(item)
743 list.append(item)
738 return list
744 return list
739
745
740 def _format_exception_only(self, etype, value):
746 def _format_exception_only(self, etype, value):
741 """Format the exception part of a traceback.
747 """Format the exception part of a traceback.
742
748
743 The arguments are the exception type and value such as given by
749 The arguments are the exception type and value such as given by
744 sys.exc_info()[:2]. The return value is a list of strings, each ending
750 sys.exc_info()[:2]. The return value is a list of strings, each ending
745 in a newline. Normally, the list contains a single string; however,
751 in a newline. Normally, the list contains a single string; however,
746 for SyntaxError exceptions, it contains several lines that (when
752 for SyntaxError exceptions, it contains several lines that (when
747 printed) display detailed information about where the syntax error
753 printed) display detailed information about where the syntax error
748 occurred. The message indicating which exception occurred is the
754 occurred. The message indicating which exception occurred is the
749 always last string in the list.
755 always last string in the list.
750
756
751 Also lifted nearly verbatim from traceback.py
757 Also lifted nearly verbatim from traceback.py
752 """
758 """
753 have_filedata = False
759 have_filedata = False
754 Colors = self.Colors
760 Colors = self.Colors
755 list = []
761 list = []
756 stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal)
762 stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal)
757 if value is None:
763 if value is None:
758 # Not sure if this can still happen in Python 2.6 and above
764 # Not sure if this can still happen in Python 2.6 and above
759 list.append(stype + '\n')
765 list.append(stype + '\n')
760 else:
766 else:
761 if issubclass(etype, SyntaxError):
767 if issubclass(etype, SyntaxError):
762 have_filedata = True
768 have_filedata = True
763 if not value.filename: value.filename = "<string>"
769 if not value.filename: value.filename = "<string>"
764 if value.lineno:
770 if value.lineno:
765 lineno = value.lineno
771 lineno = value.lineno
766 textline = linecache.getline(value.filename, value.lineno)
772 textline = linecache.getline(value.filename, value.lineno)
767 else:
773 else:
768 lineno = 'unknown'
774 lineno = 'unknown'
769 textline = ''
775 textline = ''
770 list.append('%s File %s"%s"%s, line %s%s%s\n' % \
776 list.append('%s File %s"%s"%s, line %s%s%s\n' % \
771 (Colors.normalEm,
777 (Colors.normalEm,
772 Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm,
778 Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm,
773 Colors.linenoEm, lineno, Colors.Normal ))
779 Colors.linenoEm, lineno, Colors.Normal ))
774 if textline == '':
780 if textline == '':
775 textline = py3compat.cast_unicode(value.text, "utf-8")
781 textline = py3compat.cast_unicode(value.text, "utf-8")
776
782
777 if textline is not None:
783 if textline is not None:
778 i = 0
784 i = 0
779 while i < len(textline) and textline[i].isspace():
785 while i < len(textline) and textline[i].isspace():
780 i += 1
786 i += 1
781 list.append('%s %s%s\n' % (Colors.line,
787 list.append('%s %s%s\n' % (Colors.line,
782 textline.strip(),
788 textline.strip(),
783 Colors.Normal))
789 Colors.Normal))
784 if value.offset is not None:
790 if value.offset is not None:
785 s = ' '
791 s = ' '
786 for c in textline[i:value.offset - 1]:
792 for c in textline[i:value.offset - 1]:
787 if c.isspace():
793 if c.isspace():
788 s += c
794 s += c
789 else:
795 else:
790 s += ' '
796 s += ' '
791 list.append('%s%s^%s\n' % (Colors.caret, s,
797 list.append('%s%s^%s\n' % (Colors.caret, s,
792 Colors.Normal))
798 Colors.Normal))
793
799
794 try:
800 try:
795 s = value.msg
801 s = value.msg
796 except Exception:
802 except Exception:
797 s = self._some_str(value)
803 s = self._some_str(value)
798 if s:
804 if s:
799 list.append('%s%s:%s %s\n' % (stype, Colors.excName,
805 list.append('%s%s:%s %s\n' % (stype, Colors.excName,
800 Colors.Normal, s))
806 Colors.Normal, s))
801 else:
807 else:
802 list.append('%s\n' % stype)
808 list.append('%s\n' % stype)
803
809
804 # sync with user hooks
810 # sync with user hooks
805 if have_filedata:
811 if have_filedata:
806 ipinst = get_ipython()
812 ipinst = get_ipython()
807 if ipinst is not None:
813 if ipinst is not None:
808 ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
814 ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
809
815
810 return list
816 return list
811
817
812 def get_exception_only(self, etype, value):
818 def get_exception_only(self, etype, value):
813 """Only print the exception type and message, without a traceback.
819 """Only print the exception type and message, without a traceback.
814
820
815 Parameters
821 Parameters
816 ----------
822 ----------
817 etype : exception type
823 etype : exception type
818 value : exception value
824 value : exception value
819 """
825 """
820 return ListTB.structured_traceback(self, etype, value)
826 return ListTB.structured_traceback(self, etype, value)
821
827
822 def show_exception_only(self, etype, evalue):
828 def show_exception_only(self, etype, evalue):
823 """Only print the exception type and message, without a traceback.
829 """Only print the exception type and message, without a traceback.
824
830
825 Parameters
831 Parameters
826 ----------
832 ----------
827 etype : exception type
833 etype : exception type
828 value : exception value
834 value : exception value
829 """
835 """
830 # This method needs to use __call__ from *this* class, not the one from
836 # This method needs to use __call__ from *this* class, not the one from
831 # a subclass whose signature or behavior may be different
837 # a subclass whose signature or behavior may be different
832 ostream = self.ostream
838 ostream = self.ostream
833 ostream.flush()
839 ostream.flush()
834 ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
840 ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
835 ostream.flush()
841 ostream.flush()
836
842
837 def _some_str(self, value):
843 def _some_str(self, value):
838 # Lifted from traceback.py
844 # Lifted from traceback.py
839 try:
845 try:
840 return py3compat.cast_unicode(str(value))
846 return py3compat.cast_unicode(str(value))
841 except:
847 except:
842 return u'<unprintable %s object>' % type(value).__name__
848 return u'<unprintable %s object>' % type(value).__name__
843
849
844
850
845 #----------------------------------------------------------------------------
851 #----------------------------------------------------------------------------
846 class VerboseTB(TBTools):
852 class VerboseTB(TBTools):
847 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
853 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
848 of HTML. Requires inspect and pydoc. Crazy, man.
854 of HTML. Requires inspect and pydoc. Crazy, man.
849
855
850 Modified version which optionally strips the topmost entries from the
856 Modified version which optionally strips the topmost entries from the
851 traceback, to be used with alternate interpreters (because their own code
857 traceback, to be used with alternate interpreters (because their own code
852 would appear in the traceback)."""
858 would appear in the traceback)."""
853
859
854 def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None,
860 def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None,
855 tb_offset=0, long_header=False, include_vars=True,
861 tb_offset=0, long_header=False, include_vars=True,
856 check_cache=None, debugger_cls = None,
862 check_cache=None, debugger_cls = None,
857 parent=None, config=None):
863 parent=None, config=None):
858 """Specify traceback offset, headers and color scheme.
864 """Specify traceback offset, headers and color scheme.
859
865
860 Define how many frames to drop from the tracebacks. Calling it with
866 Define how many frames to drop from the tracebacks. Calling it with
861 tb_offset=1 allows use of this handler in interpreters which will have
867 tb_offset=1 allows use of this handler in interpreters which will have
862 their own code at the top of the traceback (VerboseTB will first
868 their own code at the top of the traceback (VerboseTB will first
863 remove that frame before printing the traceback info)."""
869 remove that frame before printing the traceback info)."""
864 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
870 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
865 ostream=ostream, parent=parent, config=config)
871 ostream=ostream, parent=parent, config=config)
866 self.tb_offset = tb_offset
872 self.tb_offset = tb_offset
867 self.long_header = long_header
873 self.long_header = long_header
868 self.include_vars = include_vars
874 self.include_vars = include_vars
869 # By default we use linecache.checkcache, but the user can provide a
875 # By default we use linecache.checkcache, but the user can provide a
870 # different check_cache implementation. This is used by the IPython
876 # different check_cache implementation. This is used by the IPython
871 # kernel to provide tracebacks for interactive code that is cached,
877 # kernel to provide tracebacks for interactive code that is cached,
872 # by a compiler instance that flushes the linecache but preserves its
878 # by a compiler instance that flushes the linecache but preserves its
873 # own code cache.
879 # own code cache.
874 if check_cache is None:
880 if check_cache is None:
875 check_cache = linecache.checkcache
881 check_cache = linecache.checkcache
876 self.check_cache = check_cache
882 self.check_cache = check_cache
877
883
878 self.debugger_cls = debugger_cls or debugger.Pdb
884 self.debugger_cls = debugger_cls or debugger.Pdb
879
885
880 def format_records(self, records, last_unique, recursion_repeat):
886 def format_records(self, records, last_unique, recursion_repeat):
881 """Format the stack frames of the traceback"""
887 """Format the stack frames of the traceback"""
882 frames = []
888 frames = []
883 for r in records[:last_unique+recursion_repeat+1]:
889 for r in records[:last_unique+recursion_repeat+1]:
884 #print '*** record:',file,lnum,func,lines,index # dbg
890 #print '*** record:',file,lnum,func,lines,index # dbg
885 frames.append(self.format_record(*r))
891 frames.append(self.format_record(*r))
886
892
887 if recursion_repeat:
893 if recursion_repeat:
888 frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat)
894 frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat)
889 frames.append(self.format_record(*records[last_unique+recursion_repeat+1]))
895 frames.append(self.format_record(*records[last_unique+recursion_repeat+1]))
890
896
891 return frames
897 return frames
892
898
893 def format_record(self, frame, file, lnum, func, lines, index):
899 def format_record(self, frame, file, lnum, func, lines, index):
894 """Format a single stack frame"""
900 """Format a single stack frame"""
895 Colors = self.Colors # just a shorthand + quicker name lookup
901 Colors = self.Colors # just a shorthand + quicker name lookup
896 ColorsNormal = Colors.Normal # used a lot
902 ColorsNormal = Colors.Normal # used a lot
897 col_scheme = self.color_scheme_table.active_scheme_name
903 col_scheme = self.color_scheme_table.active_scheme_name
898 indent = ' ' * INDENT_SIZE
904 indent = ' ' * INDENT_SIZE
899 em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
905 em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
900 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
906 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
901 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
907 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
902 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
908 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
903 ColorsNormal)
909 ColorsNormal)
904 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
910 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
905 (Colors.vName, Colors.valEm, ColorsNormal)
911 (Colors.vName, Colors.valEm, ColorsNormal)
906 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
912 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
907 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
913 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
908 Colors.vName, ColorsNormal)
914 Colors.vName, ColorsNormal)
909 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
915 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
910
916
911 if not file:
917 if not file:
912 file = '?'
918 file = '?'
913 elif file.startswith(str("<")) and file.endswith(str(">")):
919 elif file.startswith(str("<")) and file.endswith(str(">")):
914 # Not a real filename, no problem...
920 # Not a real filename, no problem...
915 pass
921 pass
916 elif not os.path.isabs(file):
922 elif not os.path.isabs(file):
917 # Try to make the filename absolute by trying all
923 # Try to make the filename absolute by trying all
918 # sys.path entries (which is also what linecache does)
924 # sys.path entries (which is also what linecache does)
919 for dirname in sys.path:
925 for dirname in sys.path:
920 try:
926 try:
921 fullname = os.path.join(dirname, file)
927 fullname = os.path.join(dirname, file)
922 if os.path.isfile(fullname):
928 if os.path.isfile(fullname):
923 file = os.path.abspath(fullname)
929 file = os.path.abspath(fullname)
924 break
930 break
925 except Exception:
931 except Exception:
926 # Just in case that sys.path contains very
932 # Just in case that sys.path contains very
927 # strange entries...
933 # strange entries...
928 pass
934 pass
929
935
930 file = py3compat.cast_unicode(file, util_path.fs_encoding)
936 file = py3compat.cast_unicode(file, util_path.fs_encoding)
931 link = tpl_link % util_path.compress_user(file)
937 link = tpl_link % util_path.compress_user(file)
932 args, varargs, varkw, locals_ = inspect.getargvalues(frame)
938 args, varargs, varkw, locals_ = inspect.getargvalues(frame)
933
939
934 if func == '?':
940 if func == '?':
935 call = ''
941 call = ''
936 elif func == '<module>':
942 elif func == '<module>':
937 call = tpl_call % (func, '')
943 call = tpl_call % (func, '')
938 else:
944 else:
939 # Decide whether to include variable details or not
945 # Decide whether to include variable details or not
940 var_repr = eqrepr if self.include_vars else nullrepr
946 var_repr = eqrepr if self.include_vars else nullrepr
941 try:
947 try:
942 call = tpl_call % (func, inspect.formatargvalues(args,
948 call = tpl_call % (func, inspect.formatargvalues(args,
943 varargs, varkw,
949 varargs, varkw,
944 locals_, formatvalue=var_repr))
950 locals_, formatvalue=var_repr))
945 except KeyError:
951 except KeyError:
946 # This happens in situations like errors inside generator
952 # This happens in situations like errors inside generator
947 # expressions, where local variables are listed in the
953 # expressions, where local variables are listed in the
948 # line, but can't be extracted from the frame. I'm not
954 # line, but can't be extracted from the frame. I'm not
949 # 100% sure this isn't actually a bug in inspect itself,
955 # 100% sure this isn't actually a bug in inspect itself,
950 # but since there's no info for us to compute with, the
956 # but since there's no info for us to compute with, the
951 # best we can do is report the failure and move on. Here
957 # best we can do is report the failure and move on. Here
952 # we must *not* call any traceback construction again,
958 # we must *not* call any traceback construction again,
953 # because that would mess up use of %debug later on. So we
959 # because that would mess up use of %debug later on. So we
954 # simply report the failure and move on. The only
960 # simply report the failure and move on. The only
955 # limitation will be that this frame won't have locals
961 # limitation will be that this frame won't have locals
956 # listed in the call signature. Quite subtle problem...
962 # listed in the call signature. Quite subtle problem...
957 # I can't think of a good way to validate this in a unit
963 # I can't think of a good way to validate this in a unit
958 # test, but running a script consisting of:
964 # test, but running a script consisting of:
959 # dict( (k,v.strip()) for (k,v) in range(10) )
965 # dict( (k,v.strip()) for (k,v) in range(10) )
960 # will illustrate the error, if this exception catch is
966 # will illustrate the error, if this exception catch is
961 # disabled.
967 # disabled.
962 call = tpl_call_fail % func
968 call = tpl_call_fail % func
963
969
964 # Don't attempt to tokenize binary files.
970 # Don't attempt to tokenize binary files.
965 if file.endswith(('.so', '.pyd', '.dll')):
971 if file.endswith(('.so', '.pyd', '.dll')):
966 return '%s %s\n' % (link, call)
972 return '%s %s\n' % (link, call)
967
973
968 elif file.endswith(('.pyc', '.pyo')):
974 elif file.endswith(('.pyc', '.pyo')):
969 # Look up the corresponding source file.
975 # Look up the corresponding source file.
970 try:
976 try:
971 file = source_from_cache(file)
977 file = source_from_cache(file)
972 except ValueError:
978 except ValueError:
973 # Failed to get the source file for some reason
979 # Failed to get the source file for some reason
974 # E.g. https://github.com/ipython/ipython/issues/9486
980 # E.g. https://github.com/ipython/ipython/issues/9486
975 return '%s %s\n' % (link, call)
981 return '%s %s\n' % (link, call)
976
982
977 def linereader(file=file, lnum=[lnum], getline=linecache.getline):
983 def linereader(file=file, lnum=[lnum], getline=linecache.getline):
978 line = getline(file, lnum[0])
984 line = getline(file, lnum[0])
979 lnum[0] += 1
985 lnum[0] += 1
980 return line
986 return line
981
987
982 # Build the list of names on this line of code where the exception
988 # Build the list of names on this line of code where the exception
983 # occurred.
989 # occurred.
984 try:
990 try:
985 names = []
991 names = []
986 name_cont = False
992 name_cont = False
987
993
988 for token_type, token, start, end, line in generate_tokens(linereader):
994 for token_type, token, start, end, line in generate_tokens(linereader):
989 # build composite names
995 # build composite names
990 if token_type == tokenize.NAME and token not in keyword.kwlist:
996 if token_type == tokenize.NAME and token not in keyword.kwlist:
991 if name_cont:
997 if name_cont:
992 # Continuation of a dotted name
998 # Continuation of a dotted name
993 try:
999 try:
994 names[-1].append(token)
1000 names[-1].append(token)
995 except IndexError:
1001 except IndexError:
996 names.append([token])
1002 names.append([token])
997 name_cont = False
1003 name_cont = False
998 else:
1004 else:
999 # Regular new names. We append everything, the caller
1005 # Regular new names. We append everything, the caller
1000 # will be responsible for pruning the list later. It's
1006 # will be responsible for pruning the list later. It's
1001 # very tricky to try to prune as we go, b/c composite
1007 # very tricky to try to prune as we go, b/c composite
1002 # names can fool us. The pruning at the end is easy
1008 # names can fool us. The pruning at the end is easy
1003 # to do (or the caller can print a list with repeated
1009 # to do (or the caller can print a list with repeated
1004 # names if so desired.
1010 # names if so desired.
1005 names.append([token])
1011 names.append([token])
1006 elif token == '.':
1012 elif token == '.':
1007 name_cont = True
1013 name_cont = True
1008 elif token_type == tokenize.NEWLINE:
1014 elif token_type == tokenize.NEWLINE:
1009 break
1015 break
1010
1016
1011 except (IndexError, UnicodeDecodeError, SyntaxError):
1017 except (IndexError, UnicodeDecodeError, SyntaxError):
1012 # signals exit of tokenizer
1018 # signals exit of tokenizer
1013 # SyntaxError can occur if the file is not actually Python
1019 # SyntaxError can occur if the file is not actually Python
1014 # - see gh-6300
1020 # - see gh-6300
1015 pass
1021 pass
1016 except tokenize.TokenError as msg:
1022 except tokenize.TokenError as msg:
1017 # Tokenizing may fail for various reasons, many of which are
1023 # Tokenizing may fail for various reasons, many of which are
1018 # harmless. (A good example is when the line in question is the
1024 # harmless. (A good example is when the line in question is the
1019 # close of a triple-quoted string, cf gh-6864). We don't want to
1025 # close of a triple-quoted string, cf gh-6864). We don't want to
1020 # show this to users, but want make it available for debugging
1026 # show this to users, but want make it available for debugging
1021 # purposes.
1027 # purposes.
1022 _m = ("An unexpected error occurred while tokenizing input\n"
1028 _m = ("An unexpected error occurred while tokenizing input\n"
1023 "The following traceback may be corrupted or invalid\n"
1029 "The following traceback may be corrupted or invalid\n"
1024 "The error message is: %s\n" % msg)
1030 "The error message is: %s\n" % msg)
1025 debug(_m)
1031 debug(_m)
1026
1032
1027 # Join composite names (e.g. "dict.fromkeys")
1033 # Join composite names (e.g. "dict.fromkeys")
1028 names = ['.'.join(n) for n in names]
1034 names = ['.'.join(n) for n in names]
1029 # prune names list of duplicates, but keep the right order
1035 # prune names list of duplicates, but keep the right order
1030 unique_names = uniq_stable(names)
1036 unique_names = uniq_stable(names)
1031
1037
1032 # Start loop over vars
1038 # Start loop over vars
1033 lvals = ''
1039 lvals = ''
1034 lvals_list = []
1040 lvals_list = []
1035 if self.include_vars:
1041 if self.include_vars:
1036 for name_full in unique_names:
1042 for name_full in unique_names:
1037 name_base = name_full.split('.', 1)[0]
1043 name_base = name_full.split('.', 1)[0]
1038 if name_base in frame.f_code.co_varnames:
1044 if name_base in frame.f_code.co_varnames:
1039 if name_base in locals_:
1045 if name_base in locals_:
1040 try:
1046 try:
1041 value = repr(eval(name_full, locals_))
1047 value = repr(eval(name_full, locals_))
1042 except:
1048 except:
1043 value = undefined
1049 value = undefined
1044 else:
1050 else:
1045 value = undefined
1051 value = undefined
1046 name = tpl_local_var % name_full
1052 name = tpl_local_var % name_full
1047 else:
1053 else:
1048 if name_base in frame.f_globals:
1054 if name_base in frame.f_globals:
1049 try:
1055 try:
1050 value = repr(eval(name_full, frame.f_globals))
1056 value = repr(eval(name_full, frame.f_globals))
1051 except:
1057 except:
1052 value = undefined
1058 value = undefined
1053 else:
1059 else:
1054 value = undefined
1060 value = undefined
1055 name = tpl_global_var % name_full
1061 name = tpl_global_var % name_full
1056 lvals_list.append(tpl_name_val % (name, value))
1062 lvals_list.append(tpl_name_val % (name, value))
1057 if lvals_list:
1063 if lvals_list:
1058 lvals = '%s%s' % (indent, em_normal.join(lvals_list))
1064 lvals = '%s%s' % (indent, em_normal.join(lvals_list))
1059
1065
1060 level = '%s %s\n' % (link, call)
1066 level = '%s %s\n' % (link, call)
1061
1067
1062 if index is None:
1068 if index is None:
1063 return level
1069 return level
1064 else:
1070 else:
1065 _line_format = PyColorize.Parser(style=col_scheme, parent=self).format2
1071 _line_format = PyColorize.Parser(style=col_scheme, parent=self).format2
1066 return '%s%s' % (level, ''.join(
1072 return '%s%s' % (level, ''.join(
1067 _format_traceback_lines(lnum, index, lines, Colors, lvals,
1073 _format_traceback_lines(lnum, index, lines, Colors, lvals,
1068 _line_format)))
1074 _line_format)))
1069
1075
1070 def prepare_header(self, etype, long_version=False):
1076 def prepare_header(self, etype, long_version=False):
1071 colors = self.Colors # just a shorthand + quicker name lookup
1077 colors = self.Colors # just a shorthand + quicker name lookup
1072 colorsnormal = colors.Normal # used a lot
1078 colorsnormal = colors.Normal # used a lot
1073 exc = '%s%s%s' % (colors.excName, etype, colorsnormal)
1079 exc = '%s%s%s' % (colors.excName, etype, colorsnormal)
1074 width = min(75, get_terminal_size()[0])
1080 width = min(75, get_terminal_size()[0])
1075 if long_version:
1081 if long_version:
1076 # Header with the exception type, python version, and date
1082 # Header with the exception type, python version, and date
1077 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
1083 pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
1078 date = time.ctime(time.time())
1084 date = time.ctime(time.time())
1079
1085
1080 head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal,
1086 head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal,
1081 exc, ' ' * (width - len(str(etype)) - len(pyver)),
1087 exc, ' ' * (width - len(str(etype)) - len(pyver)),
1082 pyver, date.rjust(width) )
1088 pyver, date.rjust(width) )
1083 head += "\nA problem occurred executing Python code. Here is the sequence of function" \
1089 head += "\nA problem occurred executing Python code. Here is the sequence of function" \
1084 "\ncalls leading up to the error, with the most recent (innermost) call last."
1090 "\ncalls leading up to the error, with the most recent (innermost) call last."
1085 else:
1091 else:
1086 # Simplified header
1092 # Simplified header
1087 head = '%s%s' % (exc, 'Traceback (most recent call last)'. \
1093 head = '%s%s' % (exc, 'Traceback (most recent call last)'. \
1088 rjust(width - len(str(etype))) )
1094 rjust(width - len(str(etype))) )
1089
1095
1090 return head
1096 return head
1091
1097
1092 def format_exception(self, etype, evalue):
1098 def format_exception(self, etype, evalue):
1093 colors = self.Colors # just a shorthand + quicker name lookup
1099 colors = self.Colors # just a shorthand + quicker name lookup
1094 colorsnormal = colors.Normal # used a lot
1100 colorsnormal = colors.Normal # used a lot
1095 # Get (safely) a string form of the exception info
1101 # Get (safely) a string form of the exception info
1096 try:
1102 try:
1097 etype_str, evalue_str = map(str, (etype, evalue))
1103 etype_str, evalue_str = map(str, (etype, evalue))
1098 except:
1104 except:
1099 # User exception is improperly defined.
1105 # User exception is improperly defined.
1100 etype, evalue = str, sys.exc_info()[:2]
1106 etype, evalue = str, sys.exc_info()[:2]
1101 etype_str, evalue_str = map(str, (etype, evalue))
1107 etype_str, evalue_str = map(str, (etype, evalue))
1102 # ... and format it
1108 # ... and format it
1103 return ['%s%s%s: %s' % (colors.excName, etype_str,
1109 return ['%s%s%s: %s' % (colors.excName, etype_str,
1104 colorsnormal, py3compat.cast_unicode(evalue_str))]
1110 colorsnormal, py3compat.cast_unicode(evalue_str))]
1105
1111
1106 def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset):
1112 def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset):
1107 """Formats the header, traceback and exception message for a single exception.
1113 """Formats the header, traceback and exception message for a single exception.
1108
1114
1109 This may be called multiple times by Python 3 exception chaining
1115 This may be called multiple times by Python 3 exception chaining
1110 (PEP 3134).
1116 (PEP 3134).
1111 """
1117 """
1112 # some locals
1118 # some locals
1113 orig_etype = etype
1119 orig_etype = etype
1114 try:
1120 try:
1115 etype = etype.__name__
1121 etype = etype.__name__
1116 except AttributeError:
1122 except AttributeError:
1117 pass
1123 pass
1118
1124
1119 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1125 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1120 head = self.prepare_header(etype, self.long_header)
1126 head = self.prepare_header(etype, self.long_header)
1121 records = self.get_records(etb, number_of_lines_of_context, tb_offset)
1127 records = self.get_records(etb, number_of_lines_of_context, tb_offset)
1122
1128
1123 if records is None:
1129 if records is None:
1124 return ""
1130 return ""
1125
1131
1126 last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
1132 last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
1127
1133
1128 frames = self.format_records(records, last_unique, recursion_repeat)
1134 frames = self.format_records(records, last_unique, recursion_repeat)
1129
1135
1130 formatted_exception = self.format_exception(etype, evalue)
1136 formatted_exception = self.format_exception(etype, evalue)
1131 if records:
1137 if records:
1132 filepath, lnum = records[-1][1:3]
1138 filepath, lnum = records[-1][1:3]
1133 filepath = os.path.abspath(filepath)
1139 filepath = os.path.abspath(filepath)
1134 ipinst = get_ipython()
1140 ipinst = get_ipython()
1135 if ipinst is not None:
1141 if ipinst is not None:
1136 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
1142 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
1137
1143
1138 return [[head] + frames + [''.join(formatted_exception[0])]]
1144 return [[head] + frames + [''.join(formatted_exception[0])]]
1139
1145
1140 def get_records(self, etb, number_of_lines_of_context, tb_offset):
1146 def get_records(self, etb, number_of_lines_of_context, tb_offset):
1141 try:
1147 try:
1142 # Try the default getinnerframes and Alex's: Alex's fixes some
1148 # Try the default getinnerframes and Alex's: Alex's fixes some
1143 # problems, but it generates empty tracebacks for console errors
1149 # problems, but it generates empty tracebacks for console errors
1144 # (5 blanks lines) where none should be returned.
1150 # (5 blanks lines) where none should be returned.
1145 return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
1151 return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
1146 except UnicodeDecodeError:
1152 except UnicodeDecodeError:
1147 # This can occur if a file's encoding magic comment is wrong.
1153 # This can occur if a file's encoding magic comment is wrong.
1148 # I can't see a way to recover without duplicating a bunch of code
1154 # I can't see a way to recover without duplicating a bunch of code
1149 # from the stdlib traceback module. --TK
1155 # from the stdlib traceback module. --TK
1150 error('\nUnicodeDecodeError while processing traceback.\n')
1156 error('\nUnicodeDecodeError while processing traceback.\n')
1151 return None
1157 return None
1152 except:
1158 except:
1153 # FIXME: I've been getting many crash reports from python 2.3
1159 # FIXME: I've been getting many crash reports from python 2.3
1154 # users, traceable to inspect.py. If I can find a small test-case
1160 # users, traceable to inspect.py. If I can find a small test-case
1155 # to reproduce this, I should either write a better workaround or
1161 # to reproduce this, I should either write a better workaround or
1156 # file a bug report against inspect (if that's the real problem).
1162 # file a bug report against inspect (if that's the real problem).
1157 # So far, I haven't been able to find an isolated example to
1163 # So far, I haven't been able to find an isolated example to
1158 # reproduce the problem.
1164 # reproduce the problem.
1159 inspect_error()
1165 inspect_error()
1160 traceback.print_exc(file=self.ostream)
1166 traceback.print_exc(file=self.ostream)
1161 info('\nUnfortunately, your original traceback can not be constructed.\n')
1167 info('\nUnfortunately, your original traceback can not be constructed.\n')
1162 return None
1168 return None
1163
1169
1164 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
1170 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
1165 number_of_lines_of_context=5):
1171 number_of_lines_of_context=5):
1166 """Return a nice text document describing the traceback."""
1172 """Return a nice text document describing the traceback."""
1167
1173
1168 formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
1174 formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
1169 tb_offset)
1175 tb_offset)
1170
1176
1171 colors = self.Colors # just a shorthand + quicker name lookup
1177 colors = self.Colors # just a shorthand + quicker name lookup
1172 colorsnormal = colors.Normal # used a lot
1178 colorsnormal = colors.Normal # used a lot
1173 head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal)
1179 head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal)
1174 structured_traceback_parts = [head]
1180 structured_traceback_parts = [head]
1175 chained_exceptions_tb_offset = 0
1181 chained_exceptions_tb_offset = 0
1176 lines_of_context = 3
1182 lines_of_context = 3
1177 formatted_exceptions = formatted_exception
1183 formatted_exceptions = formatted_exception
1178 exception = self.get_parts_of_chained_exception(evalue)
1184 exception = self.get_parts_of_chained_exception(evalue)
1179 if exception:
1185 if exception:
1180 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1186 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1181 etype, evalue, etb = exception
1187 etype, evalue, etb = exception
1182 else:
1188 else:
1183 evalue = None
1189 evalue = None
1184 chained_exc_ids = set()
1190 chained_exc_ids = set()
1185 while evalue:
1191 while evalue:
1186 formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
1192 formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
1187 chained_exceptions_tb_offset)
1193 chained_exceptions_tb_offset)
1188 exception = self.get_parts_of_chained_exception(evalue)
1194 exception = self.get_parts_of_chained_exception(evalue)
1189
1195
1190 if exception and not id(exception[1]) in chained_exc_ids:
1196 if exception and not id(exception[1]) in chained_exc_ids:
1191 chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop
1197 chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop
1192 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1198 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1193 etype, evalue, etb = exception
1199 etype, evalue, etb = exception
1194 else:
1200 else:
1195 evalue = None
1201 evalue = None
1196
1202
1197 # we want to see exceptions in a reversed order:
1203 # we want to see exceptions in a reversed order:
1198 # the first exception should be on top
1204 # the first exception should be on top
1199 for formatted_exception in reversed(formatted_exceptions):
1205 for formatted_exception in reversed(formatted_exceptions):
1200 structured_traceback_parts += formatted_exception
1206 structured_traceback_parts += formatted_exception
1201
1207
1202 return structured_traceback_parts
1208 return structured_traceback_parts
1203
1209
1204 def debugger(self, force=False):
1210 def debugger(self, force=False):
1205 """Call up the pdb debugger if desired, always clean up the tb
1211 """Call up the pdb debugger if desired, always clean up the tb
1206 reference.
1212 reference.
1207
1213
1208 Keywords:
1214 Keywords:
1209
1215
1210 - force(False): by default, this routine checks the instance call_pdb
1216 - force(False): by default, this routine checks the instance call_pdb
1211 flag and does not actually invoke the debugger if the flag is false.
1217 flag and does not actually invoke the debugger if the flag is false.
1212 The 'force' option forces the debugger to activate even if the flag
1218 The 'force' option forces the debugger to activate even if the flag
1213 is false.
1219 is false.
1214
1220
1215 If the call_pdb flag is set, the pdb interactive debugger is
1221 If the call_pdb flag is set, the pdb interactive debugger is
1216 invoked. In all cases, the self.tb reference to the current traceback
1222 invoked. In all cases, the self.tb reference to the current traceback
1217 is deleted to prevent lingering references which hamper memory
1223 is deleted to prevent lingering references which hamper memory
1218 management.
1224 management.
1219
1225
1220 Note that each call to pdb() does an 'import readline', so if your app
1226 Note that each call to pdb() does an 'import readline', so if your app
1221 requires a special setup for the readline completers, you'll have to
1227 requires a special setup for the readline completers, you'll have to
1222 fix that by hand after invoking the exception handler."""
1228 fix that by hand after invoking the exception handler."""
1223
1229
1224 if force or self.call_pdb:
1230 if force or self.call_pdb:
1225 if self.pdb is None:
1231 if self.pdb is None:
1226 self.pdb = self.debugger_cls()
1232 self.pdb = self.debugger_cls()
1227 # the system displayhook may have changed, restore the original
1233 # the system displayhook may have changed, restore the original
1228 # for pdb
1234 # for pdb
1229 display_trap = DisplayTrap(hook=sys.__displayhook__)
1235 display_trap = DisplayTrap(hook=sys.__displayhook__)
1230 with display_trap:
1236 with display_trap:
1231 self.pdb.reset()
1237 self.pdb.reset()
1232 # Find the right frame so we don't pop up inside ipython itself
1238 # Find the right frame so we don't pop up inside ipython itself
1233 if hasattr(self, 'tb') and self.tb is not None:
1239 if hasattr(self, 'tb') and self.tb is not None:
1234 etb = self.tb
1240 etb = self.tb
1235 else:
1241 else:
1236 etb = self.tb = sys.last_traceback
1242 etb = self.tb = sys.last_traceback
1237 while self.tb is not None and self.tb.tb_next is not None:
1243 while self.tb is not None and self.tb.tb_next is not None:
1238 self.tb = self.tb.tb_next
1244 self.tb = self.tb.tb_next
1239 if etb and etb.tb_next:
1245 if etb and etb.tb_next:
1240 etb = etb.tb_next
1246 etb = etb.tb_next
1241 self.pdb.botframe = etb.tb_frame
1247 self.pdb.botframe = etb.tb_frame
1242 self.pdb.interaction(None, etb)
1248 self.pdb.interaction(None, etb)
1243
1249
1244 if hasattr(self, 'tb'):
1250 if hasattr(self, 'tb'):
1245 del self.tb
1251 del self.tb
1246
1252
1247 def handler(self, info=None):
1253 def handler(self, info=None):
1248 (etype, evalue, etb) = info or sys.exc_info()
1254 (etype, evalue, etb) = info or sys.exc_info()
1249 self.tb = etb
1255 self.tb = etb
1250 ostream = self.ostream
1256 ostream = self.ostream
1251 ostream.flush()
1257 ostream.flush()
1252 ostream.write(self.text(etype, evalue, etb))
1258 ostream.write(self.text(etype, evalue, etb))
1253 ostream.write('\n')
1259 ostream.write('\n')
1254 ostream.flush()
1260 ostream.flush()
1255
1261
1256 # Changed so an instance can just be called as VerboseTB_inst() and print
1262 # Changed so an instance can just be called as VerboseTB_inst() and print
1257 # out the right info on its own.
1263 # out the right info on its own.
1258 def __call__(self, etype=None, evalue=None, etb=None):
1264 def __call__(self, etype=None, evalue=None, etb=None):
1259 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
1265 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
1260 if etb is None:
1266 if etb is None:
1261 self.handler()
1267 self.handler()
1262 else:
1268 else:
1263 self.handler((etype, evalue, etb))
1269 self.handler((etype, evalue, etb))
1264 try:
1270 try:
1265 self.debugger()
1271 self.debugger()
1266 except KeyboardInterrupt:
1272 except KeyboardInterrupt:
1267 print("\nKeyboardInterrupt")
1273 print("\nKeyboardInterrupt")
1268
1274
1269
1275
1270 #----------------------------------------------------------------------------
1276 #----------------------------------------------------------------------------
1271 class FormattedTB(VerboseTB, ListTB):
1277 class FormattedTB(VerboseTB, ListTB):
1272 """Subclass ListTB but allow calling with a traceback.
1278 """Subclass ListTB but allow calling with a traceback.
1273
1279
1274 It can thus be used as a sys.excepthook for Python > 2.1.
1280 It can thus be used as a sys.excepthook for Python > 2.1.
1275
1281
1276 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
1282 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
1277
1283
1278 Allows a tb_offset to be specified. This is useful for situations where
1284 Allows a tb_offset to be specified. This is useful for situations where
1279 one needs to remove a number of topmost frames from the traceback (such as
1285 one needs to remove a number of topmost frames from the traceback (such as
1280 occurs with python programs that themselves execute other python code,
1286 occurs with python programs that themselves execute other python code,
1281 like Python shells). """
1287 like Python shells). """
1282
1288
1283 def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False,
1289 def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False,
1284 ostream=None,
1290 ostream=None,
1285 tb_offset=0, long_header=False, include_vars=False,
1291 tb_offset=0, long_header=False, include_vars=False,
1286 check_cache=None, debugger_cls=None,
1292 check_cache=None, debugger_cls=None,
1287 parent=None, config=None):
1293 parent=None, config=None):
1288
1294
1289 # NEVER change the order of this list. Put new modes at the end:
1295 # NEVER change the order of this list. Put new modes at the end:
1290 self.valid_modes = ['Plain', 'Context', 'Verbose', 'Minimal']
1296 self.valid_modes = ['Plain', 'Context', 'Verbose', 'Minimal']
1291 self.verbose_modes = self.valid_modes[1:3]
1297 self.verbose_modes = self.valid_modes[1:3]
1292
1298
1293 VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
1299 VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
1294 ostream=ostream, tb_offset=tb_offset,
1300 ostream=ostream, tb_offset=tb_offset,
1295 long_header=long_header, include_vars=include_vars,
1301 long_header=long_header, include_vars=include_vars,
1296 check_cache=check_cache, debugger_cls=debugger_cls,
1302 check_cache=check_cache, debugger_cls=debugger_cls,
1297 parent=parent, config=config)
1303 parent=parent, config=config)
1298
1304
1299 # Different types of tracebacks are joined with different separators to
1305 # Different types of tracebacks are joined with different separators to
1300 # form a single string. They are taken from this dict
1306 # form a single string. They are taken from this dict
1301 self._join_chars = dict(Plain='', Context='\n', Verbose='\n',
1307 self._join_chars = dict(Plain='', Context='\n', Verbose='\n',
1302 Minimal='')
1308 Minimal='')
1303 # set_mode also sets the tb_join_char attribute
1309 # set_mode also sets the tb_join_char attribute
1304 self.set_mode(mode)
1310 self.set_mode(mode)
1305
1311
1306 def _extract_tb(self, tb):
1307 if tb:
1308 return traceback.extract_tb(tb)
1309 else:
1310 return None
1311
1312 def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5):
1312 def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5):
1313 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1313 tb_offset = self.tb_offset if tb_offset is None else tb_offset
1314 mode = self.mode
1314 mode = self.mode
1315 if mode in self.verbose_modes:
1315 if mode in self.verbose_modes:
1316 # Verbose modes need a full traceback
1316 # Verbose modes need a full traceback
1317 return VerboseTB.structured_traceback(
1317 return VerboseTB.structured_traceback(
1318 self, etype, value, tb, tb_offset, number_of_lines_of_context
1318 self, etype, value, tb, tb_offset, number_of_lines_of_context
1319 )
1319 )
1320 elif mode == 'Minimal':
1320 elif mode == 'Minimal':
1321 return ListTB.get_exception_only(self, etype, value)
1321 return ListTB.get_exception_only(self, etype, value)
1322 else:
1322 else:
1323 # We must check the source cache because otherwise we can print
1323 # We must check the source cache because otherwise we can print
1324 # out-of-date source code.
1324 # out-of-date source code.
1325 self.check_cache()
1325 self.check_cache()
1326 # Now we can extract and format the exception
1326 # Now we can extract and format the exception
1327 return ListTB.structured_traceback(
1327 return ListTB.structured_traceback(
1328 self, etype, value, tb, tb_offset, number_of_lines_of_context
1328 self, etype, value, tb, tb_offset, number_of_lines_of_context
1329 )
1329 )
1330
1330
1331 def stb2text(self, stb):
1331 def stb2text(self, stb):
1332 """Convert a structured traceback (a list) to a string."""
1332 """Convert a structured traceback (a list) to a string."""
1333 return self.tb_join_char.join(stb)
1333 return self.tb_join_char.join(stb)
1334
1334
1335
1335
1336 def set_mode(self, mode=None):
1336 def set_mode(self, mode=None):
1337 """Switch to the desired mode.
1337 """Switch to the desired mode.
1338
1338
1339 If mode is not specified, cycles through the available modes."""
1339 If mode is not specified, cycles through the available modes."""
1340
1340
1341 if not mode:
1341 if not mode:
1342 new_idx = (self.valid_modes.index(self.mode) + 1 ) % \
1342 new_idx = (self.valid_modes.index(self.mode) + 1 ) % \
1343 len(self.valid_modes)
1343 len(self.valid_modes)
1344 self.mode = self.valid_modes[new_idx]
1344 self.mode = self.valid_modes[new_idx]
1345 elif mode not in self.valid_modes:
1345 elif mode not in self.valid_modes:
1346 raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n'
1346 raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n'
1347 'Valid modes: ' + str(self.valid_modes))
1347 'Valid modes: ' + str(self.valid_modes))
1348 else:
1348 else:
1349 self.mode = mode
1349 self.mode = mode
1350 # include variable details only in 'Verbose' mode
1350 # include variable details only in 'Verbose' mode
1351 self.include_vars = (self.mode == self.valid_modes[2])
1351 self.include_vars = (self.mode == self.valid_modes[2])
1352 # Set the join character for generating text tracebacks
1352 # Set the join character for generating text tracebacks
1353 self.tb_join_char = self._join_chars[self.mode]
1353 self.tb_join_char = self._join_chars[self.mode]
1354
1354
1355 # some convenient shortcuts
1355 # some convenient shortcuts
1356 def plain(self):
1356 def plain(self):
1357 self.set_mode(self.valid_modes[0])
1357 self.set_mode(self.valid_modes[0])
1358
1358
1359 def context(self):
1359 def context(self):
1360 self.set_mode(self.valid_modes[1])
1360 self.set_mode(self.valid_modes[1])
1361
1361
1362 def verbose(self):
1362 def verbose(self):
1363 self.set_mode(self.valid_modes[2])
1363 self.set_mode(self.valid_modes[2])
1364
1364
1365 def minimal(self):
1365 def minimal(self):
1366 self.set_mode(self.valid_modes[3])
1366 self.set_mode(self.valid_modes[3])
1367
1367
1368
1368
1369 #----------------------------------------------------------------------------
1369 #----------------------------------------------------------------------------
1370 class AutoFormattedTB(FormattedTB):
1370 class AutoFormattedTB(FormattedTB):
1371 """A traceback printer which can be called on the fly.
1371 """A traceback printer which can be called on the fly.
1372
1372
1373 It will find out about exceptions by itself.
1373 It will find out about exceptions by itself.
1374
1374
1375 A brief example::
1375 A brief example::
1376
1376
1377 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
1377 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
1378 try:
1378 try:
1379 ...
1379 ...
1380 except:
1380 except:
1381 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
1381 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
1382 """
1382 """
1383
1383
1384 def __call__(self, etype=None, evalue=None, etb=None,
1384 def __call__(self, etype=None, evalue=None, etb=None,
1385 out=None, tb_offset=None):
1385 out=None, tb_offset=None):
1386 """Print out a formatted exception traceback.
1386 """Print out a formatted exception traceback.
1387
1387
1388 Optional arguments:
1388 Optional arguments:
1389 - out: an open file-like object to direct output to.
1389 - out: an open file-like object to direct output to.
1390
1390
1391 - tb_offset: the number of frames to skip over in the stack, on a
1391 - tb_offset: the number of frames to skip over in the stack, on a
1392 per-call basis (this overrides temporarily the instance's tb_offset
1392 per-call basis (this overrides temporarily the instance's tb_offset
1393 given at initialization time. """
1393 given at initialization time. """
1394
1394
1395 if out is None:
1395 if out is None:
1396 out = self.ostream
1396 out = self.ostream
1397 out.flush()
1397 out.flush()
1398 out.write(self.text(etype, evalue, etb, tb_offset))
1398 out.write(self.text(etype, evalue, etb, tb_offset))
1399 out.write('\n')
1399 out.write('\n')
1400 out.flush()
1400 out.flush()
1401 # FIXME: we should remove the auto pdb behavior from here and leave
1401 # FIXME: we should remove the auto pdb behavior from here and leave
1402 # that to the clients.
1402 # that to the clients.
1403 try:
1403 try:
1404 self.debugger()
1404 self.debugger()
1405 except KeyboardInterrupt:
1405 except KeyboardInterrupt:
1406 print("\nKeyboardInterrupt")
1406 print("\nKeyboardInterrupt")
1407
1407
1408 def structured_traceback(self, etype=None, value=None, tb=None,
1408 def structured_traceback(self, etype=None, value=None, tb=None,
1409 tb_offset=None, number_of_lines_of_context=5):
1409 tb_offset=None, number_of_lines_of_context=5):
1410 if etype is None:
1410 if etype is None:
1411 etype, value, tb = sys.exc_info()
1411 etype, value, tb = sys.exc_info()
1412 self.tb = tb
1412 if not isinstance(tb, tuple):
1413 # tb is a tuple if this is a chained exception.
1414 # We keep first traceback.
1415 self.tb = tb
1413 return FormattedTB.structured_traceback(
1416 return FormattedTB.structured_traceback(
1414 self, etype, value, tb, tb_offset, number_of_lines_of_context)
1417 self, etype, value, tb, tb_offset, number_of_lines_of_context)
1415
1418
1416
1419
1417 #---------------------------------------------------------------------------
1420 #---------------------------------------------------------------------------
1418
1421
1419 # A simple class to preserve Nathan's original functionality.
1422 # A simple class to preserve Nathan's original functionality.
1420 class ColorTB(FormattedTB):
1423 class ColorTB(FormattedTB):
1421 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1424 """Shorthand to initialize a FormattedTB in Linux colors mode."""
1422
1425
1423 def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs):
1426 def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs):
1424 FormattedTB.__init__(self, color_scheme=color_scheme,
1427 FormattedTB.__init__(self, color_scheme=color_scheme,
1425 call_pdb=call_pdb, **kwargs)
1428 call_pdb=call_pdb, **kwargs)
1426
1429
1427
1430
1428 class SyntaxTB(ListTB):
1431 class SyntaxTB(ListTB):
1429 """Extension which holds some state: the last exception value"""
1432 """Extension which holds some state: the last exception value"""
1430
1433
1431 def __init__(self, color_scheme='NoColor', parent=None, config=None):
1434 def __init__(self, color_scheme='NoColor', parent=None, config=None):
1432 ListTB.__init__(self, color_scheme, parent=parent, config=config)
1435 ListTB.__init__(self, color_scheme, parent=parent, config=config)
1433 self.last_syntax_error = None
1436 self.last_syntax_error = None
1434
1437
1435 def __call__(self, etype, value, elist):
1438 def __call__(self, etype, value, elist):
1436 self.last_syntax_error = value
1439 self.last_syntax_error = value
1437
1440
1438 ListTB.__call__(self, etype, value, elist)
1441 ListTB.__call__(self, etype, value, elist)
1439
1442
1440 def structured_traceback(self, etype, value, elist, tb_offset=None,
1443 def structured_traceback(self, etype, value, elist, tb_offset=None,
1441 context=5):
1444 context=5):
1442 # If the source file has been edited, the line in the syntax error can
1445 # If the source file has been edited, the line in the syntax error can
1443 # be wrong (retrieved from an outdated cache). This replaces it with
1446 # be wrong (retrieved from an outdated cache). This replaces it with
1444 # the current value.
1447 # the current value.
1445 if isinstance(value, SyntaxError) \
1448 if isinstance(value, SyntaxError) \
1446 and isinstance(value.filename, str) \
1449 and isinstance(value.filename, str) \
1447 and isinstance(value.lineno, int):
1450 and isinstance(value.lineno, int):
1448 linecache.checkcache(value.filename)
1451 linecache.checkcache(value.filename)
1449 newtext = linecache.getline(value.filename, value.lineno)
1452 newtext = linecache.getline(value.filename, value.lineno)
1450 if newtext:
1453 if newtext:
1451 value.text = newtext
1454 value.text = newtext
1452 self.last_syntax_error = value
1455 self.last_syntax_error = value
1453 return super(SyntaxTB, self).structured_traceback(etype, value, elist,
1456 return super(SyntaxTB, self).structured_traceback(etype, value, elist,
1454 tb_offset=tb_offset, context=context)
1457 tb_offset=tb_offset, context=context)
1455
1458
1456 def clear_err_state(self):
1459 def clear_err_state(self):
1457 """Return the current error state and clear it"""
1460 """Return the current error state and clear it"""
1458 e = self.last_syntax_error
1461 e = self.last_syntax_error
1459 self.last_syntax_error = None
1462 self.last_syntax_error = None
1460 return e
1463 return e
1461
1464
1462 def stb2text(self, stb):
1465 def stb2text(self, stb):
1463 """Convert a structured traceback (a list) to a string."""
1466 """Convert a structured traceback (a list) to a string."""
1464 return ''.join(stb)
1467 return ''.join(stb)
1465
1468
1466
1469
1467 # some internal-use functions
1470 # some internal-use functions
1468 def text_repr(value):
1471 def text_repr(value):
1469 """Hopefully pretty robust repr equivalent."""
1472 """Hopefully pretty robust repr equivalent."""
1470 # this is pretty horrible but should always return *something*
1473 # this is pretty horrible but should always return *something*
1471 try:
1474 try:
1472 return pydoc.text.repr(value)
1475 return pydoc.text.repr(value)
1473 except KeyboardInterrupt:
1476 except KeyboardInterrupt:
1474 raise
1477 raise
1475 except:
1478 except:
1476 try:
1479 try:
1477 return repr(value)
1480 return repr(value)
1478 except KeyboardInterrupt:
1481 except KeyboardInterrupt:
1479 raise
1482 raise
1480 except:
1483 except:
1481 try:
1484 try:
1482 # all still in an except block so we catch
1485 # all still in an except block so we catch
1483 # getattr raising
1486 # getattr raising
1484 name = getattr(value, '__name__', None)
1487 name = getattr(value, '__name__', None)
1485 if name:
1488 if name:
1486 # ick, recursion
1489 # ick, recursion
1487 return text_repr(name)
1490 return text_repr(name)
1488 klass = getattr(value, '__class__', None)
1491 klass = getattr(value, '__class__', None)
1489 if klass:
1492 if klass:
1490 return '%s instance' % text_repr(klass)
1493 return '%s instance' % text_repr(klass)
1491 except KeyboardInterrupt:
1494 except KeyboardInterrupt:
1492 raise
1495 raise
1493 except:
1496 except:
1494 return 'UNRECOVERABLE REPR FAILURE'
1497 return 'UNRECOVERABLE REPR FAILURE'
1495
1498
1496
1499
1497 def eqrepr(value, repr=text_repr):
1500 def eqrepr(value, repr=text_repr):
1498 return '=%s' % repr(value)
1501 return '=%s' % repr(value)
1499
1502
1500
1503
1501 def nullrepr(value, repr=text_repr):
1504 def nullrepr(value, repr=text_repr):
1502 return ''
1505 return ''
General Comments 0
You need to be logged in to leave comments. Login now