util/ExceptionHandler.py
changeset 1941 cde74a39df51
parent 1881 091005ec69c4
child 1953 5736d25bb393
equal deleted inserted replaced
1940:8dc4ebc97777 1941:cde74a39df51
    35 import wx
    35 import wx
    36 
    36 
    37 Max_Traceback_List_Size = 20
    37 Max_Traceback_List_Size = 20
    38 
    38 
    39 
    39 
    40 def Display_Exception_Dialog(e_type, e_value, e_tb, bug_report_path):
    40 def Display_Exception_Dialog(e_type, e_value, e_tb, bug_report_path, exit):
    41     trcbck_lst = []
    41     trcbck_lst = []
    42     for i, line in enumerate(traceback.extract_tb(e_tb)):
    42     for i, line in enumerate(traceback.extract_tb(e_tb)):
    43         trcbck = " " + str(i+1) + ". "
    43         trcbck = " " + str(i+1) + ". "
    44         if line[0].find(os.getcwd()) == -1:
    44         if line[0].find(os.getcwd()) == -1:
    45             trcbck += "file : " + str(line[0]) + ",   "
    45             trcbck += "file : " + str(line[0]) + ",   "
    71         trcbck_lst)
    71         trcbck_lst)
    72     try:
    72     try:
    73         res = (dlg.ShowModal() == wx.ID_OK)
    73         res = (dlg.ShowModal() == wx.ID_OK)
    74     finally:
    74     finally:
    75         dlg.Destroy()
    75         dlg.Destroy()
       
    76 
       
    77     if exit : sys.exit() #wx.Exit()
    76 
    78 
    77     return res
    79     return res
    78 
    80 
    79 
    81 
    80 def get_last_traceback(tb):
    82 def get_last_traceback(tb):
   123         lst.sort()
   125         lst.sort()
   124         for a in lst:
   126         for a in lst:
   125             output.write(a + ":\n" + str(info[a]) + "\n\n")
   127             output.write(a + ":\n" + str(info[a]) + "\n\n")
   126         output.close()
   128         output.close()
   127 
   129 
   128     def handle_exception(e_type, e_value, e_traceback):
   130     def handle_exception(e_type, e_value, e_traceback, exit = False):
   129         traceback.print_exception(e_type, e_value, e_traceback)  # this is very helpful when there's an exception in the rest of this func
   131         traceback.print_exception(e_type, e_value, e_traceback)  # this is very helpful when there's an exception in the rest of this func
   130         last_tb = get_last_traceback(e_traceback)
   132         last_tb = get_last_traceback(e_traceback)
   131         ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno)
   133         ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno)
   132         if ex not in ignored_exceptions:
   134         if ex not in ignored_exceptions:
   133             ignored_exceptions.append(ex)
   135             ignored_exceptions.append(ex)
   134             date = time.ctime()
   136             date = time.ctime()
   135             path = tempfile.gettempdir()+os.sep+wx.GetApp().GetAppName()
   137             path = tempfile.gettempdir()+os.sep+wx.GetApp().GetAppName()
   136             bug_report_path = path + os.sep + "bug_report_" + time.strftime("%Y_%m_%d__%H-%M-%S") + ".txt"
   138             bug_report_path = path + os.sep + "bug_report_" + time.strftime("%Y_%m_%d__%H-%M-%S") + ".txt"
   137             save_bug_report(e_type, e_value, e_traceback, bug_report_path, date)
   139             save_bug_report(e_type, e_value, e_traceback, bug_report_path, date)
   138             Display_Exception_Dialog(e_type, e_value, e_traceback, bug_report_path)
   140             wx.CallAfter(Display_Exception_Dialog, e_type, e_value, e_traceback, bug_report_path, exit)
   139     # sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args)
   141     # sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args)
   140     sys.excepthook = handle_exception
   142     sys.excepthook = handle_exception
   141 
   143 
   142     init_old = threading.Thread.__init__
   144     init_old = threading.Thread.__init__
   143 
   145 
   152                 raise
   154                 raise
   153             except Exception:
   155             except Exception:
   154                 sys.excepthook(*sys.exc_info())
   156                 sys.excepthook(*sys.exc_info())
   155         self.run = run_with_except_hook
   157         self.run = run_with_except_hook
   156     threading.Thread.__init__ = init
   158     threading.Thread.__init__ = init
       
   159 
       
   160     return handle_exception