From 208694ec5a61e5e21d5a9ca9d4b940fc4c1ece3c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 13 Feb 2016 11:25:40 +0530 Subject: [PATCH] Do not store a reference to the main window in sys.excepthook --- src/calibre/gui2/lrf_renderer/main.py | 2 +- src/calibre/gui2/main.py | 2 +- src/calibre/gui2/main_window.py | 17 ++++++++++++++++- src/calibre/gui2/tweak_book/main.py | 2 +- src/calibre/gui2/viewer/main.py | 3 +-- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/calibre/gui2/lrf_renderer/main.py b/src/calibre/gui2/lrf_renderer/main.py index 0f3e02585c..2c96a8b203 100644 --- a/src/calibre/gui2/lrf_renderer/main.py +++ b/src/calibre/gui2/lrf_renderer/main.py @@ -312,7 +312,7 @@ def main(args=sys.argv, logger=None): opts = normalize_settings(parser, opts) stream = open(args[1], 'rb') if len(args) > 1 else None main = file_renderer(stream, opts, logger=logger) - sys.excepthook = main.unhandled_exception + main.set_exception_handler() main.show() main.render() main.activateWindow() diff --git a/src/calibre/gui2/main.py b/src/calibre/gui2/main.py index 50745ddd84..5775d00cba 100644 --- a/src/calibre/gui2/main.py +++ b/src/calibre/gui2/main.py @@ -222,7 +222,7 @@ class GuiRunner(QObject): prints('Started up in %.2f seconds'%(time.time() - self.startup_time), 'with', len(db.data), 'books') add_filesystem_book = partial(main.iactions['Add Books'].add_filesystem_book, allow_device=False) - sys.excepthook = main.unhandled_exception + main.set_exception_handler() if len(self.args) > 1: files = [os.path.abspath(p) for p in self.args[1:] if not os.path.isdir(p)] diff --git a/src/calibre/gui2/main_window.py b/src/calibre/gui2/main_window.py index 075a19f5b9..cbf69c8d17 100644 --- a/src/calibre/gui2/main_window.py +++ b/src/calibre/gui2/main_window.py @@ -5,7 +5,7 @@ __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' -import StringIO, traceback, sys, gc +import StringIO, traceback, sys, gc, weakref from PyQt5.Qt import (QMainWindow, QTimer, QAction, QMenu, QMenuBar, QIcon, pyqtSignal, QObject) @@ -68,6 +68,18 @@ class GarbageCollector(QObject): for obj in gc.garbage: print (obj, repr(obj), type(obj)) +class ExceptionHandler(object): + + def __init__(self, main_window): + self.wref = weakref.ref(main_window) + + def __call__(self, type, value, tb): + mw = self.wref() + if mw is not None: + mw.unhandled_exception(type, value, tb) + else: + sys.__excepthook__(type, value, tb) + class MainWindow(QMainWindow): ___menu_bar = None @@ -115,6 +127,9 @@ class MainWindow(QMainWindow): else: gc.enable() if enabled else gc.disable() + def set_exception_handler(self): + sys.excepthook = ExceptionHandler(self) + def unhandled_exception(self, type, value, tb): if type == KeyboardInterrupt: self.keyboard_interrupt.emit() diff --git a/src/calibre/gui2/tweak_book/main.py b/src/calibre/gui2/tweak_book/main.py index 044b805bbb..144a85b22e 100644 --- a/src/calibre/gui2/tweak_book/main.py +++ b/src/calibre/gui2/tweak_book/main.py @@ -69,7 +69,7 @@ def _run(args, notify=None): Application.setOrganizationName(ORG_NAME) Application.setApplicationName(APP_UID) main = Main(opts, notify=notify) - sys.excepthook = main.unhandled_exception + main.set_exception_handler() main.show() if len(args) > 1: main.boss.open_book(args[1], edit_file=args[2:], clear_notify_data=False) diff --git a/src/calibre/gui2/viewer/main.py b/src/calibre/gui2/viewer/main.py index 169d97cdb0..cc15f38be5 100644 --- a/src/calibre/gui2/viewer/main.py +++ b/src/calibre/gui2/viewer/main.py @@ -1187,8 +1187,7 @@ def main(args=sys.argv): # turn_off_internal_scrollbars does not take effect for the first # rendered document main.view.load_path(P('viewer/blank.html', allow_user_override=False)) - - sys.excepthook = main.unhandled_exception + main.set_exception_handler() main.show() if opts.raise_window: main.raise_()