Show More
@@ -16,11 +16,23 b' Author: Christian Boos' | |||||
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
|
19 | import os | |||
|
20 | import signal | |||
|
21 | import time | |||
|
22 | import threading | |||
|
23 | ||||
19 | from IPython.core.interactiveshell import InteractiveShell |
|
24 | from IPython.core.interactiveshell import InteractiveShell | |
20 | from IPython.external.qt_for_kernel import QtCore, QtGui |
|
25 | from IPython.external.qt_for_kernel import QtCore, QtGui | |
21 | from IPython.lib.inputhook import allow_CTRL_C, ignore_CTRL_C, stdin_ready |
|
26 | from IPython.lib.inputhook import allow_CTRL_C, ignore_CTRL_C, stdin_ready | |
22 |
|
27 | |||
23 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
|
29 | # Module Globals | |||
|
30 | #----------------------------------------------------------------------------- | |||
|
31 | ||||
|
32 | got_kbdint = False | |||
|
33 | sigint_timer = None | |||
|
34 | ||||
|
35 | #----------------------------------------------------------------------------- | |||
24 | # Code |
|
36 | # Code | |
25 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
26 |
|
38 | |||
@@ -64,8 +76,6 b' def create_inputhook_qt4(mgr, app=None):' | |||||
64 | # Otherwise create the inputhook_qt4/preprompthook_qt4 pair of |
|
76 | # Otherwise create the inputhook_qt4/preprompthook_qt4 pair of | |
65 | # hooks (they both share the got_kbdint flag) |
|
77 | # hooks (they both share the got_kbdint flag) | |
66 |
|
78 | |||
67 | got_kbdint = [False] |
|
|||
68 |
|
||||
69 | def inputhook_qt4(): |
|
79 | def inputhook_qt4(): | |
70 | """PyOS_InputHook python hook for Qt4. |
|
80 | """PyOS_InputHook python hook for Qt4. | |
71 |
|
81 | |||
@@ -114,10 +124,31 b' def create_inputhook_qt4(mgr, app=None):' | |||||
114 | event_loop.exec_() |
|
124 | event_loop.exec_() | |
115 | timer.stop() |
|
125 | timer.stop() | |
116 | except KeyboardInterrupt: |
|
126 | except KeyboardInterrupt: | |
|
127 | global got_kbdint, sigint_timer | |||
|
128 | ||||
117 | ignore_CTRL_C() |
|
129 | ignore_CTRL_C() | |
118 |
got_kbdint |
|
130 | got_kbdint = True | |
119 | print("\nKeyboardInterrupt - Ctrl-C again for new prompt") |
|
|||
120 | mgr.clear_inputhook() |
|
131 | mgr.clear_inputhook() | |
|
132 | ||||
|
133 | # This generates a second SIGINT so the user doesn't have to | |||
|
134 | # press CTRL+C twice to get a clean prompt. | |||
|
135 | # | |||
|
136 | # Since we can't catch the resulting KeyboardInterrupt here | |||
|
137 | # (because this is a ctypes callback), we use a timer to | |||
|
138 | # generate the SIGINT after we leave this callback. | |||
|
139 | # | |||
|
140 | # Unfortunately this doesn't work on Windows (SIGINT kills | |||
|
141 | # Python and CTRL_C_EVENT doesn't work). | |||
|
142 | if(os.name == 'posix'): | |||
|
143 | pid = os.getpid() | |||
|
144 | if(not sigint_timer): | |||
|
145 | sigint_timer = threading.Timer(.01, os.kill, | |||
|
146 | args=[pid, signal.SIGINT] ) | |||
|
147 | sigint_timer.start() | |||
|
148 | else: | |||
|
149 | print("\nKeyboardInterrupt - Ctrl-C again for new prompt") | |||
|
150 | ||||
|
151 | ||||
121 | except: # NO exceptions are allowed to escape from a ctypes callback |
|
152 | except: # NO exceptions are allowed to escape from a ctypes callback | |
122 | ignore_CTRL_C() |
|
153 | ignore_CTRL_C() | |
123 | from traceback import print_exc |
|
154 | from traceback import print_exc | |
@@ -134,9 +165,15 b' def create_inputhook_qt4(mgr, app=None):' | |||||
134 | (in case the latter was temporarily deactivated after a |
|
165 | (in case the latter was temporarily deactivated after a | |
135 | CTRL+C) |
|
166 | CTRL+C) | |
136 | """ |
|
167 | """ | |
137 | if got_kbdint[0]: |
|
168 | global got_kbdint, sigint_timer | |
|
169 | ||||
|
170 | if(sigint_timer): | |||
|
171 | sigint_timer.cancel() | |||
|
172 | sigint_timer = None | |||
|
173 | ||||
|
174 | if got_kbdint: | |||
138 | mgr.set_inputhook(inputhook_qt4) |
|
175 | mgr.set_inputhook(inputhook_qt4) | |
139 |
got_kbdint |
|
176 | got_kbdint = False | |
140 |
|
177 | |||
141 | ip._inputhook_qt4 = inputhook_qt4 |
|
178 | ip._inputhook_qt4 = inputhook_qt4 | |
142 | ip.set_hook('pre_prompt_hook', preprompthook_qt4) |
|
179 | ip.set_hook('pre_prompt_hook', preprompthook_qt4) |
General Comments 0
You need to be logged in to leave comments.
Login now