forked from ipython/ipython
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathultratb.py
1371 lines (1140 loc) · 53.4 KB
/
ultratb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Verbose and colourful traceback formatting.
**ColorTB**
I've always found it a bit hard to visually parse tracebacks in Python. The
ColorTB class is a solution to that problem. It colors the different parts of a
traceback in a manner similar to what you would expect from a syntax-highlighting
text editor.
Installation instructions for ColorTB::
import sys,ultratb
sys.excepthook = ultratb.ColorTB()
**VerboseTB**
I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
of useful info when a traceback occurs. Ping originally had it spit out HTML
and intended it for CGI programmers, but why should they have all the fun? I
altered it to spit out colored text to the terminal. It's a bit overwhelming,
but kind of neat, and maybe useful for long-running programs that you believe
are bug-free. If a crash *does* occur in that type of program you want details.
Give it a shot--you'll love it or you'll hate it.
.. note::
The Verbose mode prints the variables currently visible where the exception
happened (shortening their strings if too long). This can potentially be
very slow, if you happen to have a huge data structure whose string
representation is complex to compute. Your computer may appear to freeze for
a while with cpu usage at 100%. If this occurs, you can cancel the traceback
with Ctrl-C (maybe hitting it more than once).
If you encounter this kind of situation often, you may want to use the
Verbose_novars mode instead of the regular Verbose, which avoids formatting
variables (but otherwise includes the information and context given by
Verbose).
Installation instructions for VerboseTB::
import sys,ultratb
sys.excepthook = ultratb.VerboseTB()
Note: Much of the code in this module was lifted verbatim from the standard
library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
Color schemes
-------------
The colors are defined in the class TBTools through the use of the
ColorSchemeTable class. Currently the following exist:
- NoColor: allows all of this module to be used in any terminal (the color
escapes are just dummy blank strings).
- Linux: is meant to look good in a terminal like the Linux console (black
or very dark background).
- LightBG: similar to Linux but swaps dark/light colors to be more readable
in light background terminals.
You can implement other color schemes easily, the syntax is fairly
self-explanatory. Please send back new schemes you develop to the author for
possible inclusion in future releases.
Inheritance diagram:
.. inheritance-diagram:: IPython.core.ultratb
:parts: 3
"""
#*****************************************************************************
# Copyright (C) 2001 Nathaniel Gray <[email protected]>
# Copyright (C) 2001-2004 Fernando Perez <[email protected]>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#*****************************************************************************
from __future__ import unicode_literals
from __future__ import print_function
import inspect
import keyword
import linecache
import os
import pydoc
import re
import sys
import time
import tokenize
import traceback
import types
try: # Python 2
generate_tokens = tokenize.generate_tokens
except AttributeError: # Python 3
generate_tokens = tokenize.tokenize
# For purposes of monkeypatching inspect to fix a bug in it.
from inspect import getsourcefile, getfile, getmodule, \
ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
# IPython's own modules
# Modified pdb which doesn't damage IPython's readline handling
from IPython import get_ipython
from IPython.core import debugger
from IPython.core.display_trap import DisplayTrap
from IPython.core.excolors import exception_colors
from IPython.utils import PyColorize
from IPython.utils import io
from IPython.utils import openpy
from IPython.utils import path as util_path
from IPython.utils import py3compat
from IPython.utils import ulinecache
from IPython.utils.data import uniq_stable
from IPython.utils.warn import info, error
# Globals
# amount of space to put line numbers before verbose tracebacks
INDENT_SIZE = 8
# Default color scheme. This is used, for example, by the traceback
# formatter. When running in an actual IPython instance, the user's rc.colors
# value is used, but having a module global makes this functionality available
# to users of ultratb who are NOT running inside ipython.
DEFAULT_SCHEME = 'NoColor'
# ---------------------------------------------------------------------------
# Code begins
# Utility functions
def inspect_error():
"""Print a message about internal inspect errors.
These are unfortunately quite common."""
error('Internal Python error in the inspect module.\n'
'Below is the traceback from this internal error.\n')
# This function is a monkeypatch we apply to the Python inspect module. We have
# now found when it's needed (see discussion on issue gh-1456), and we have a
# test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if
# the monkeypatch is not applied. TK, Aug 2012.
def findsource(object):
"""Return the entire source file and starting line number for an object.
The argument may be a module, class, method, function, traceback, frame,
or code object. The source code is returned as a list of all the lines
in the file and the line number indexes a line in that list. An IOError
is raised if the source code cannot be retrieved.
FIXED version with which we monkeypatch the stdlib to work around a bug."""
file = getsourcefile(object) or getfile(object)
# If the object is a frame, then trying to get the globals dict from its
# module won't work. Instead, the frame object itself has the globals
# dictionary.
globals_dict = None
if inspect.isframe(object):
# XXX: can this ever be false?
globals_dict = object.f_globals
else:
module = getmodule(object, file)
if module:
globals_dict = module.__dict__
lines = linecache.getlines(file, globals_dict)
if not lines:
raise IOError('could not get source code')
if ismodule(object):
return lines, 0
if isclass(object):
name = object.__name__
pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
# make some effort to find the best matching class definition:
# use the one with the least indentation, which is the one
# that's most probably not inside a function definition.
candidates = []
for i in range(len(lines)):
match = pat.match(lines[i])
if match:
# if it's at toplevel, it's already the best one
if lines[i][0] == 'c':
return lines, i
# else add whitespace to candidate list
candidates.append((match.group(1), i))
if candidates:
# this will sort by whitespace, and by line number,
# less whitespace first
candidates.sort()
return lines, candidates[0][1]
else:
raise IOError('could not find class definition')
if ismethod(object):
object = object.__func__
if isfunction(object):
object = object.__code__
if istraceback(object):
object = object.tb_frame
if isframe(object):
object = object.f_code
if iscode(object):
if not hasattr(object, 'co_firstlineno'):
raise IOError('could not find function definition')
pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
pmatch = pat.match
# fperez - fix: sometimes, co_firstlineno can give a number larger than
# the length of lines, which causes an error. Safeguard against that.
lnum = min(object.co_firstlineno, len(lines)) - 1
while lnum > 0:
if pmatch(lines[lnum]): break
lnum -= 1
return lines, lnum
raise IOError('could not find code object')
# Monkeypatch inspect to apply our bugfix.
def with_patch_inspect(f):
"""decorator for monkeypatching inspect.findsource"""
def wrapped(*args, **kwargs):
save_findsource = inspect.findsource
inspect.findsource = findsource
try:
return f(*args, **kwargs)
finally:
inspect.findsource = save_findsource
return wrapped
def fix_frame_records_filenames(records):
"""Try to fix the filenames in each record from inspect.getinnerframes().
Particularly, modules loaded from within zip files have useless filenames
attached to their code object, and inspect.getinnerframes() just uses it.
"""
fixed_records = []
for frame, filename, line_no, func_name, lines, index in records:
# Look inside the frame's globals dictionary for __file__,
# which should be better. However, keep Cython filenames since
# we prefer the source filenames over the compiled .so file.
filename = py3compat.cast_unicode_py2(filename, "utf-8")
if not filename.endswith(('.pyx', '.pxd', '.pxi')):
better_fn = frame.f_globals.get('__file__', None)
if isinstance(better_fn, str):
# Check the type just in case someone did something weird with
# __file__. It might also be None if the error occurred during
# import.
filename = better_fn
fixed_records.append((frame, filename, line_no, func_name, lines, index))
return fixed_records
@with_patch_inspect
def _fixed_getinnerframes(etb, context=1, tb_offset=0):
LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
# If the error is at the console, don't build any context, since it would
# otherwise produce 5 blank lines printed out (there is no file at the
# console)
rec_check = records[tb_offset:]
try:
rname = rec_check[0][1]
if rname == '<ipython console>' or rname.endswith('<string>'):
return rec_check
except IndexError:
pass
aux = traceback.extract_tb(etb)
assert len(records) == len(aux)
for i, (file, lnum, _, _) in zip(range(len(records)), aux):
maybeStart = lnum - 1 - context // 2
start = max(maybeStart, 0)
end = start + context
lines = ulinecache.getlines(file)[start:end]
buf = list(records[i])
buf[LNUM_POS] = lnum
buf[INDEX_POS] = lnum - 1 - start
buf[LINES_POS] = lines
records[i] = tuple(buf)
return records[tb_offset:]
# Helper function -- largely belongs to VerboseTB, but we need the same
# functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
# can be recognized properly by ipython.el's py-traceback-line-re
# (SyntaxErrors have to be treated specially because they have no traceback)
_parser = PyColorize.Parser()
def _format_traceback_lines(lnum, index, lines, Colors, lvals=None, scheme=None):
numbers_width = INDENT_SIZE - 1
res = []
i = lnum - index
# This lets us get fully syntax-highlighted tracebacks.
if scheme is None:
ipinst = get_ipython()
if ipinst is not None:
scheme = ipinst.colors
else:
scheme = DEFAULT_SCHEME
_line_format = _parser.format2
for line in lines:
line = py3compat.cast_unicode(line)
new_line, err = _line_format(line, 'str', scheme)
if not err: line = new_line
if i == lnum:
# This is the line with the error
pad = numbers_width - len(str(i))
if pad >= 3:
marker = '-' * (pad - 3) + '-> '
elif pad == 2:
marker = '> '
elif pad == 1:
marker = '>'
else:
marker = ''
num = marker + str(i)
line = '%s%s%s %s%s' % (Colors.linenoEm, num,
Colors.line, line, Colors.Normal)
else:
num = '%*s' % (numbers_width, i)
line = '%s%s%s %s' % (Colors.lineno, num,
Colors.Normal, line)
res.append(line)
if lvals and i == lnum:
res.append(lvals + '\n')
i = i + 1
return res
#---------------------------------------------------------------------------
# Module classes
class TBTools(object):
"""Basic tools used by all traceback printer classes."""
# Number of frames to skip when reporting tracebacks
tb_offset = 0
def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None):
# Whether to call the interactive pdb debugger after printing
# tracebacks or not
self.call_pdb = call_pdb
# Output stream to write to. Note that we store the original value in
# a private attribute and then make the public ostream a property, so
# that we can delay accessing io.stdout until runtime. The way
# things are written now, the io.stdout object is dynamically managed
# so a reference to it should NEVER be stored statically. This
# property approach confines this detail to a single location, and all
# subclasses can simply access self.ostream for writing.
self._ostream = ostream
# Create color table
self.color_scheme_table = exception_colors()
self.set_colors(color_scheme)
self.old_scheme = color_scheme # save initial value for toggles
if call_pdb:
self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name)
else:
self.pdb = None
def _get_ostream(self):
"""Output stream that exceptions are written to.
Valid values are:
- None: the default, which means that IPython will dynamically resolve
to io.stdout. This ensures compatibility with most tools, including
Windows (where plain stdout doesn't recognize ANSI escapes).
- Any object with 'write' and 'flush' attributes.
"""
return io.stdout if self._ostream is None else self._ostream
def _set_ostream(self, val):
assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush'))
self._ostream = val
ostream = property(_get_ostream, _set_ostream)
def set_colors(self, *args, **kw):
"""Shorthand access to the color table scheme selector method."""
# Set own color table
self.color_scheme_table.set_active_scheme(*args, **kw)
# for convenience, set Colors to the active scheme
self.Colors = self.color_scheme_table.active_colors
# Also set colors of debugger
if hasattr(self, 'pdb') and self.pdb is not None:
self.pdb.set_colors(*args, **kw)
def color_toggle(self):
"""Toggle between the currently active color scheme and NoColor."""
if self.color_scheme_table.active_scheme_name == 'NoColor':
self.color_scheme_table.set_active_scheme(self.old_scheme)
self.Colors = self.color_scheme_table.active_colors
else:
self.old_scheme = self.color_scheme_table.active_scheme_name
self.color_scheme_table.set_active_scheme('NoColor')
self.Colors = self.color_scheme_table.active_colors
def stb2text(self, stb):
"""Convert a structured traceback (a list) to a string."""
return '\n'.join(stb)
def text(self, etype, value, tb, tb_offset=None, context=5):
"""Return formatted traceback.
Subclasses may override this if they add extra arguments.
"""
tb_list = self.structured_traceback(etype, value, tb,
tb_offset, context)
return self.stb2text(tb_list)
def structured_traceback(self, etype, evalue, tb, tb_offset=None,
context=5, mode=None):
"""Return a list of traceback frames.
Must be implemented by each class.
"""
raise NotImplementedError()
#---------------------------------------------------------------------------
class ListTB(TBTools):
"""Print traceback information from a traceback list, with optional color.
Calling requires 3 arguments: (etype, evalue, elist)
as would be obtained by::
etype, evalue, tb = sys.exc_info()
if tb:
elist = traceback.extract_tb(tb)
else:
elist = None
It can thus be used by programs which need to process the traceback before
printing (such as console replacements based on the code module from the
standard library).
Because they are meant to be called without a full traceback (only a
list), instances of this class can't call the interactive pdb debugger."""
def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None):
TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
ostream=ostream)
def __call__(self, etype, value, elist):
self.ostream.flush()
self.ostream.write(self.text(etype, value, elist))
self.ostream.write('\n')
def structured_traceback(self, etype, value, elist, tb_offset=None,
context=5):
"""Return a color formatted string with the traceback info.
Parameters
----------
etype : exception type
Type of the exception raised.
value : object
Data stored in the exception
elist : list
List of frames, see class docstring for details.
tb_offset : int, optional
Number of frames in the traceback to skip. If not given, the
instance value is used (set in constructor).
context : int, optional
Number of lines of context information to print.
Returns
-------
String with formatted exception.
"""
tb_offset = self.tb_offset if tb_offset is None else tb_offset
Colors = self.Colors
out_list = []
if elist:
if tb_offset and len(elist) > tb_offset:
elist = elist[tb_offset:]
out_list.append('Traceback %s(most recent call last)%s:' %
(Colors.normalEm, Colors.Normal) + '\n')
out_list.extend(self._format_list(elist))
# The exception info should be a single entry in the list.
lines = ''.join(self._format_exception_only(etype, value))
out_list.append(lines)
# Note: this code originally read:
## for line in lines[:-1]:
## out_list.append(" "+line)
## out_list.append(lines[-1])
# This means it was indenting everything but the last line by a little
# bit. I've disabled this for now, but if we see ugliness somewhere we
# can restore it.
return out_list
def _format_list(self, extracted_list):
"""Format a list of traceback entry tuples for printing.
Given a list of tuples as returned by extract_tb() or
extract_stack(), return a list of strings ready for printing.
Each string in the resulting list corresponds to the item with the
same index in the argument list. Each string ends in a newline;
the strings may contain internal newlines as well, for those items
whose source text line is not None.
Lifted almost verbatim from traceback.py
"""
Colors = self.Colors
list = []
for filename, lineno, name, line in extracted_list[:-1]:
item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
(Colors.filename, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.Normal,
Colors.lineno, lineno, Colors.Normal,
Colors.name, py3compat.cast_unicode_py2(name, "utf-8"), Colors.Normal)
if line:
item += ' %s\n' % line.strip()
list.append(item)
# Emphasize the last entry
filename, lineno, name, line = extracted_list[-1]
item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
(Colors.normalEm,
Colors.filenameEm, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.normalEm,
Colors.linenoEm, lineno, Colors.normalEm,
Colors.nameEm, py3compat.cast_unicode_py2(name, "utf-8"), Colors.normalEm,
Colors.Normal)
if line:
item += '%s %s%s\n' % (Colors.line, line.strip(),
Colors.Normal)
list.append(item)
return list
def _format_exception_only(self, etype, value):
"""Format the exception part of a traceback.
The arguments are the exception type and value such as given by
sys.exc_info()[:2]. The return value is a list of strings, each ending
in a newline. Normally, the list contains a single string; however,
for SyntaxError exceptions, it contains several lines that (when
printed) display detailed information about where the syntax error
occurred. The message indicating which exception occurred is the
always last string in the list.
Also lifted nearly verbatim from traceback.py
"""
have_filedata = False
Colors = self.Colors
list = []
stype = Colors.excName + etype.__name__ + Colors.Normal
if value is None:
# Not sure if this can still happen in Python 2.6 and above
list.append(py3compat.cast_unicode(stype) + '\n')
else:
if issubclass(etype, SyntaxError):
have_filedata = True
if not value.filename: value.filename = "<string>"
if value.lineno:
lineno = value.lineno
textline = ulinecache.getline(value.filename, value.lineno)
else:
lineno = 'unknown'
textline = ''
list.append('%s File %s"%s"%s, line %s%s%s\n' % \
(Colors.normalEm,
Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm,
Colors.linenoEm, lineno, Colors.Normal ))
if textline == '':
textline = py3compat.cast_unicode(value.text, "utf-8")
if textline is not None:
i = 0
while i < len(textline) and textline[i].isspace():
i += 1
list.append('%s %s%s\n' % (Colors.line,
textline.strip(),
Colors.Normal))
if value.offset is not None:
s = ' '
for c in textline[i:value.offset - 1]:
if c.isspace():
s += c
else:
s += ' '
list.append('%s%s^%s\n' % (Colors.caret, s,
Colors.Normal))
try:
s = value.msg
except Exception:
s = self._some_str(value)
if s:
list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
Colors.Normal, s))
else:
list.append('%s\n' % str(stype))
# sync with user hooks
if have_filedata:
ipinst = get_ipython()
if ipinst is not None:
ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
return list
def get_exception_only(self, etype, value):
"""Only print the exception type and message, without a traceback.
Parameters
----------
etype : exception type
value : exception value
"""
return ListTB.structured_traceback(self, etype, value, [])
def show_exception_only(self, etype, evalue):
"""Only print the exception type and message, without a traceback.
Parameters
----------
etype : exception type
value : exception value
"""
# This method needs to use __call__ from *this* class, not the one from
# a subclass whose signature or behavior may be different
ostream = self.ostream
ostream.flush()
ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
ostream.flush()
def _some_str(self, value):
# Lifted from traceback.py
try:
return str(value)
except:
return '<unprintable %s object>' % type(value).__name__
#----------------------------------------------------------------------------
class VerboseTB(TBTools):
"""A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
of HTML. Requires inspect and pydoc. Crazy, man.
Modified version which optionally strips the topmost entries from the
traceback, to be used with alternate interpreters (because their own code
would appear in the traceback)."""
def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None,
tb_offset=0, long_header=False, include_vars=True,
check_cache=None):
"""Specify traceback offset, headers and color scheme.
Define how many frames to drop from the tracebacks. Calling it with
tb_offset=1 allows use of this handler in interpreters which will have
their own code at the top of the traceback (VerboseTB will first
remove that frame before printing the traceback info)."""
TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
ostream=ostream)
self.tb_offset = tb_offset
self.long_header = long_header
self.include_vars = include_vars
# By default we use linecache.checkcache, but the user can provide a
# different check_cache implementation. This is used by the IPython
# kernel to provide tracebacks for interactive code that is cached,
# by a compiler instance that flushes the linecache but preserves its
# own code cache.
if check_cache is None:
check_cache = linecache.checkcache
self.check_cache = check_cache
def format_records(self, records):
Colors = self.Colors # just a shorthand + quicker name lookup
ColorsNormal = Colors.Normal # used a lot
col_scheme = self.color_scheme_table.active_scheme_name
indent = ' ' * INDENT_SIZE
em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
frames = []
# build some color string templates outside these nested loops
tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
ColorsNormal)
tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
(Colors.vName, Colors.valEm, ColorsNormal)
tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
Colors.vName, ColorsNormal)
tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line,
ColorsNormal)
abspath = os.path.abspath
for frame, file, lnum, func, lines, index in records:
#print '*** record:',file,lnum,func,lines,index # dbg
if not file:
file = '?'
elif file.startswith(str("<")) and file.endswith(str(">")):
# Not a real filename, no problem...
pass
elif not os.path.isabs(file):
# Try to make the filename absolute by trying all
# sys.path entries (which is also what linecache does)
for dirname in sys.path:
try:
fullname = os.path.join(dirname, file)
if os.path.isfile(fullname):
file = os.path.abspath(fullname)
break
except Exception:
# Just in case that sys.path contains very
# strange entries...
pass
file = py3compat.cast_unicode(file, util_path.fs_encoding)
link = tpl_link % file
args, varargs, varkw, locals = inspect.getargvalues(frame)
if func == '?':
call = ''
else:
# Decide whether to include variable details or not
var_repr = self.include_vars and eqrepr or nullrepr
try:
call = tpl_call % (func, inspect.formatargvalues(args,
varargs, varkw,
locals, formatvalue=var_repr))
except KeyError:
# This happens in situations like errors inside generator
# expressions, where local variables are listed in the
# line, but can't be extracted from the frame. I'm not
# 100% sure this isn't actually a bug in inspect itself,
# but since there's no info for us to compute with, the
# best we can do is report the failure and move on. Here
# we must *not* call any traceback construction again,
# because that would mess up use of %debug later on. So we
# simply report the failure and move on. The only
# limitation will be that this frame won't have locals
# listed in the call signature. Quite subtle problem...
# I can't think of a good way to validate this in a unit
# test, but running a script consisting of:
# dict( (k,v.strip()) for (k,v) in range(10) )
# will illustrate the error, if this exception catch is
# disabled.
call = tpl_call_fail % func
# Don't attempt to tokenize binary files.
if file.endswith(('.so', '.pyd', '.dll')):
frames.append('%s %s\n' % (link, call))
continue
elif file.endswith(('.pyc', '.pyo')):
# Look up the corresponding source file.
file = openpy.source_from_cache(file)
def linereader(file=file, lnum=[lnum], getline=ulinecache.getline):
line = getline(file, lnum[0])
lnum[0] += 1
return line
# Build the list of names on this line of code where the exception
# occurred.
try:
names = []
name_cont = False
for token_type, token, start, end, line in generate_tokens(linereader):
# build composite names
if token_type == tokenize.NAME and token not in keyword.kwlist:
if name_cont:
# Continuation of a dotted name
try:
names[-1].append(token)
except IndexError:
names.append([token])
name_cont = False
else:
# Regular new names. We append everything, the caller
# will be responsible for pruning the list later. It's
# very tricky to try to prune as we go, b/c composite
# names can fool us. The pruning at the end is easy
# to do (or the caller can print a list with repeated
# names if so desired.
names.append([token])
elif token == '.':
name_cont = True
elif token_type == tokenize.NEWLINE:
break
except (IndexError, UnicodeDecodeError, SyntaxError):
# signals exit of tokenizer
# SyntaxError can occur if the file is not actually Python
# - see gh-6300
pass
except tokenize.TokenError as msg:
_m = ("An unexpected error occurred while tokenizing input\n"
"The following traceback may be corrupted or invalid\n"
"The error message is: %s\n" % msg)
error(_m)
# Join composite names (e.g. "dict.fromkeys")
names = ['.'.join(n) for n in names]
# prune names list of duplicates, but keep the right order
unique_names = uniq_stable(names)
# Start loop over vars
lvals = []
if self.include_vars:
for name_full in unique_names:
name_base = name_full.split('.', 1)[0]
if name_base in frame.f_code.co_varnames:
if name_base in locals:
try:
value = repr(eval(name_full, locals))
except:
value = undefined
else:
value = undefined
name = tpl_local_var % name_full
else:
if name_base in frame.f_globals:
try:
value = repr(eval(name_full, frame.f_globals))
except:
value = undefined
else:
value = undefined
name = tpl_global_var % name_full
lvals.append(tpl_name_val % (name, value))
if lvals:
lvals = '%s%s' % (indent, em_normal.join(lvals))
else:
lvals = ''
level = '%s %s\n' % (link, call)
if index is None:
frames.append(level)
else:
frames.append('%s%s' % (level, ''.join(
_format_traceback_lines(lnum, index, lines, Colors, lvals,
col_scheme))))
return frames
def prepare_chained_exception_message(self, cause):
direct_cause = "\nThe above exception was the direct cause of the following exception:\n"
exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n"
if cause:
message = [[direct_cause]]
else:
message = [[exception_during_handling]]
return message
def prepare_header(self, etype, long_version=False):
colors = self.Colors # just a shorthand + quicker name lookup
colorsnormal = colors.Normal # used a lot
exc = '%s%s%s' % (colors.excName, etype, colorsnormal)
if long_version:
# Header with the exception type, python version, and date
pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
date = time.ctime(time.time())
head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * 75, colorsnormal,
exc, ' ' * (75 - len(str(etype)) - len(pyver)),
pyver, date.rjust(75) )
head += "\nA problem occurred executing Python code. Here is the sequence of function" \
"\ncalls leading up to the error, with the most recent (innermost) call last."
else:
# Simplified header
head = '%s%s' % (exc, 'Traceback (most recent call last)'. \
rjust(75 - len(str(etype))) )
return head
def format_exception(self, etype, evalue):
colors = self.Colors # just a shorthand + quicker name lookup
colorsnormal = colors.Normal # used a lot
indent = ' ' * INDENT_SIZE
# Get (safely) a string form of the exception info
try:
etype_str, evalue_str = map(str, (etype, evalue))
except:
# User exception is improperly defined.
etype, evalue = str, sys.exc_info()[:2]
etype_str, evalue_str = map(str, (etype, evalue))
# ... and format it
exception = ['%s%s%s: %s' % (colors.excName, etype_str,
colorsnormal, py3compat.cast_unicode(evalue_str))]
if (not py3compat.PY3) and type(evalue) is types.InstanceType:
try:
names = [w for w in dir(evalue) if isinstance(w, py3compat.string_types)]
except:
# Every now and then, an object with funny internals blows up
# when dir() is called on it. We do the best we can to report
# the problem and continue
_m = '%sException reporting error (object with broken dir())%s:'
exception.append(_m % (colors.excName, colorsnormal))
etype_str, evalue_str = map(str, sys.exc_info()[:2])
exception.append('%s%s%s: %s' % (colors.excName, etype_str,
colorsnormal, py3compat.cast_unicode(evalue_str)))
names = []
for name in names:
value = text_repr(getattr(evalue, name))
exception.append('\n%s%s = %s' % (indent, name, value))
return exception
def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset):
# some locals
try:
etype = etype.__name__
except AttributeError:
pass
tb_offset = self.tb_offset if tb_offset is None else tb_offset
head = self.prepare_header(etype, self.long_header)
records = self.get_records(etb, number_of_lines_of_context, tb_offset)
frames = self.format_records(records)
if records is None:
return ""
formatted_exception = self.format_exception(etype, evalue)
if records:
filepath, lnum = records[-1][1:3]
filepath = os.path.abspath(filepath)
ipinst = get_ipython()
if ipinst is not None:
ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
return [[head] + frames + [''.join(formatted_exception[0])]]
def get_records(self, etb, number_of_lines_of_context, tb_offset):
try:
# Try the default getinnerframes and Alex's: Alex's fixes some
# problems, but it generates empty tracebacks for console errors
# (5 blanks lines) where none should be returned.
return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
except:
# FIXME: I've been getting many crash reports from python 2.3
# users, traceable to inspect.py. If I can find a small test-case
# to reproduce this, I should either write a better workaround or
# file a bug report against inspect (if that's the real problem).
# So far, I haven't been able to find an isolated example to
# reproduce the problem.
inspect_error()
traceback.print_exc(file=self.ostream)
info('\nUnfortunately, your original traceback can not be constructed.\n')
return None
def get_parts_of_chained_exception(self, evalue):
def get_chained_exception(exception_value):
cause = getattr(exception_value, '__cause__', None)
if cause:
return cause
return getattr(exception_value, '__context__', None)
chained_evalue = get_chained_exception(evalue)
if chained_evalue:
return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__
def structured_traceback(self, etype, evalue, etb, tb_offset=None,
number_of_lines_of_context=5):
"""Return a nice text document describing the traceback."""
formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
tb_offset)