util/ExceptionHandler.py
author Edouard Tisserant
Thu, 17 Nov 2022 11:08:36 +0100
changeset 3682 c613afdab571
parent 1953 5736d25bb393
child 3303 0ffb41625592
permissions -rw-r--r--
IDE: Optimization of modification events processing in text editors.

Too many modifications types where registered, and then too many events were fired.
Also, in case of uninterrupted sequence of events, updates to the model is deferred to the end of that sequence (wx.Callafter).
1792
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     3
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     6
#
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     7
# Copyright (C) 2007:      Edouard TISSERANT and Laurent BESSARD
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     8
# Copyright (C) 2016-2017: Andrey Skvortsov <andrej.skvortzov@gmail.com>
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     9
#
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    10
# See COPYING file for copyrights details.
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    11
#
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    12
# This program is free software; you can redistribute it and/or
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    13
# modify it under the terms of the GNU General Public License
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    14
# as published by the Free Software Foundation; either version 2
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    15
# of the License, or (at your option) any later version.
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    16
#
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    17
# This program is distributed in the hope that it will be useful,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    20
# GNU General Public License for more details.
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    21
#
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    22
# You should have received a copy of the GNU General Public License
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    23
# along with this program; if not, write to the Free Software
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    24
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    25
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1792
diff changeset
    26
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1792
diff changeset
    27
from __future__ import absolute_import
1792
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    28
import os
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    29
import sys
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    30
import time
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    31
import tempfile
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    32
import platform
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    33
import traceback
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    34
import threading
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    35
import wx
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    36
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    37
Max_Traceback_List_Size = 20
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    38
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    39
1941
cde74a39df51 Fixed Exception dialog disapearing after a view second when exception occurs during app startup.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1881
diff changeset
    40
def Display_Exception_Dialog(e_type, e_value, e_tb, bug_report_path, exit):
1792
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    41
    trcbck_lst = []
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    42
    for i, line in enumerate(traceback.extract_tb(e_tb)):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    43
        trcbck = " " + str(i+1) + ". "
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    44
        if line[0].find(os.getcwd()) == -1:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    45
            trcbck += "file : " + str(line[0]) + ",   "
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    46
        else:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    47
            trcbck += "file : " + str(line[0][len(os.getcwd()):]) + ",   "
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    48
        trcbck += "line : " + str(line[1]) + ",   " + "function : " + str(line[2])
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    49
        trcbck_lst.append(trcbck)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    50
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    51
    # Allow clicking....
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    52
    cap = wx.Window_GetCapture()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    53
    if cap:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    54
        cap.ReleaseMouse()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    55
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    56
    dlg = wx.SingleChoiceDialog(
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    57
        None,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    58
        _("""
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    59
An unhandled exception (bug) occured. Bug report saved at :
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    60
(%s)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    61
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    62
Please be kind enough to send this file to:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    63
beremiz-devel@lists.sourceforge.net
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    64
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    65
You should now restart program.
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    66
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    67
Traceback:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    68
""") % bug_report_path +
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    69
        repr(e_type) + " : " + repr(e_value),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    70
        _("Error"),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    71
        trcbck_lst)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    72
    try:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    73
        res = (dlg.ShowModal() == wx.ID_OK)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    74
    finally:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    75
        dlg.Destroy()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    76
1953
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1941
diff changeset
    77
    if exit:
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1941
diff changeset
    78
        sys.exit()  # wx.Exit()
1941
cde74a39df51 Fixed Exception dialog disapearing after a view second when exception occurs during app startup.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1881
diff changeset
    79
1792
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    80
    return res
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    81
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    82
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    83
def get_last_traceback(tb):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    84
    while tb.tb_next:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    85
        tb = tb.tb_next
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    86
    return tb
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    87
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    88
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    89
def format_namespace(d, indent='    '):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    90
    return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.iteritems()])
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    91
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    92
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    93
ignored_exceptions = []  # a problem with a line in a module is only reported once per session
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    94
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    95
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    96
def AddExceptHook(app_version='[No version]'):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    97
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    98
    def save_bug_report(e_type, e_value, e_traceback, bug_report_path, date):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    99
        info = {
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   100
            'app-title': wx.GetApp().GetAppName(),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   101
            'app-version': app_version,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   102
            'wx-version': wx.VERSION_STRING,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   103
            'wx-platform': wx.Platform,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   104
            'python-version': platform.python_version(),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   105
            'platform': platform.platform(),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   106
            'e-type': e_type,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   107
            'e-value': e_value,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   108
            'date': date,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   109
            'cwd': os.getcwd(),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   110
        }
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   111
        if e_traceback:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   112
            info['traceback'] = ''.join(traceback.format_tb(e_traceback)) + '%s: %s' % (e_type, e_value)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   113
            last_tb = get_last_traceback(e_traceback)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   114
            exception_locals = last_tb.tb_frame.f_locals  # the locals at the level of the stack trace where the exception actually occurred
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   115
            info['locals'] = format_namespace(exception_locals)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   116
            if 'self' in exception_locals:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   117
                try:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   118
                    info['self'] = format_namespace(exception_locals['self'].__dict__)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   119
                except Exception:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   120
                    pass
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   121
        path = os.path.dirname(bug_report_path)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   122
        if not os.path.exists(path):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   123
            os.mkdir(path)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   124
        output = open(bug_report_path, 'w')
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   125
        lst = info.keys()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   126
        lst.sort()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   127
        for a in lst:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   128
            output.write(a + ":\n" + str(info[a]) + "\n\n")
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   129
        output.close()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   130
1953
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1941
diff changeset
   131
    def handle_exception(e_type, e_value, e_traceback, exit=False):
1792
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   132
        traceback.print_exception(e_type, e_value, e_traceback)  # this is very helpful when there's an exception in the rest of this func
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   133
        last_tb = get_last_traceback(e_traceback)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   134
        ex = (last_tb.tb_frame.f_code.co_filename, last_tb.tb_frame.f_lineno)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   135
        if ex not in ignored_exceptions:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   136
            ignored_exceptions.append(ex)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   137
            date = time.ctime()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   138
            path = tempfile.gettempdir()+os.sep+wx.GetApp().GetAppName()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   139
            bug_report_path = path + os.sep + "bug_report_" + time.strftime("%Y_%m_%d__%H-%M-%S") + ".txt"
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   140
            save_bug_report(e_type, e_value, e_traceback, bug_report_path, date)
1941
cde74a39df51 Fixed Exception dialog disapearing after a view second when exception occurs during app startup.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1881
diff changeset
   141
            wx.CallAfter(Display_Exception_Dialog, e_type, e_value, e_traceback, bug_report_path, exit)
1792
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   142
    # sys.excepthook = lambda *args: wx.CallAfter(handle_exception, *args)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   143
    sys.excepthook = handle_exception
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   144
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   145
    init_old = threading.Thread.__init__
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   146
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   147
    def init(self, *args, **kwargs):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   148
        init_old(self, *args, **kwargs)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   149
        run_old = self.run
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   150
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   151
        def run_with_except_hook(*args, **kw):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   152
            try:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   153
                run_old(*args, **kw)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   154
            except (KeyboardInterrupt, SystemExit):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   155
                raise
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   156
            except Exception:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   157
                sys.excepthook(*sys.exc_info())
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   158
        self.run = run_with_except_hook
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   159
    threading.Thread.__init__ = init
1941
cde74a39df51 Fixed Exception dialog disapearing after a view second when exception occurs during app startup.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1881
diff changeset
   160
cde74a39df51 Fixed Exception dialog disapearing after a view second when exception occurs during app startup.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1881
diff changeset
   161
    return handle_exception