From 67b4669f240e268f827b910c7160703baf4bf3f2 2021-03-05 16:41:14 From: Matthias Bussonnier Date: 2021-03-05 16:41:14 Subject: [PATCH] Backport PR #12842: Reclaim sys.excepthook for interactive shell in %gui qt --- diff --git a/IPython/terminal/pt_inputhooks/qt.py b/IPython/terminal/pt_inputhooks/qt.py index 9f5e1b4..1c93797 100644 --- a/IPython/terminal/pt_inputhooks/qt.py +++ b/IPython/terminal/pt_inputhooks/qt.py @@ -1,6 +1,7 @@ import sys import os from IPython.external.qt_for_kernel import QtCore, QtGui +from IPython import get_ipython # If we create a QApplication, keep a reference to it so that it doesn't get # garbage collected. @@ -8,6 +9,12 @@ _appref = None _already_warned = False +def _reclaim_excepthook(): + shell = get_ipython() + if shell is not None: + sys.excepthook = shell.excepthook + + def inputhook(context): global _appref app = QtCore.QCoreApplication.instance() @@ -27,6 +34,13 @@ def inputhook(context): return QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) _appref = app = QtGui.QApplication([" "]) + + # "reclaim" IPython sys.excepthook after event loop starts + # without this, it defaults back to BaseIPythonApplication.excepthook + # and exceptions in the Qt event loop are rendered without traceback + # formatting and look like "bug in IPython". + QtCore.QTimer.singleShot(0, _reclaim_excepthook) + event_loop = QtCore.QEventLoop(app) if sys.platform == 'win32':