Show More
@@ -42,31 +42,44 b' def create_inputhook_qt4(mgr, app=None):' | |||||
42 |
|
42 | |||
43 | Notes |
|
43 | Notes | |
44 | ----- |
|
44 | ----- | |
|
45 | We use a custom input hook instead of PyQt4's default one, as it | |||
|
46 | interacts better with the readline packages (issue #481). | |||
|
47 | ||||
45 | The inputhook function works in tandem with a 'pre_prompt_hook' |
|
48 | The inputhook function works in tandem with a 'pre_prompt_hook' | |
46 | which automatically restores the hook as an inputhook in case the |
|
49 | which automatically restores the hook as an inputhook in case the | |
47 | latter has been temporarily disabled after having intercepted a |
|
50 | latter has been temporarily disabled after having intercepted a | |
48 | KeyboardInterrupt. |
|
51 | KeyboardInterrupt. | |
49 | """ |
|
52 | """ | |
|
53 | ||||
50 | if app is None: |
|
54 | if app is None: | |
51 | app = QtCore.QCoreApplication.instance() |
|
55 | app = QtCore.QCoreApplication.instance() | |
52 | if app is None: |
|
56 | if app is None: | |
53 | app = QtGui.QApplication([" "]) |
|
57 | app = QtGui.QApplication([" "]) | |
54 |
|
58 | |||
55 | # Always use a custom input hook instead of PyQt4's default |
|
59 | # Re-use previously created inputhook if any | |
56 | # one, as it interacts better with readline packages (issue |
|
60 | ip = ipapi.get() | |
57 | # #481). |
|
61 | if hasattr(ip, '_inputhook_qt4'): | |
|
62 | return app, ip._inputhook_qt4 | |||
58 |
|
63 | |||
59 | # Note that we can't let KeyboardInterrupt escape from that |
|
64 | # Otherwise create the inputhook_qt4/preprompthook_qt4 pair of | |
60 | # hook, as no exception can be raised from within a ctypes |
|
65 | # hooks (they both share the got_kbdint flag) | |
61 | # python callback. We need to make a compromise: a trapped |
|
|||
62 | # KeyboardInterrupt will temporarily disable the input hook |
|
|||
63 | # until we start over with a new prompt line with a second |
|
|||
64 | # CTRL+C. |
|
|||
65 |
|
66 | |||
66 | got_kbdint = [False] |
|
67 | got_kbdint = [False] | |
67 |
|
68 | |||
68 | def inputhook_qt4(): |
|
69 | def inputhook_qt4(): | |
|
70 | """PyOS_InputHook python hook for Qt4. | |||
|
71 | ||||
|
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 | |||
|
74 | loop. | |||
|
75 | ||||
|
76 | As a Python ctypes callback can't raise an exception, we catch | |||
|
77 | the KeyboardInterrupt and temporarily deactivate the hook, | |||
|
78 | which will let a *second* CTRL+C be processed normally and go | |||
|
79 | back to a clean prompt line. | |||
|
80 | """ | |||
69 | try: |
|
81 | try: | |
|
82 | app = QtCore.QCoreApplication.instance() | |||
70 | app.processEvents(QtCore.QEventLoop.AllEvents, 300) |
|
83 | app.processEvents(QtCore.QEventLoop.AllEvents, 300) | |
71 | if not stdin_ready(): |
|
84 | if not stdin_ready(): | |
72 | timer = QtCore.QTimer() |
|
85 | timer = QtCore.QTimer() | |
@@ -83,9 +96,16 b' def create_inputhook_qt4(mgr, app=None):' | |||||
83 | return 0 |
|
96 | return 0 | |
84 |
|
97 | |||
85 | def preprompthook_qt4(ishell): |
|
98 | def preprompthook_qt4(ishell): | |
|
99 | """'pre_prompt_hook' used to restore the Qt4 input hook | |||
|
100 | ||||
|
101 | (in case the latter was temporarily deactivated after a | |||
|
102 | CTRL+C) | |||
|
103 | """ | |||
86 | if got_kbdint[0]: |
|
104 | if got_kbdint[0]: | |
87 | mgr.set_inputhook(inputhook_qt4) |
|
105 | mgr.set_inputhook(inputhook_qt4) | |
88 | got_kbdint[0] = False |
|
106 | got_kbdint[0] = False | |
89 | ipapi.get().set_hook('pre_prompt_hook', preprompthook_qt4) |
|
107 | ||
|
108 | ip._inputhook_qt4 = inputhook_qt4 | |||
|
109 | ip.set_hook('pre_prompt_hook', preprompthook_qt4) | |||
90 |
|
110 | |||
91 | return app, inputhook_qt4 |
|
111 | return app, inputhook_qt4 |
General Comments 0
You need to be logged in to leave comments.
Login now