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