@@ -186,48 +186,45 @@ def _maybe_allow_interrupt(qapp):
186186 that a non-python handler was installed, i.e. in Julia, and not SIG_IGN
187187 which means we should ignore the interrupts.
188188 """
189+
189190 old_sigint_handler = signal .getsignal (signal .SIGINT )
190- handler_args = None
191- skip = False
192191 if old_sigint_handler in (None , signal .SIG_IGN , signal .SIG_DFL ):
193- skip = True
194- else :
195- wsock , rsock = socket .socketpair ()
196- wsock .setblocking (False )
197- old_wakeup_fd = signal .set_wakeup_fd (wsock .fileno ())
198- sn = QtCore .QSocketNotifier (
199- rsock .fileno (), QtCore .QSocketNotifier .Type .Read
200- )
192+ yield
193+ return
194+
195+ handler_args = None
196+ wsock , rsock = socket .socketpair ()
197+ wsock .setblocking (False )
198+ rsock .setblocking (False )
199+ old_wakeup_fd = signal .set_wakeup_fd (wsock .fileno ())
200+ sn = QtCore .QSocketNotifier (rsock .fileno (), QtCore .QSocketNotifier .Type .Read )
201+
202+ # We do not actually care about this value other than running some Python code to
203+ # ensure that the interpreter has a chance to handle the signal in Python land. We
204+ # also need to drain the socket because it will be written to as part of the wakeup!
205+ # There are some cases where this may fire too soon / more than once on Windows so
206+ # we should be forgiving about reading an empty socket.
207+ # Clear the socket to re-arm the notifier.
208+ @sn .activated .connect
209+ def _may_clear_sock (* args ):
210+ try :
211+ rsock .recv (1 )
212+ except BlockingIOError :
213+ pass
214+
215+ def handle (* args ):
216+ nonlocal handler_args
217+ handler_args = args
218+ qapp .quit ()
201219
202- # We do not actually care about this value other than running some
203- # Python code to ensure that the interpreter has a chance to handle the
204- # signal in Python land. We also need to drain the socket because it
205- # will be written to as part of the wakeup! There are some cases where
206- # this may fire too soon / more than once on Windows so we should be
207- # forgiving about reading an empty socket.
208- rsock .setblocking (False )
209- # Clear the socket to re-arm the notifier.
210- @sn .activated .connect
211- def _may_clear_sock (* args ):
212- try :
213- rsock .recv (1 )
214- except BlockingIOError :
215- pass
216-
217- def handle (* args ):
218- nonlocal handler_args
219- handler_args = args
220- qapp .quit ()
221-
222- signal .signal (signal .SIGINT , handle )
220+ signal .signal (signal .SIGINT , handle )
223221 try :
224222 yield
225223 finally :
226- if not skip :
227- wsock .close ()
228- rsock .close ()
229- sn .setEnabled (False )
230- signal .set_wakeup_fd (old_wakeup_fd )
231- signal .signal (signal .SIGINT , old_sigint_handler )
232- if handler_args is not None :
233- old_sigint_handler (* handler_args )
224+ wsock .close ()
225+ rsock .close ()
226+ sn .setEnabled (False )
227+ signal .set_wakeup_fd (old_wakeup_fd )
228+ signal .signal (signal .SIGINT , old_sigint_handler )
229+ if handler_args is not None :
230+ old_sigint_handler (* handler_args )
0 commit comments