MCPcopy Create free account
hub / github.com/fabioz/PyDev.Debugger / inputhook_qt4

Function inputhook_qt4

pydev_ipython/inputhookqt4.py:97–177  ·  view source on GitHub ↗

PyOS_InputHook python hook for Qt4. Process pending Qt events and if there's no pending keyboard input, spend a short slice of time (50ms) running the Qt event loop. As a Python ctypes callback can't raise an exception, we catch the KeyboardInterrupt and tem

()

Source from the content-addressed store, hash-verified

95 # hooks (they both share the got_kbdint flag)
96
97 def inputhook_qt4():
98 """PyOS_InputHook python hook for Qt4.
99
100 Process pending Qt events and if there's no pending keyboard
101 input, spend a short slice of time (50ms) running the Qt event
102 loop.
103
104 As a Python ctypes callback can't raise an exception, we catch
105 the KeyboardInterrupt and temporarily deactivate the hook,
106 which will let a *second* CTRL+C be processed normally and go
107 back to a clean prompt line.
108 """
109 try:
110 allow_CTRL_C()
111 app = QtCore.QCoreApplication.instance()
112 if not app: # shouldn't happen, but safer if it happens anyway...
113 return 0
114 app.processEvents(QtCore.QEventLoop.AllEvents, 300)
115 if not stdin_ready():
116 # Generally a program would run QCoreApplication::exec()
117 # from main() to enter and process the Qt event loop until
118 # quit() or exit() is called and the program terminates.
119 #
120 # For our input hook integration, we need to repeatedly
121 # enter and process the Qt event loop for only a short
122 # amount of time (say 50ms) to ensure that Python stays
123 # responsive to other user inputs.
124 #
125 # A naive approach would be to repeatedly call
126 # QCoreApplication::exec(), using a timer to quit after a
127 # short amount of time. Unfortunately, QCoreApplication
128 # emits an aboutToQuit signal before stopping, which has
129 # the undesirable effect of closing all modal windows.
130 #
131 # To work around this problem, we instead create a
132 # QEventLoop and call QEventLoop::exec(). Other than
133 # setting some state variables which do not seem to be
134 # used anywhere, the only thing QCoreApplication adds is
135 # the aboutToQuit signal which is precisely what we are
136 # trying to avoid.
137 timer = QtCore.QTimer()
138 event_loop = QtCore.QEventLoop()
139 timer.timeout.connect(event_loop.quit)
140 while not stdin_ready():
141 timer.start(50)
142 event_loop.exec_()
143 timer.stop()
144 except KeyboardInterrupt:
145 global got_kbdint, sigint_timer
146
147 ignore_CTRL_C()
148 got_kbdint = True
149 mgr.clear_inputhook()
150
151 # This generates a second SIGINT so the user doesn't have to
152 # press CTRL+C twice to get a clean prompt.
153 #
154 # Since we can't catch the resulting KeyboardInterrupt here

Callers

nothing calls this directly

Calls 8

allow_CTRL_CFunction · 0.90
ignore_CTRL_CFunction · 0.90
print_excFunction · 0.85
clear_inputhookMethod · 0.80
instanceMethod · 0.45
connectMethod · 0.45
startMethod · 0.45
stopMethod · 0.45

Tested by

no test coverage detected