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