python - Grab any exception in PyQt -


i've created gui app in pyqt, want share many people. unexpected exceptions , take granted - after every exception improve code , gets better , better.

i use logger recording these exceptions , special hook pyqt silenced exceptions. code looks this:

logger

def setlogger(level=logging.debug,               name="my_logger",               file=join("src", "log.out")):      logger = logging.getlogger(name)     logger.setlevel(level)      # console logger     ch = logging.streamhandler()     console_lvl = logging.debug     ch.setlevel(console_lvl)     formatter = logging.formatter('%(message)s')     ch.setformatter(formatter)      #file logger         fh = logging.filehandler(file)     fh.setlevel(level)     formatter = logging.formatter(         '%(asctime)s :: %(name)s - %(message)s',         datefmt='%m/%d/%y %i:%m:%s %p')     fh.setformatter(formatter)     logger.addhandler(ch)     logger.addhandler(fh)     return logger 

2 typical classes in gui

class mainwindow(qtgui.qmainwindow):      def __init__(self):         self.logger = setlogger(name='main_window')         [...]  class uploaddialog(qtgui.qdialog):  def __init__(self):     self.logger = setlogger(name='upload_dialog')     [...] 

and on
pyqt has silenced exceptions - exception occurs program keeps running. use special hook situation (have seen somewhere, not code)

import sys sys._excepthook = sys.excepthook  def exception_hook(exctype, value, traceback):     sys._excepthook(exctype, value, traceback)     sys.exit(1) 

and want grab exception, users of program in case of crash send me log.out , see full log.

i've started this.

try:     app = qtgui.qapplication(sys.argv)     window = mainwindow()     window.show()     sys.exit(app.exec_()) except exception:     logger.exception("mainwindow exception") 

i worked well, until exception raised inside uploaddialog. nothing 'recorded'. did:

#inside mainwindow class def rundialog(self):     try:         dialog = uploaddialog()         dialog.exec_()     except exception:         self.logger.exception("dialog exception")         sys.exit(1) 

again, ok, until exception rised inside workthread (inherited qthread class background upload). , again.

#inside uploaddialog def upload(self):     # initialized self.task = workthread()     try:         self.task.start()     except exception:         self.logger.exception("exception in workthread") 

and exception raised not after start of workthread when initializing, so

class uploaddialog(qtgui.qdialog):      def __init__(self):         try:             self.task = workthread()         except exception:             self.logger.exception("exception in workthread") 

and here took head , screamed myself - "stop it. you're doing wrong. it's not pythonic, it's dirty, makes eyes bleed".

so, i'm asking - there elegant , pythonic way grab absolutely exception, raised in pyqt app 1 try...except block?

the point of using excepthook can monitor all exceptions raised program, , handle them centrally.

so should rid of of try/except blocks, , log exceptions inside excepthook function. "most", because may affected bug 1230540, sys.excepthook not called correctly outside of main thread. see tracker thread workarounds that, or this answer.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -