##// END OF EJS Templates
inputhook: improve CTRL+C handling with qt4...
Christian Boos -
Show More
@@ -196,18 +196,35 b' class InputHookManager(object):'
196 app = QtGui.QApplication(sys.argv)
196 app = QtGui.QApplication(sys.argv)
197 """
197 """
198 from IPython.external.qt_for_kernel import QtCore, QtGui
198 from IPython.external.qt_for_kernel import QtCore, QtGui
199 from IPython.core import ipapi
199
200
200 if app is None:
201 if app is None:
201 app = QtCore.QCoreApplication.instance()
202 app = QtCore.QCoreApplication.instance()
202 if app is None:
203 if app is None:
203 app = QtGui.QApplication([" "])
204 app = QtGui.QApplication([" "])
204
205
205 # Always use the following input hook instead of PyQt4's
206 # Always use a custom input hook instead of PyQt4's default
206 # default one, as it interacts better with readline packages
207 # one, as it interacts better with readline packages (issue
207 # (issue #481)
208 # #481).
209
210 # Note that we can't let KeyboardInterrupt escape from that
211 # hook, (no exception can't be raised from within a ctypes
212 # python callback). We need to make a compromise: a trapped
213 # KeyboardInterrupt will prevent the input hook to re-enter
214 # the exec loop, until we start over with a new prompt line.
215 # This means one needs a double CTRL+C to get back to the
216 # prompt.
217
218 got_kbdint = [False]
219
220 def preprompthook_qt4(self):
221 got_kbdint[0] = False
222 ipapi.get().set_hook('pre_prompt_hook', preprompthook_qt4)
208
223
209 def inputhook_qt4():
224 def inputhook_qt4():
210 try:
225 try:
226 if got_kbdint[0]:
227 return 0
211 app.processEvents(QtCore.QEventLoop.AllEvents, 300)
228 app.processEvents(QtCore.QEventLoop.AllEvents, 300)
212 if not stdin_ready():
229 if not stdin_ready():
213 timer = QtCore.QTimer()
230 timer = QtCore.QTimer()
@@ -217,7 +234,9 b' class InputHookManager(object):'
217 app.exec_()
234 app.exec_()
218 timer.stop()
235 timer.stop()
219 except KeyboardInterrupt:
236 except KeyboardInterrupt:
220 pass
237 got_kbdint[0] = True
238 print("\n(event loop interrupted - "
239 "hit CTRL+C again to clear the prompt)")
221 return 0
240 return 0
222 self.set_inputhook(inputhook_qt4)
241 self.set_inputhook(inputhook_qt4)
223
242
General Comments 0
You need to be logged in to leave comments. Login now