util/ExceptionHandler.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Sun, 08 Dec 2024 13:59:36 +0100
changeset 4064 f273ee835411
parent 3750 f62625418bff
permissions -rw-r--r--
PLCOpen editor: fix exception when using file open dialog

Consequence of wxPython 3 to 4 API changes.
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
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3546
diff changeset
    27
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....
3303
0ffb41625592 Preliminary support for WxPython 4.1.0. Needs more testing. Grid selection/focus seems broken, and probably many other bugs hidden in dialogs and editors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1953
diff changeset
    52
    cap = wx.Window.GetCapture()
1792
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):
3546
ee51d1deadfd wxPython4 sequels: fix Wx exceptions ignored because no traceback
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3444
diff changeset
    84
    while True:
ee51d1deadfd wxPython4 sequels: fix Wx exceptions ignored because no traceback
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3444
diff changeset
    85
        if not hasattr(tb, "tb_next"):
ee51d1deadfd wxPython4 sequels: fix Wx exceptions ignored because no traceback
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3444
diff changeset
    86
            break
ee51d1deadfd wxPython4 sequels: fix Wx exceptions ignored because no traceback
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3444
diff changeset
    87
        if tb.tb_next:
ee51d1deadfd wxPython4 sequels: fix Wx exceptions ignored because no traceback
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3444
diff changeset
    88
            tb = tb.tb_next
ee51d1deadfd wxPython4 sequels: fix Wx exceptions ignored because no traceback
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3444
diff changeset
    89
        else:
ee51d1deadfd wxPython4 sequels: fix Wx exceptions ignored because no traceback
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3444
diff changeset
    90
            break
ee51d1deadfd wxPython4 sequels: fix Wx exceptions ignored because no traceback
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3444
diff changeset
    91
1792
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    92
    return tb
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    93
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
def format_namespace(d, indent='    '):
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3546
diff changeset
    96
    return '\n'.join(['%s%s: %s' % (indent, k, repr(v)[:10000]) for k, v in d.items()])
1792
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
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    99
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
   100
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   101
3444
91b2c014494e IDE: in case of unhandled exception, write bug report to given log file, so that test report can have it.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   102
def AddExceptHook(app_version='[No version]', logf = None):
1792
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   103
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   104
    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
   105
        info = {
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   106
            'app-title': wx.GetApp().GetAppName(),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   107
            'app-version': app_version,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   108
            'wx-version': wx.VERSION_STRING,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   109
            'wx-platform': wx.Platform,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   110
            'python-version': platform.python_version(),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   111
            'platform': platform.platform(),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   112
            'e-type': e_type,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   113
            'e-value': e_value,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   114
            'date': date,
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   115
            'cwd': os.getcwd(),
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   116
        }
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   117
        if e_traceback:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   118
            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
   119
            last_tb = get_last_traceback(e_traceback)
3546
ee51d1deadfd wxPython4 sequels: fix Wx exceptions ignored because no traceback
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3444
diff changeset
   120
            # save locals at the level of the stack trace where the exception actually occurred
ee51d1deadfd wxPython4 sequels: fix Wx exceptions ignored because no traceback
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3444
diff changeset
   121
            exception_locals = last_tb.tb_frame.f_locals if last_tb else {};
1792
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   122
            info['locals'] = format_namespace(exception_locals)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   123
            if 'self' in exception_locals:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   124
                try:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   125
                    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
   126
                except Exception:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   127
                    pass
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   128
        path = os.path.dirname(bug_report_path)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   129
        if not os.path.exists(path):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   130
            os.mkdir(path)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   131
        output = open(bug_report_path, 'w')
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3546
diff changeset
   132
        lst = list(info.keys())
1792
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   133
        lst.sort()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   134
        for a in lst:
3444
91b2c014494e IDE: in case of unhandled exception, write bug report to given log file, so that test report can have it.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   135
            line = a + ":\n" + str(info[a]) + "\n\n"
91b2c014494e IDE: in case of unhandled exception, write bug report to given log file, so that test report can have it.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   136
            output.write(line)
91b2c014494e IDE: in case of unhandled exception, write bug report to given log file, so that test report can have it.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   137
            if logf is not None:
91b2c014494e IDE: in case of unhandled exception, write bug report to given log file, so that test report can have it.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   138
                logf.write(line)
1792
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   139
        output.close()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   140
1953
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1941
diff changeset
   141
    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
   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
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   143
        last_tb = get_last_traceback(e_traceback)
3546
ee51d1deadfd wxPython4 sequels: fix Wx exceptions ignored because no traceback
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3444
diff changeset
   144
        ex = (last_tb.tb_frame.f_code.co_filename if last_tb else "unknown",
ee51d1deadfd wxPython4 sequels: fix Wx exceptions ignored because no traceback
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3444
diff changeset
   145
              last_tb.tb_frame.f_lineno if last_tb else None)
1792
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   146
        if ex not in ignored_exceptions:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   147
            ignored_exceptions.append(ex)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   148
            date = time.ctime()
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   149
            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
   150
            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
   151
            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
   152
            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
   153
    # 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
   154
    sys.excepthook = handle_exception
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   155
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   156
    init_old = threading.Thread.__init__
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   157
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   158
    def init(self, *args, **kwargs):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   159
        init_old(self, *args, **kwargs)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   160
        run_old = self.run
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   161
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   162
        def run_with_except_hook(*args, **kw):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   163
            try:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   164
                run_old(*args, **kw)
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   165
            except (KeyboardInterrupt, SystemExit):
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   166
                raise
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   167
            except Exception:
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   168
                sys.excepthook(*sys.exc_info())
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   169
        self.run = run_with_except_hook
4d1de8b0183f unify exception handling of Beremiz and PLCOpenEditor
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   170
    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
   171
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
   172
    return handle_exception