18
18
19
19
class Code (object ):
20
20
""" wrapper around Python code objects """
21
+
21
22
def __init__ (self , rawcode ):
22
23
if not hasattr (rawcode , "co_filename" ):
23
24
rawcode = getrawcode (rawcode )
@@ -26,7 +27,7 @@ def __init__(self, rawcode):
26
27
self .firstlineno = rawcode .co_firstlineno - 1
27
28
self .name = rawcode .co_name
28
29
except AttributeError :
29
- raise TypeError ("not a code object: %r" % (rawcode ,))
30
+ raise TypeError ("not a code object: %r" % (rawcode ,))
30
31
self .raw = rawcode
31
32
32
33
def __eq__ (self , other ):
@@ -82,6 +83,7 @@ def getargs(self, var=False):
82
83
argcount += raw .co_flags & CO_VARKEYWORDS
83
84
return raw .co_varnames [:argcount ]
84
85
86
+
85
87
class Frame (object ):
86
88
"""Wrapper around a Python frame holding f_locals and f_globals
87
89
in which expressions can be evaluated."""
@@ -119,7 +121,7 @@ def exec_(self, code, **vars):
119
121
"""
120
122
f_locals = self .f_locals .copy ()
121
123
f_locals .update (vars )
122
- py .builtin .exec_ (code , self .f_globals , f_locals )
124
+ py .builtin .exec_ (code , self .f_globals , f_locals )
123
125
124
126
def repr (self , object ):
125
127
""" return a 'safe' (non-recursive, one-line) string repr for 'object'
@@ -143,6 +145,7 @@ def getargs(self, var=False):
143
145
pass # this can occur when using Psyco
144
146
return retval
145
147
148
+
146
149
class TracebackEntry (object ):
147
150
""" a single entry in a traceback """
148
151
@@ -168,7 +171,7 @@ def relline(self):
168
171
return self .lineno - self .frame .code .firstlineno
169
172
170
173
def __repr__ (self ):
171
- return "<TracebackEntry %s:%d>" % (self .frame .code .path , self .lineno + 1 )
174
+ return "<TracebackEntry %s:%d>" % (self .frame .code .path , self .lineno + 1 )
172
175
173
176
@property
174
177
def statement (self ):
@@ -249,17 +252,19 @@ def __str__(self):
249
252
raise
250
253
except :
251
254
line = "???"
252
- return " File %r:%d in %s\n %s\n " % (fn , self .lineno + 1 , name , line )
255
+ return " File %r:%d in %s\n %s\n " % (fn , self .lineno + 1 , name , line )
253
256
254
257
def name (self ):
255
258
return self .frame .code .raw .co_name
256
259
name = property (name , None , None , "co_name of underlaying code" )
257
260
261
+
258
262
class Traceback (list ):
259
263
""" Traceback objects encapsulate and offer higher level
260
264
access to Traceback entries.
261
265
"""
262
266
Entry = TracebackEntry
267
+
263
268
def __init__ (self , tb , excinfo = None ):
264
269
""" initialize from given python traceback object and ExceptionInfo """
265
270
self ._excinfo = excinfo
@@ -289,7 +294,7 @@ def cut(self, path=None, lineno=None, firstlineno=None, excludepath=None):
289
294
(excludepath is None or not hasattr (codepath , 'relto' ) or
290
295
not codepath .relto (excludepath )) and
291
296
(lineno is None or x .lineno == lineno ) and
292
- (firstlineno is None or x .frame .code .firstlineno == firstlineno )):
297
+ (firstlineno is None or x .frame .code .firstlineno == firstlineno )):
293
298
return Traceback (x ._rawentry , self ._excinfo )
294
299
return self
295
300
@@ -315,7 +320,7 @@ def getcrashentry(self):
315
320
""" return last non-hidden traceback entry that lead
316
321
to the exception of a traceback.
317
322
"""
318
- for i in range (- 1 , - len (self )- 1 , - 1 ):
323
+ for i in range (- 1 , - len (self ) - 1 , - 1 ):
319
324
entry = self [i ]
320
325
if not entry .ishidden ():
321
326
return entry
@@ -330,17 +335,17 @@ def recursionindex(self):
330
335
# id for the code.raw is needed to work around
331
336
# the strange metaprogramming in the decorator lib from pypi
332
337
# which generates code objects that have hash/value equality
333
- #XXX needs a test
338
+ # XXX needs a test
334
339
key = entry .frame .code .path , id (entry .frame .code .raw ), entry .lineno
335
- #print "checking for recursion at", key
340
+ # print "checking for recursion at", key
336
341
l = cache .setdefault (key , [])
337
342
if l :
338
343
f = entry .frame
339
344
loc = f .f_locals
340
345
for otherloc in l :
341
346
if f .is_true (f .eval (co_equal ,
342
- __recursioncache_locals_1 = loc ,
343
- __recursioncache_locals_2 = otherloc )):
347
+ __recursioncache_locals_1 = loc ,
348
+ __recursioncache_locals_2 = otherloc )):
344
349
return i
345
350
l .append (entry .frame .f_locals )
346
351
return None
@@ -349,6 +354,7 @@ def recursionindex(self):
349
354
co_equal = compile ('__recursioncache_locals_1 == __recursioncache_locals_2' ,
350
355
'?' , 'eval' )
351
356
357
+
352
358
class ExceptionInfo (object ):
353
359
""" wraps sys.exc_info() objects and offers
354
360
help for navigating the traceback.
@@ -405,10 +411,10 @@ def _getreprcrash(self):
405
411
exconly = self .exconly (tryshort = True )
406
412
entry = self .traceback .getcrashentry ()
407
413
path , lineno = entry .frame .code .raw .co_filename , entry .lineno
408
- return ReprFileLocation (path , lineno + 1 , exconly )
414
+ return ReprFileLocation (path , lineno + 1 , exconly )
409
415
410
416
def getrepr (self , showlocals = False , style = "long" ,
411
- abspath = False , tbfilter = True , funcargs = False ):
417
+ abspath = False , tbfilter = True , funcargs = False ):
412
418
""" return str()able representation of this exception info.
413
419
showlocals: show locals per traceback entry
414
420
style: long|short|no|native traceback style
@@ -425,7 +431,7 @@ def getrepr(self, showlocals=False, style="long",
425
431
)), self ._getreprcrash ())
426
432
427
433
fmt = FormattedExcinfo (showlocals = showlocals , style = style ,
428
- abspath = abspath , tbfilter = tbfilter , funcargs = funcargs )
434
+ abspath = abspath , tbfilter = tbfilter , funcargs = funcargs )
429
435
return fmt .repr_excinfo (self )
430
436
431
437
def __str__ (self ):
@@ -469,7 +475,7 @@ def __init__(self, showlocals=False, style="long", abspath=True, tbfilter=True,
469
475
def _getindent (self , source ):
470
476
# figure out indent for given source
471
477
try :
472
- s = str (source .getstatement (len (source )- 1 ))
478
+ s = str (source .getstatement (len (source ) - 1 ))
473
479
except KeyboardInterrupt :
474
480
raise
475
481
except :
@@ -513,7 +519,7 @@ def get_source(self, source, line_index=-1, excinfo=None, short=False):
513
519
for line in source .lines [:line_index ]:
514
520
lines .append (space_prefix + line )
515
521
lines .append (self .flow_marker + " " + source .lines [line_index ])
516
- for line in source .lines [line_index + 1 :]:
522
+ for line in source .lines [line_index + 1 :]:
517
523
lines .append (space_prefix + line )
518
524
if excinfo is not None :
519
525
indent = 4 if short else self ._getindent (source )
@@ -546,10 +552,10 @@ def repr_locals(self, locals):
546
552
# _repr() function, which is only reprlib.Repr in
547
553
# disguise, so is very configurable.
548
554
str_repr = self ._saferepr (value )
549
- #if len(str_repr) < 70 or not isinstance(value,
555
+ # if len(str_repr) < 70 or not isinstance(value,
550
556
# (list, tuple, dict)):
551
- lines .append ("%-10s = %s" % (name , str_repr ))
552
- #else:
557
+ lines .append ("%-10s = %s" % (name , str_repr ))
558
+ # else:
553
559
# self._line("%-10s =\\" % (name,))
554
560
# # XXX
555
561
# py.std.pprint.pprint(value, stream=self.excinfowriter)
@@ -575,14 +581,14 @@ def repr_traceback_entry(self, entry, excinfo=None):
575
581
s = self .get_source (source , line_index , excinfo , short = short )
576
582
lines .extend (s )
577
583
if short :
578
- message = "in %s" % (entry .name )
584
+ message = "in %s" % (entry .name )
579
585
else :
580
586
message = excinfo and excinfo .typename or ""
581
587
path = self ._makepath (entry .path )
582
- filelocrepr = ReprFileLocation (path , entry .lineno + 1 , message )
588
+ filelocrepr = ReprFileLocation (path , entry .lineno + 1 , message )
583
589
localsrepr = None
584
590
if not short :
585
- localsrepr = self .repr_locals (entry .locals )
591
+ localsrepr = self .repr_locals (entry .locals )
586
592
return ReprEntry (lines , reprargs , localsrepr , filelocrepr , style )
587
593
if excinfo :
588
594
lines .extend (self .get_exconly (excinfo , indent = 4 ))
@@ -645,7 +651,7 @@ def _truncate_recursive_traceback(self, traceback):
645
651
traceback = traceback [:recursionindex + 1 ]
646
652
else :
647
653
extraline = None
648
-
654
+
649
655
return traceback , extraline
650
656
651
657
def repr_excinfo (self , excinfo ):
@@ -699,7 +705,7 @@ def __unicode__(self):
699
705
return io .getvalue ().strip ()
700
706
701
707
def __repr__ (self ):
702
- return "<%s instance at %0x>" % (self .__class__ , id (self ))
708
+ return "<%s instance at %0x>" % (self .__class__ , id (self ))
703
709
704
710
705
711
class ExceptionRepr (TerminalRepr ):
@@ -743,6 +749,7 @@ def toterminal(self, tw):
743
749
self .reprtraceback .toterminal (tw )
744
750
super (ReprExceptionInfo , self ).toterminal (tw )
745
751
752
+
746
753
class ReprTraceback (TerminalRepr ):
747
754
entrysep = "_ "
748
755
@@ -758,20 +765,22 @@ def toterminal(self, tw):
758
765
tw .line ("" )
759
766
entry .toterminal (tw )
760
767
if i < len (self .reprentries ) - 1 :
761
- next_entry = self .reprentries [i + 1 ]
768
+ next_entry = self .reprentries [i + 1 ]
762
769
if entry .style == "long" or \
763
770
entry .style == "short" and next_entry .style == "long" :
764
771
tw .sep (self .entrysep )
765
772
766
773
if self .extraline :
767
774
tw .line (self .extraline )
768
775
776
+
769
777
class ReprTracebackNative (ReprTraceback ):
770
778
def __init__ (self , tblines ):
771
779
self .style = "native"
772
780
self .reprentries = [ReprEntryNative (tblines )]
773
781
self .extraline = None
774
782
783
+
775
784
class ReprEntryNative (TerminalRepr ):
776
785
style = "native"
777
786
@@ -781,6 +790,7 @@ def __init__(self, tblines):
781
790
def toterminal (self , tw ):
782
791
tw .write ("" .join (self .lines ))
783
792
793
+
784
794
class ReprEntry (TerminalRepr ):
785
795
localssep = "_ "
786
796
@@ -797,15 +807,15 @@ def toterminal(self, tw):
797
807
for line in self .lines :
798
808
red = line .startswith ("E " )
799
809
tw .line (line , bold = True , red = red )
800
- #tw.line("")
810
+ # tw.line("")
801
811
return
802
812
if self .reprfuncargs :
803
813
self .reprfuncargs .toterminal (tw )
804
814
for line in self .lines :
805
815
red = line .startswith ("E " )
806
816
tw .line (line , bold = True , red = red )
807
817
if self .reprlocals :
808
- #tw.sep(self.localssep, "Locals")
818
+ # tw.sep(self.localssep, "Locals")
809
819
tw .line ("" )
810
820
self .reprlocals .toterminal (tw )
811
821
if self .reprfileloc :
@@ -818,6 +828,7 @@ def __str__(self):
818
828
self .reprlocals ,
819
829
self .reprfileloc )
820
830
831
+
821
832
class ReprFileLocation (TerminalRepr ):
822
833
def __init__ (self , path , lineno , message ):
823
834
self .path = str (path )
@@ -834,6 +845,7 @@ def toterminal(self, tw):
834
845
tw .write (self .path , bold = True , red = True )
835
846
tw .line (":%s: %s" % (self .lineno , msg ))
836
847
848
+
837
849
class ReprLocals (TerminalRepr ):
838
850
def __init__ (self , lines ):
839
851
self .lines = lines
@@ -842,6 +854,7 @@ def toterminal(self, tw):
842
854
for line in self .lines :
843
855
tw .line (line )
844
856
857
+
845
858
class ReprFuncArgs (TerminalRepr ):
846
859
def __init__ (self , args ):
847
860
self .args = args
@@ -850,11 +863,11 @@ def toterminal(self, tw):
850
863
if self .args :
851
864
linesofar = ""
852
865
for name , value in self .args :
853
- ns = "%s = %s" % (name , value )
866
+ ns = "%s = %s" % (name , value )
854
867
if len (ns ) + len (linesofar ) + 2 > tw .fullwidth :
855
868
if linesofar :
856
869
tw .line (linesofar )
857
- linesofar = ns
870
+ linesofar = ns
858
871
else :
859
872
if linesofar :
860
873
linesofar += ", " + ns
0 commit comments