##// END OF EJS Templates
Workaround so only one CTRL-C is required for a new prompt in --gui=qt...
Siyu Zhang -
Show More
@@ -1,144 +1,158 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Qt4's inputhook support function
3 Qt4's inputhook support function
4
4
5 Author: Christian Boos
5 Author: Christian Boos
6 """
6 """
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2011 The IPython Development Team
9 # Copyright (C) 2011 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 from IPython.core.interactiveshell import InteractiveShell
19 from IPython.core.interactiveshell import InteractiveShell
20 from IPython.external.qt_for_kernel import QtCore, QtGui
20 from IPython.external.qt_for_kernel import QtCore, QtGui
21 from IPython.lib.inputhook import allow_CTRL_C, ignore_CTRL_C, stdin_ready
21 from IPython.lib.inputhook import allow_CTRL_C, ignore_CTRL_C, stdin_ready
22
22
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24 # Code
24 # Code
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
26
27 def create_inputhook_qt4(mgr, app=None):
27 def create_inputhook_qt4(mgr, app=None):
28 """Create an input hook for running the Qt4 application event loop.
28 """Create an input hook for running the Qt4 application event loop.
29
29
30 Parameters
30 Parameters
31 ----------
31 ----------
32 mgr : an InputHookManager
32 mgr : an InputHookManager
33
33
34 app : Qt Application, optional.
34 app : Qt Application, optional.
35 Running application to use. If not given, we probe Qt for an
35 Running application to use. If not given, we probe Qt for an
36 existing application object, and create a new one if none is found.
36 existing application object, and create a new one if none is found.
37
37
38 Returns
38 Returns
39 -------
39 -------
40 A pair consisting of a Qt Application (either the one given or the
40 A pair consisting of a Qt Application (either the one given or the
41 one found or created) and a inputhook.
41 one found or created) and a inputhook.
42
42
43 Notes
43 Notes
44 -----
44 -----
45 We use a custom input hook instead of PyQt4's default one, as it
45 We use a custom input hook instead of PyQt4's default one, as it
46 interacts better with the readline packages (issue #481).
46 interacts better with the readline packages (issue #481).
47
47
48 The inputhook function works in tandem with a 'pre_prompt_hook'
48 The inputhook function works in tandem with a 'pre_prompt_hook'
49 which automatically restores the hook as an inputhook in case the
49 which automatically restores the hook as an inputhook in case the
50 latter has been temporarily disabled after having intercepted a
50 latter has been temporarily disabled after having intercepted a
51 KeyboardInterrupt.
51 KeyboardInterrupt.
52 """
52 """
53
53
54 if app is None:
54 if app is None:
55 app = QtCore.QCoreApplication.instance()
55 app = QtCore.QCoreApplication.instance()
56 if app is None:
56 if app is None:
57 app = QtGui.QApplication([" "])
57 app = QtGui.QApplication([" "])
58
58
59 # Re-use previously created inputhook if any
59 # Re-use previously created inputhook if any
60 ip = InteractiveShell.instance()
60 ip = InteractiveShell.instance()
61 if hasattr(ip, '_inputhook_qt4'):
61 if hasattr(ip, '_inputhook_qt4'):
62 return app, ip._inputhook_qt4
62 return app, ip._inputhook_qt4
63
63
64 # Otherwise create the inputhook_qt4/preprompthook_qt4 pair of
64 # Otherwise create the inputhook_qt4/preprompthook_qt4 pair of
65 # hooks (they both share the got_kbdint flag)
65 # hooks (they both share the got_kbdint flag)
66
66
67 got_kbdint = [False]
67 got_kbdint = [False]
68
68
69 def inputhook_qt4():
69 def inputhook_qt4():
70 """PyOS_InputHook python hook for Qt4.
70 """PyOS_InputHook python hook for Qt4.
71
71
72 Process pending Qt events and if there's no pending keyboard
72 Process pending Qt events and if there's no pending keyboard
73 input, spend a short slice of time (50ms) running the Qt event
73 input, spend a short slice of time (50ms) running the Qt event
74 loop.
74 loop.
75
75
76 As a Python ctypes callback can't raise an exception, we catch
76 As a Python ctypes callback can't raise an exception, we catch
77 the KeyboardInterrupt and temporarily deactivate the hook,
77 the KeyboardInterrupt and temporarily deactivate the hook,
78 which will let a *second* CTRL+C be processed normally and go
78 which will let a *second* CTRL+C be processed normally and go
79 back to a clean prompt line.
79 back to a clean prompt line.
80 """
80 """
81 try:
81 try:
82 allow_CTRL_C()
82 allow_CTRL_C()
83 app = QtCore.QCoreApplication.instance()
83 app = QtCore.QCoreApplication.instance()
84 if not app: # shouldn't happen, but safer if it happens anyway...
84 if not app: # shouldn't happen, but safer if it happens anyway...
85 return 0
85 return 0
86 app.processEvents(QtCore.QEventLoop.AllEvents, 300)
86 app.processEvents(QtCore.QEventLoop.AllEvents, 300)
87 if not stdin_ready():
87 if not stdin_ready():
88 # Generally a program would run QCoreApplication::exec()
88 # Generally a program would run QCoreApplication::exec()
89 # from main() to enter and process the Qt event loop until
89 # from main() to enter and process the Qt event loop until
90 # quit() or exit() is called and the program terminates.
90 # quit() or exit() is called and the program terminates.
91 #
91 #
92 # For our input hook integration, we need to repeatedly
92 # For our input hook integration, we need to repeatedly
93 # enter and process the Qt event loop for only a short
93 # enter and process the Qt event loop for only a short
94 # amount of time (say 50ms) to ensure that Python stays
94 # amount of time (say 50ms) to ensure that Python stays
95 # responsive to other user inputs.
95 # responsive to other user inputs.
96 #
96 #
97 # A naive approach would be to repeatedly call
97 # A naive approach would be to repeatedly call
98 # QCoreApplication::exec(), using a timer to quit after a
98 # QCoreApplication::exec(), using a timer to quit after a
99 # short amount of time. Unfortunately, QCoreApplication
99 # short amount of time. Unfortunately, QCoreApplication
100 # emits an aboutToQuit signal before stopping, which has
100 # emits an aboutToQuit signal before stopping, which has
101 # the undesirable effect of closing all modal windows.
101 # the undesirable effect of closing all modal windows.
102 #
102 #
103 # To work around this problem, we instead create a
103 # To work around this problem, we instead create a
104 # QEventLoop and call QEventLoop::exec(). Other than
104 # QEventLoop and call QEventLoop::exec(). Other than
105 # setting some state variables which do not seem to be
105 # setting some state variables which do not seem to be
106 # used anywhere, the only thing QCoreApplication adds is
106 # used anywhere, the only thing QCoreApplication adds is
107 # the aboutToQuit signal which is precisely what we are
107 # the aboutToQuit signal which is precisely what we are
108 # trying to avoid.
108 # trying to avoid.
109 timer = QtCore.QTimer()
109 timer = QtCore.QTimer()
110 event_loop = QtCore.QEventLoop()
110 event_loop = QtCore.QEventLoop()
111 timer.timeout.connect(event_loop.quit)
111 timer.timeout.connect(event_loop.quit)
112 while not stdin_ready():
112 while not stdin_ready():
113 timer.start(50)
113 timer.start(50)
114 event_loop.exec_()
114 event_loop.exec_()
115 timer.stop()
115 timer.stop()
116 except KeyboardInterrupt:
116 except KeyboardInterrupt:
117 ignore_CTRL_C()
117 ignore_CTRL_C()
118 got_kbdint[0] = True
118 got_kbdint[0] = True
119 print("\nKeyboardInterrupt - Ctrl-C again for new prompt")
120 mgr.clear_inputhook()
119 mgr.clear_inputhook()
120 import os, signal, time, threading
121 if(os.name == 'posix'):
122 pid = os.getpid()
123 print("^C")
124 thread = threading.Thread(
125 target = lambda:
126 ( time.sleep(.1),
127 os.kill(pid, signal.SIGINT) )
128 )
129
130 thread.start()
131 else:
132 print("\nKeyboardInterrupt - Ctrl-C again for new prompt")
133
134
121 except: # NO exceptions are allowed to escape from a ctypes callback
135 except: # NO exceptions are allowed to escape from a ctypes callback
122 ignore_CTRL_C()
136 ignore_CTRL_C()
123 from traceback import print_exc
137 from traceback import print_exc
124 print_exc()
138 print_exc()
125 print("Got exception from inputhook_qt4, unregistering.")
139 print("Got exception from inputhook_qt4, unregistering.")
126 mgr.clear_inputhook()
140 mgr.clear_inputhook()
127 finally:
141 finally:
128 allow_CTRL_C()
142 allow_CTRL_C()
129 return 0
143 return 0
130
144
131 def preprompthook_qt4(ishell):
145 def preprompthook_qt4(ishell):
132 """'pre_prompt_hook' used to restore the Qt4 input hook
146 """'pre_prompt_hook' used to restore the Qt4 input hook
133
147
134 (in case the latter was temporarily deactivated after a
148 (in case the latter was temporarily deactivated after a
135 CTRL+C)
149 CTRL+C)
136 """
150 """
137 if got_kbdint[0]:
151 if got_kbdint[0]:
138 mgr.set_inputhook(inputhook_qt4)
152 mgr.set_inputhook(inputhook_qt4)
139 got_kbdint[0] = False
153 got_kbdint[0] = False
140
154
141 ip._inputhook_qt4 = inputhook_qt4
155 ip._inputhook_qt4 = inputhook_qt4
142 ip.set_hook('pre_prompt_hook', preprompthook_qt4)
156 ip.set_hook('pre_prompt_hook', preprompthook_qt4)
143
157
144 return app, inputhook_qt4
158 return app, inputhook_qt4
General Comments 0
You need to be logged in to leave comments. Login now