BeremizIDE.py
changeset 1792 4d1de8b0183f
parent 1783 3311eea28d56
child 1795 e27d253bd0ba
equal deleted inserted replaced
1791:3216ed1ba1f7 1792:4d1de8b0183f
    39 import wx.stc
    39 import wx.stc
    40 import cPickle
    40 import cPickle
    41 import types
    41 import types
    42 import time
    42 import time
    43 import re
    43 import re
    44 import platform
       
    45 import time
    44 import time
    46 import traceback
       
    47 import commands
    45 import commands
    48 import threading
       
    49 from threading import Lock, Timer, currentThread
    46 from threading import Lock, Timer, currentThread
    50 from time import time as gettime
    47 from time import time as gettime
    51 
    48 
    52 import util.paths as paths
    49 import util.paths as paths
    53 from docutil import OpenHtmlFrame
    50 from docutil import OpenHtmlFrame
  1104             if selected != -1:
  1101             if selected != -1:
  1105                 viewer = self.TabsOpened.GetPage(selected)
  1102                 viewer = self.TabsOpened.GetPage(selected)
  1106                 viewer.AddHighlight(infos[1:], start, end, highlight_type)
  1103                 viewer.AddHighlight(infos[1:], start, end, highlight_type)
  1107         else:
  1104         else:
  1108             IDEFrame.ShowHighlight(self, infos, start, end, highlight_type)
  1105             IDEFrame.ShowHighlight(self, infos, start, end, highlight_type)
  1109 
       
  1110 
       
  1111 # -------------------------------------------------------------------------------
       
  1112 #                               Exception Handler
       
  1113 # -------------------------------------------------------------------------------
       
  1114 
       
  1115 Max_Traceback_List_Size = 20
       
  1116 
       
  1117 
       
  1118 def Display_Exception_Dialog(e_type, e_value, e_tb, bug_report_path):
       
  1119     trcbck_lst = []
       
  1120     for i, line in enumerate(traceback.extract_tb(e_tb)):
       
  1121         trcbck = " " + str(i+1) + ". "
       
  1122         if line[0].find(os.getcwd()) == -1:
       
  1123             trcbck += "file : " + str(line[0]) + ",   "
       
  1124         else:
       
  1125             trcbck += "file : " + str(line[0][len(os.getcwd()):]) + ",   "
       
  1126         trcbck += "line : " + str(line[1]) + ",   " + "function : " + str(line[2])
       
  1127         trcbck_lst.append(trcbck)
       
  1128 
       
  1129     # Allow clicking....
       
  1130     cap = wx.Window_GetCapture()
       
  1131     if cap:
       
  1132         cap.ReleaseMouse()
       
  1133 
       
  1134     dlg = wx.SingleChoiceDialog(
       
  1135         None,
       
  1136         _("""
       
  1137 An unhandled exception (bug) occured. Bug report saved at :
       
  1138 (%s)
       
  1139 
       
  1140 Please be kind enough to send this file to:
       
  1141 beremiz-devel@lists.sourceforge.net
       
  1142 
       
  1143 You should now restart program.
       
  1144 
       
  1145 Traceback:
       
  1146 """) % bug_report_path +
       
  1147         repr(e_type) + " : " + repr(e_value),
       
  1148         _("Error"),
       
  1149         trcbck_lst)
       
  1150     try:
       
  1151         res = (dlg.ShowModal() == wx.ID_OK)
       
  1152     finally:
       
  1153         dlg.Destroy()
       
  1154 
       
  1155     return res
       
  1156 
       
  1157 
       
  1158 def get_last_traceback(tb):
       
  1159     while tb.tb_next:
       
  1160         tb = tb.tb_next
       
  1161     return tb
       
  1162 
       
  1163 
       
  1164 def format_namespace(d, indent='    '):
       
  1165     return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.iteritems()])
       
  1166 
       
  1167 
       
  1168 ignored_exceptions = []  # a problem with a line in a module is only reported once per session
       
  1169 
       
  1170 
       
  1171 def AddExceptHook(path, app_version='[No version]'):
       
  1172 
       
  1173     def save_bug_report(e_type, e_value, e_traceback, bug_report_path, date):
       
  1174         info = {
       
  1175             'app-title': wx.GetApp().GetAppName(),
       
  1176             'app-version': app_version,
       
  1177             'wx-version': wx.VERSION_STRING,
       
  1178             'wx-platform': wx.Platform,
       
  1179             'python-version': platform.python_version(),
       
  1180             'platform': platform.platform(),
       
  1181             'e-type': e_type,
       
  1182             'e-value': e_value,
       
  1183             'date': date,
       
  1184             'cwd': os.getcwd(),
       
  1185         }
       
  1186         if e_traceback:
       
  1187             info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value)
       
  1188             last_tb = get_last_traceback(e_traceback)
       
  1189             exception_locals = last_tb.tb_frame.f_locals  # the locals at the level of the stack trace where the exception actually occurred
       
  1190             info['locals'] = format_namespace(exception_locals)
       
  1191             if 'self' in exception_locals:
       
  1192                 try:
       
  1193                     info['self'] = format_namespace(exception_locals['self'].__dict__)
       
  1194                 except Exception:
       
  1195                     pass
       
  1196         if not os.path.exists(path):
       
  1197             os.mkdir(path)
       
  1198         output = open(bug_report_path, 'w')
       
  1199         lst = info.keys()
       
  1200         lst.sort()
       
  1201         for a in lst:
       
  1202             output.write(a + ":\n" + str(info[a]) + "\n\n")
       
  1203         output.close()
       
  1204 
       
  1205     def handle_exception(e_type, e_value, e_traceback):
       
  1206         traceback.print_exception(e_type, e_value, e_traceback)  # this is very helpful when there's an exception in the rest of this func
       
  1207         last_tb = get_last_traceback(e_traceback)
       
  1208         ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno)
       
  1209         if ex not in ignored_exceptions:
       
  1210             ignored_exceptions.append(ex)
       
  1211             date = time.ctime()
       
  1212             bug_report_path = path + os.sep + "bug_report_" + time.strftime("%Y_%m_%d__%H-%M-%S") + ".txt"
       
  1213             save_bug_report(e_type, e_value, e_traceback, bug_report_path, date)
       
  1214             Display_Exception_Dialog(e_type, e_value, e_traceback, bug_report_path)
       
  1215     # sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args)
       
  1216     sys.excepthook = handle_exception
       
  1217 
       
  1218     init_old = threading.Thread.__init__
       
  1219 
       
  1220     def init(self, *args, **kwargs):
       
  1221         init_old(self, *args, **kwargs)
       
  1222         run_old = self.run
       
  1223 
       
  1224         def run_with_except_hook(*args, **kw):
       
  1225             try:
       
  1226                 run_old(*args, **kw)
       
  1227             except (KeyboardInterrupt, SystemExit):
       
  1228                 raise
       
  1229             except Exception:
       
  1230                 sys.excepthook(*sys.exc_info())
       
  1231         self.run = run_with_except_hook
       
  1232     threading.Thread.__init__ = init