Skip to content

Commit 76f9a74

Browse files
committed
inputhook: make PyQt4 plays nicer with pyreadline
PyQt4's own input hook is not really appropriate when PyReadline is active (and possibly other readline packages running the PyOS_InputHook repeatedly). Arguably this should be fixed in PyQt4 itself, but in the meantime we can install a local hook which does the Qt event loop processing in a less aggressive way.
1 parent a761793 commit 76f9a74

1 file changed

Lines changed: 21 additions & 21 deletions

File tree

‎IPython/lib/inputhook.py‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -197,31 +197,31 @@ def enable_qt4(self, app=None):
197197
"""
198198
from IPython.external.qt_for_kernel import QtCore, QtGui
199199

200-
if 'pyreadline' in sys.modules:
201-
# see IPython GitHub Issue #281 for more info on this issue
202-
# Similar intermittent behavior has been reported on OSX,
203-
# but not consistently reproducible
204-
warnings.warn("""PyReadline's inputhook can conflict with Qt, causing delays
205-
in interactive input. If you do see this issue, we recommend using another GUI
206-
toolkit if you can, or disable readline with the configuration option
207-
'TerminalInteractiveShell.readline_use=False', specified in a config file or
208-
at the command-line""",
209-
RuntimeWarning)
210-
211-
# PyQt4 has had this since 4.3.1. In version 4.2, PyOS_InputHook
212-
# was set when QtCore was imported, but if it ever got removed,
213-
# you couldn't reset it. For earlier versions we can
214-
# probably implement a ctypes version.
215-
try:
216-
QtCore.pyqtRestoreInputHook()
217-
except AttributeError:
218-
pass
219-
220-
self._current_gui = GUI_QT4
221200
if app is None:
222201
app = QtCore.QCoreApplication.instance()
223202
if app is None:
224203
app = QtGui.QApplication([" "])
204+
205+
# Always use the following input hook instead of PyQt4's
206+
# default one, as it interacts better with readline packages
207+
# (issue #481)
208+
209+
def inputhook_qt4():
210+
try:
211+
app.processEvents(QtCore.QEventLoop.AllEvents, 300)
212+
if not stdin_ready():
213+
timer = QtCore.QTimer()
214+
timer.timeout.connect(app.quit)
215+
while not stdin_ready():
216+
timer.start(50)
217+
app.exec_()
218+
timer.stop()
219+
except KeyboardInterrupt:
220+
pass
221+
return 0
222+
self.set_inputhook(inputhook_qt4)
223+
224+
self._current_gui = GUI_QT4
225225
app._in_event_loop = True
226226
self._apps[GUI_QT4] = app
227227
return app

0 commit comments

Comments
 (0)