util/ExceptionHandler.py
changeset 3546 ee51d1deadfd
parent 3444 91b2c014494e
child 3750 f62625418bff
equal deleted inserted replaced
3545:67e1707f24c8 3546:ee51d1deadfd
    79 
    79 
    80     return res
    80     return res
    81 
    81 
    82 
    82 
    83 def get_last_traceback(tb):
    83 def get_last_traceback(tb):
    84     while tb.tb_next:
    84     while True:
    85         tb = tb.tb_next
    85         if not hasattr(tb, "tb_next"):
       
    86             break
       
    87         if tb.tb_next:
       
    88             tb = tb.tb_next
       
    89         else:
       
    90             break
       
    91 
    86     return tb
    92     return tb
    87 
    93 
    88 
    94 
    89 def format_namespace(d, indent='    '):
    95 def format_namespace(d, indent='    '):
    90     return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.iteritems()])
    96     return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.iteritems()])
   109             'cwd': os.getcwd(),
   115             'cwd': os.getcwd(),
   110         }
   116         }
   111         if e_traceback:
   117         if e_traceback:
   112             info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value)
   118             info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value)
   113             last_tb = get_last_traceback(e_traceback)
   119             last_tb = get_last_traceback(e_traceback)
   114             exception_locals = last_tb.tb_frame.f_locals  # the locals at the level of the stack trace where the exception actually occurred
   120             # save locals at the level of the stack trace where the exception actually occurred
       
   121             exception_locals = last_tb.tb_frame.f_locals if last_tb else {};
   115             info['locals'] = format_namespace(exception_locals)
   122             info['locals'] = format_namespace(exception_locals)
   116             if 'self' in exception_locals:
   123             if 'self' in exception_locals:
   117                 try:
   124                 try:
   118                     info['self'] = format_namespace(exception_locals['self'].__dict__)
   125                     info['self'] = format_namespace(exception_locals['self'].__dict__)
   119                 except Exception:
   126                 except Exception:
   132         output.close()
   139         output.close()
   133 
   140 
   134     def handle_exception(e_type, e_value, e_traceback, exit=False):
   141     def handle_exception(e_type, e_value, e_traceback, exit=False):
   135         traceback.print_exception(e_type, e_value, e_traceback)  # this is very helpful when there's an exception in the rest of this func
   142         traceback.print_exception(e_type, e_value, e_traceback)  # this is very helpful when there's an exception in the rest of this func
   136         last_tb = get_last_traceback(e_traceback)
   143         last_tb = get_last_traceback(e_traceback)
   137         ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno)
   144         ex = (last_tb.tb_frame.f_code.co_filename if last_tb else "unknown",
       
   145               last_tb.tb_frame.f_lineno if last_tb else None)
   138         if ex not in ignored_exceptions:
   146         if ex not in ignored_exceptions:
   139             ignored_exceptions.append(ex)
   147             ignored_exceptions.append(ex)
   140             date = time.ctime()
   148             date = time.ctime()
   141             path = tempfile.gettempdir()+os.sep+wx.GetApp().GetAppName()
   149             path = tempfile.gettempdir()+os.sep+wx.GetApp().GetAppName()
   142             bug_report_path = path + os.sep + "bug_report_" + time.strftime("%Y_%m_%d__%H-%M-%S") + ".txt"
   150             bug_report_path = path + os.sep + "bug_report_" + time.strftime("%Y_%m_%d__%H-%M-%S") + ".txt"