Ignore:
Timestamp:
01/11/13 17:16:34 (15 months ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
Message:

Override sys.excepthook with a custom handler.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Orange/OrangeCanvas/application/outputview.py

    r11257 r11258  
    11""" 
    22""" 
     3import sys 
     4import traceback 
     5 
    36from functools import wraps 
    47from PyQt4.QtGui import ( 
     
    710) 
    811 
    9 from PyQt4.QtCore import Qt, QObject, QEvent, QCoreApplication 
     12from PyQt4.QtCore import Qt, QObject, QEvent, QCoreApplication, QThread 
    1013from PyQt4.QtCore import pyqtSignal as Signal 
    1114 
     
    264267            event.call() 
    265268            event.accept() 
     269 
     270 
     271class ExceptHook(QObject): 
     272    handledException = Signal() 
     273 
     274    def __init__(self, parent=None, stream=None): 
     275        QObject.__init__(self, parent) 
     276        self.stream = stream 
     277 
     278    def __call__(self, exc_type, exc_value, tb): 
     279        text = traceback.format_exception(exc_type, exc_value, tb) 
     280        separator = "-" * 80 + "\n" 
     281        if QThread.currentThread() != QCoreApplication.instance().thread(): 
     282            header = exc_type.__name__ + " (in non GUI thread)" 
     283        else: 
     284            header = exc_type.__name__ 
     285 
     286        header_fmt = "%%%is\n" 
     287        if tb: 
     288            header += (header_fmt % (80 - len(header))) % text[0].strip() 
     289            del text[0] 
     290        else: 
     291            header 
     292 
     293        if self.stream is None: 
     294            stream = sys.stderr 
     295        else: 
     296            stream = self.stream 
     297 
     298        stream.writelines([separator, header] + text) 
     299 
     300        self.handledException.emit() 
Note: See TracChangeset for help on using the changeset viewer.