##// END OF EJS Templates
i1673 some DRY refactoring
Justyna Ilczuk -
Show More
@@ -72,7 +72,7 b' Inheritance diagram:'
72 72 :parts: 3
73 73 """
74 74
75 # *****************************************************************************
75 #*****************************************************************************
76 76 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
77 77 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
78 78 #
@@ -129,7 +129,7 b' INDENT_SIZE = 8'
129 129 # to users of ultratb who are NOT running inside ipython.
130 130 DEFAULT_SCHEME = 'NoColor'
131 131
132 #---------------------------------------------------------------------------
132 # ---------------------------------------------------------------------------
133 133 # Code begins
134 134
135 135 # Utility functions
@@ -936,6 +936,31 b' class VerboseTB(TBTools):'
936 936 exception.append('\n%s%s = %s' % (indent, name, value))
937 937 return exception
938 938
939 def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset):
940 # some locals
941 try:
942 etype = etype.__name__
943 except AttributeError:
944 pass
945
946 tb_offset = self.tb_offset if tb_offset is None else tb_offset
947 head = self.prepare_header(etype, self.long_header)
948 records = self.get_records(etb, number_of_lines_of_context, tb_offset)
949
950 frames = self.format_records(records)
951 if records is None:
952 return ""
953
954 formatted_exception = self.format_exception(etype, evalue)
955 if records:
956 filepath, lnum = records[-1][1:3]
957 filepath = os.path.abspath(filepath)
958 ipinst = get_ipython()
959 if ipinst is not None:
960 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
961
962 return [[head] + frames + [''.join(formatted_exception[0])]]
963
939 964 def get_records(self, etb, number_of_lines_of_context, tb_offset):
940 965 try:
941 966 # Try the default getinnerframes and Alex's: Alex's fixes some
@@ -956,13 +981,13 b' class VerboseTB(TBTools):'
956 981 return None
957 982
958 983 def get_exception_from_context(self, evalue):
959 if hasattr(evalue, '__context__'): # and not evalue.__suppress_context__:
984 if hasattr(evalue, '__context__') and not evalue.__suppress_context__:
960 985 context = evalue.__context__
961 986 if not context:
962 987 return None
963 988 else:
964 989 exception_traceback = context.__traceback__
965 exception_type = context.__class__.__name__
990 exception_type = context.__class__
966 991 return exception_type, context, exception_traceback
967 992 else:
968 993 return None
@@ -970,62 +995,30 b' class VerboseTB(TBTools):'
970 995 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
971 996 number_of_lines_of_context=5):
972 997 """Return a nice text document describing the traceback."""
973 tb_offset = self.tb_offset if tb_offset is None else tb_offset
974 998
975 # some locals
976 try:
977 etype = etype.__name__
978 except AttributeError:
979 pass
980 999
981 1000 structured_traceback_parts = []
982 1001
983 exceptions = []
984 current_exception_value = evalue
985 1002 if py3compat.PY3:
986 while current_exception_value:
987 head = self.prepare_header(etype, self.long_header)
988 records = self.get_records(etb, number_of_lines_of_context, tb_offset)
989
990 frames = self.format_records(records)
991 if records is None:
992 return ""
993
994 formatted_exception = self.format_exception(etype, current_exception_value)
995 if records:
996 filepath, lnum = records[-1][1:3]
997 filepath = os.path.abspath(filepath)
998 ipinst = get_ipython()
999 if ipinst is not None:
1000 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
1001
1002 exceptions += [[head] + frames + [''.join(formatted_exception[0])]]
1003
1004 exception = self.get_exception_from_context(current_exception_value)
1003 formatted_exceptions = []
1004 while evalue:
1005 formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
1006 tb_offset)
1007 exception = self.get_exception_from_context(evalue)
1005 1008 if exception:
1006 exceptions += self.prepare_chained_exception_message(current_exception_value.__cause__)
1007 etype, current_exception_value, etb = exception
1009 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
1010 etype, evalue, etb = exception
1008 1011 else:
1009 break
1012 evalue = None
1010 1013
1011 for exception in reversed(exceptions):
1012 structured_traceback_parts += exception
1014 # we want to see exceptions in a reversed order:
1015 # the first exception should be on top
1016 for formatted_exception in reversed(formatted_exceptions):
1017 structured_traceback_parts += formatted_exception
1013 1018 else:
1014 head = self.prepare_header(etype, self.long_header)
1015 records = self.get_records(etb, number_of_lines_of_context, tb_offset)
1016
1017 frames = self.format_records(records)
1018 if records is None:
1019 return ""
1020
1021 exception = self.format_exception(etype, evalue)
1022 if records:
1023 filepath, lnum = records[-1][1:3]
1024 filepath = os.path.abspath(filepath)
1025 ipinst = get_ipython()
1026 if ipinst is not None:
1027 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
1028 structured_traceback_parts.append([head] + frames + [''.join(exception[0])])
1019 formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
1020 tb_offset)
1021 structured_traceback_parts.append(formatted_exception)
1029 1022
1030 1023 return structured_traceback_parts
1031 1024
@@ -1323,8 +1316,6 b' def nullrepr(value, repr=text_repr):'
1323 1316 return ''
1324 1317
1325 1318
1326
1327
1328 1319 #----------------------------------------------------------------------------
1329 1320
1330 1321 # module testing (minimal)
General Comments 0
You need to be logged in to leave comments. Login now