##// END OF EJS Templates
Pass parent to child for configuration to propagate
Matthias Bussonnier -
Show More

The requested changes are too big and content was truncated. Show full diff

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