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