99import re
1010import sys
1111import tokenize
12- import types
1312
1413from functools import partial
1514from itertools import chain
1615
1716from colorama import AnsiToWin32
17+ from colorama import Back
1818from colorama import Fore
1919from colorama import Style
2020from fields import Fields
2121
22-
23-
2422__version__ = "0.2.1"
2523__all__ = 'Q' , 'When' , 'And' , 'Or' , 'CodePrinter' , 'Debugger' , 'VarsPrinter' , 'trace' , 'stop'
2624
3634 'exception' : '' ,
3735 'detail' : '' ,
3836 'vars' : '' ,
37+ 'vars-name' : '' ,
3938 'call' : '' ,
4039 'line' : '' ,
40+ 'internal-failure' : '' ,
41+ 'internal-detail' : '' ,
4142}
4243EVENT_COLORS = {
4344 'reset' : Style .RESET_ALL ,
4950 'return' : Style .BRIGHT + Fore .GREEN ,
5051 'exception' : Style .BRIGHT + Fore .RED ,
5152 'detail' : Style .NORMAL ,
52- 'vars' : Fore .MAGENTA ,
53+ 'vars' : Style .RESET_ALL + Fore .MAGENTA ,
54+ 'vars-name' : Style .BRIGHT ,
55+ 'internal-failure' : Back .RED + Style .BRIGHT + Fore .RED ,
56+ 'internal-detail' : Fore .WHITE ,
5357}
5458CODE_COLORS = {
5559 'call' : Fore .RESET + Style .BRIGHT ,
@@ -439,6 +443,12 @@ def stream(self, value):
439443 self .event_colors = NO_COLORS
440444 self .code_colors = NO_COLORS
441445
446+ def _safe_repr (self , obj ):
447+ try :
448+ return repr (obj )
449+ except Exception as exc :
450+ return "{internal-failure}!!! FAILED REPR: {internal-detail}{!r}" .format (exc , ** self .event_colors )
451+
442452
443453class CodePrinter (Fields .stream .filename_alignment , ColorStreamAction ):
444454 """
@@ -480,11 +490,11 @@ def __call__(self, event, sep=os.path.sep, join=os.path.join):
480490 ))
481491
482492 if event .kind in ('return' , 'exception' ):
483- self .stream .write ("{:>{align}} {continuation}{:9} {color}{} value: {detail}{!r }{reset}\n " .format (
493+ self .stream .write ("{:>{align}} {continuation}{:9} {color}{} value: {detail}{}{reset}\n " .format (
484494 "" ,
485495 "..." ,
486496 event .kind ,
487- event .arg ,
497+ self . _safe_repr ( event .arg ) ,
488498 align = self .filename_alignment ,
489499 color = self .event_colors [event .kind ],
490500 ** self .event_colors
@@ -536,7 +546,7 @@ def _safe_eval(self, code, event):
536546 try :
537547 return eval (code , event .globals if self .globals else {}, event .locals )
538548 except Exception as exc :
539- return "{exception }FAILED EVAL: {}" .format (exc , ** self .event_colors )
549+ return "{internal-failure }FAILED EVAL: {internal-detail}{!r }" .format (exc , ** self .event_colors )
540550
541551 def __call__ (self , event ):
542552 """
@@ -549,11 +559,11 @@ def __call__(self, event):
549559
550560 for code , symbols in self .names .items ():
551561 if frame_symbols >= symbols :
552- self .stream .write ("{:>{align}} {vars}{:9} {reset }{} {vars}=> {reset}{!r }\n " .format (
562+ self .stream .write ("{:>{align}} {vars}{:9} {vars-name }{} {vars}=> {reset}{}{reset }\n " .format (
553563 "" ,
554564 "vars" if first else "..." ,
555565 code ,
556- self ._safe_eval (code , event ),
566+ self ._safe_repr ( self . _safe_eval (code , event ) ),
557567 align = self .filename_alignment ,
558568 ** self .event_colors
559569 ))
0 commit comments