Do not store a reference to the main window in sys.excepthook

This commit is contained in:
Kovid Goyal 2016-02-13 11:25:40 +05:30
parent 942d18810c
commit 208694ec5a
5 changed files with 20 additions and 6 deletions

View File

@ -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()

View File

@ -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)]

View File

@ -5,7 +5,7 @@ __license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
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()

View File

@ -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)

View File

@ -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_()