##// END OF EJS Templates
Initial refactoring to support structured traceback information.
Brian Granger -
Show More
@@ -105,23 +105,6 b' class SpaceInInput(exceptions.Exception): pass'
105 105
106 106 class Bunch: pass
107 107
108 class SyntaxTB(ultratb.ListTB):
109 """Extension which holds some state: the last exception value"""
110
111 def __init__(self,color_scheme = 'NoColor'):
112 ultratb.ListTB.__init__(self,color_scheme)
113 self.last_syntax_error = None
114
115 def __call__(self, etype, value, elist):
116 self.last_syntax_error = value
117 ultratb.ListTB.__call__(self,etype,value,elist)
118
119 def clear_err_state(self):
120 """Return the current error state and clear it"""
121 e = self.last_syntax_error
122 self.last_syntax_error = None
123 return e
124
125 108
126 109 def get_default_colors():
127 110 if sys.platform=='darwin':
@@ -1104,7 +1087,7 b' class InteractiveShell(Configurable, Magic):'
1104 1087
1105 1088 def init_traceback_handlers(self, custom_exceptions):
1106 1089 # Syntax error handler.
1107 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1090 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1108 1091
1109 1092 # The interactive one is initialized with an offset, meaning we always
1110 1093 # want to remove the topmost item in the traceback, which is our own
@@ -178,7 +178,7 b' def test_shist():'
178 178 # XXX failing for now, until we get clearcmd out of quarantine. But we should
179 179 # fix this and revert the skip to happen only if numpy is not around.
180 180 #@dec.skipif_not_numpy
181 @dec.skipknownfailure
181 @dec.skip_known_failure
182 182 def test_numpy_clear_array_undec():
183 183 from IPython.extensions import clearcmd
184 184
@@ -385,7 +385,7 b' class ListTB(TBTools):'
385 385 IPython.utils.io.Term.cerr.write(self.text(etype,value,elist))
386 386 IPython.utils.io.Term.cerr.write('\n')
387 387
388 def text(self, etype, value, elist, context=5):
388 def structured_traceback(self, etype, value, elist, context=5):
389 389 """Return a color formatted string with the traceback info.
390 390
391 391 Parameters
@@ -414,6 +414,12 b' class ListTB(TBTools):'
414 414 for line in lines[:-1]:
415 415 out_string.append(" "+line)
416 416 out_string.append(lines[-1])
417 return out_string
418
419 def text(self, etype, value, elist, context=5):
420 out_string = ListTB.structured_traceback(
421 self, etype, value, elist, context
422 )
417 423 return ''.join(out_string)
418 424
419 425 def _format_list(self, extracted_list):
@@ -569,7 +575,7 b' class VerboseTB(TBTools):'
569 575 self.long_header = long_header
570 576 self.include_vars = include_vars
571 577
572 def text(self, etype, evalue, etb, context=5):
578 def structured_traceback(self, etype, evalue, etb, context=5):
573 579 """Return a nice text document describing the traceback."""
574 580
575 581 # some locals
@@ -861,7 +867,14 b' class VerboseTB(TBTools):'
861 867 # vds: <<
862 868
863 869 # return all our info assembled as a single string
864 return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
870 # return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
871 return [head] + frames + [''.join(exception[0])]
872
873 def text(self, etype, evalue, etb, context=5):
874 tb_list = VerboseTB.structured_traceback(
875 self, etype, evalue, etb, context
876 )
877 return '\n'.join(tb_list)
865 878
866 879 def debugger(self,force=False):
867 880 """Call up the pdb debugger if desired, always clean up the tb
@@ -957,17 +970,13 b' class FormattedTB(VerboseTB,ListTB):'
957 970 else:
958 971 return None
959 972
960 def text(self, etype, value, tb,context=5,mode=None):
961 """Return formatted traceback.
962
963 If the optional mode parameter is given, it overrides the current
964 mode."""
965
966 if mode is None:
967 mode = self.mode
973 def structured_traceback(self, etype, value, tb, context=5, mode=None):
974 mode = self.mode if mode is None else mode
968 975 if mode in self.verbose_modes:
969 # verbose modes need a full traceback
970 return VerboseTB.text(self,etype, value, tb,context=5)
976 # Verbose modes need a full traceback
977 return VerboseTB.structured_traceback(
978 self, etype, value, tb, context
979 )
971 980 else:
972 981 # We must check the source cache because otherwise we can print
973 982 # out-of-date source code.
@@ -976,7 +985,19 b' class FormattedTB(VerboseTB,ListTB):'
976 985 elist = self._extract_tb(tb)
977 986 if len(elist) > self.tb_offset:
978 987 del elist[:self.tb_offset]
979 return ListTB.text(self,etype,value,elist)
988 return ListTB.structured_traceback(
989 self, etype, value, elist, context
990 )
991
992 def text(self, etype, value, tb, context=5, mode=None):
993 """Return formatted traceback.
994
995 If the optional mode parameter is given, it overrides the current
996 mode."""
997 tb_list = FormattedTB.structured_traceback(
998 self, etype, value, tb, context, mode
999 )
1000 return '\n'.join(tb_list)
980 1001
981 1002 def set_mode(self,mode=None):
982 1003 """Switch to the desired mode.
@@ -1051,13 +1072,23 b' class AutoFormattedTB(FormattedTB):'
1051 1072 except KeyboardInterrupt:
1052 1073 print "\nKeyboardInterrupt"
1053 1074
1054 def text(self,etype=None,value=None,tb=None,context=5,mode=None):
1075 def structured_traceback(self, etype=None, value=None, tb=None,
1076 context=5, mode=None):
1055 1077 if etype is None:
1056 1078 etype,value,tb = sys.exc_info()
1057 1079 self.tb = tb
1058 return FormattedTB.text(self,etype,value,tb,context=5,mode=mode)
1080 return FormattedTB.structured_traceback(
1081 self, etype, value, tb, context, mode
1082 )
1083
1084 def text(self, etype=None, value=None, tb=None, context=5, mode=None):
1085 tb_list = AutoFormattedTB.structured_traceback(
1086 self, etype, value, tb, context, mode
1087 )
1088 return '\n'.join(tb_list)
1059 1089
1060 1090 #---------------------------------------------------------------------------
1091
1061 1092 # A simple class to preserve Nathan's original functionality.
1062 1093 class ColorTB(FormattedTB):
1063 1094 """Shorthand to initialize a FormattedTB in Linux colors mode."""
@@ -1065,6 +1096,24 b' class ColorTB(FormattedTB):'
1065 1096 FormattedTB.__init__(self,color_scheme=color_scheme,
1066 1097 call_pdb=call_pdb)
1067 1098
1099
1100 class SyntaxTB(ListTB):
1101 """Extension which holds some state: the last exception value"""
1102
1103 def __init__(self,color_scheme = 'NoColor'):
1104 ListTB.__init__(self,color_scheme)
1105 self.last_syntax_error = None
1106
1107 def __call__(self, etype, value, elist):
1108 self.last_syntax_error = value
1109 ListTB.__call__(self,etype,value,elist)
1110
1111 def clear_err_state(self):
1112 """Return the current error state and clear it"""
1113 e = self.last_syntax_error
1114 self.last_syntax_error = None
1115 return e
1116
1068 1117 #----------------------------------------------------------------------------
1069 1118 # module testing (minimal)
1070 1119 if __name__ == "__main__":
General Comments 0
You need to be logged in to leave comments. Login now