Skip to content

Fix support for Ctrl-C on the macosx backend. #25966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion lib/matplotlib/backends/backend_macosx.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import signal
import socket

import matplotlib as mpl
from matplotlib import _api, cbook
Expand Down Expand Up @@ -166,7 +168,37 @@ def destroy(self):

@classmethod
def start_main_loop(cls):
_macosx.show()
# Set up a SIGINT handler to allow terminating a plot via CTRL-C.
# The logic is largely copied from qt_compat._maybe_allow_interrupt; see its
# docstring for details. Parts are implemented by wake_on_fd_write in ObjC.

old_sigint_handler = signal.getsignal(signal.SIGINT)
if old_sigint_handler in (None, signal.SIG_IGN, signal.SIG_DFL):
_macosx.show()
return

handler_args = None
wsock, rsock = socket.socketpair()
wsock.setblocking(False)
rsock.setblocking(False)
old_wakeup_fd = signal.set_wakeup_fd(wsock.fileno())
_macosx.wake_on_fd_write(rsock.fileno())

def handle(*args):
nonlocal handler_args
handler_args = args
_macosx.stop()

signal.signal(signal.SIGINT, handle)
try:
_macosx.show()
finally:
wsock.close()
rsock.close()
signal.set_wakeup_fd(old_wakeup_fd)
signal.signal(signal.SIGINT, old_sigint_handler)
if handler_args is not None:
old_sigint_handler(*handler_args)

def show(self):
if not self._shown:
Expand Down
75 changes: 36 additions & 39 deletions lib/matplotlib/backends/qt_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,48 +186,45 @@ def _maybe_allow_interrupt(qapp):
that a non-python handler was installed, i.e. in Julia, and not SIG_IGN
which means we should ignore the interrupts.
"""

old_sigint_handler = signal.getsignal(signal.SIGINT)
handler_args = None
skip = False
if old_sigint_handler in (None, signal.SIG_IGN, signal.SIG_DFL):
skip = True
else:
wsock, rsock = socket.socketpair()
wsock.setblocking(False)
old_wakeup_fd = signal.set_wakeup_fd(wsock.fileno())
sn = QtCore.QSocketNotifier(
rsock.fileno(), QtCore.QSocketNotifier.Type.Read
)
yield
return

handler_args = None
wsock, rsock = socket.socketpair()
wsock.setblocking(False)
rsock.setblocking(False)
old_wakeup_fd = signal.set_wakeup_fd(wsock.fileno())
sn = QtCore.QSocketNotifier(rsock.fileno(), QtCore.QSocketNotifier.Type.Read)

# We do not actually care about this value other than running some Python code to
# ensure that the interpreter has a chance to handle the signal in Python land. We
# also need to drain the socket because it will be written to as part of the wakeup!
# There are some cases where this may fire too soon / more than once on Windows so
# we should be forgiving about reading an empty socket.
# Clear the socket to re-arm the notifier.
@sn.activated.connect
def _may_clear_sock(*args):
try:
rsock.recv(1)
except BlockingIOError:
pass

def handle(*args):
nonlocal handler_args
handler_args = args
qapp.quit()

# We do not actually care about this value other than running some
# Python code to ensure that the interpreter has a chance to handle the
# signal in Python land. We also need to drain the socket because it
# will be written to as part of the wakeup! There are some cases where
# this may fire too soon / more than once on Windows so we should be
# forgiving about reading an empty socket.
rsock.setblocking(False)
# Clear the socket to re-arm the notifier.
@sn.activated.connect
def _may_clear_sock(*args):
try:
rsock.recv(1)
except BlockingIOError:
pass

def handle(*args):
nonlocal handler_args
handler_args = args
qapp.quit()

signal.signal(signal.SIGINT, handle)
signal.signal(signal.SIGINT, handle)
try:
yield
finally:
if not skip:
wsock.close()
rsock.close()
sn.setEnabled(False)
signal.set_wakeup_fd(old_wakeup_fd)
signal.signal(signal.SIGINT, old_sigint_handler)
if handler_args is not None:
old_sigint_handler(*handler_args)
wsock.close()
rsock.close()
sn.setEnabled(False)
signal.set_wakeup_fd(old_wakeup_fd)
signal.signal(signal.SIGINT, old_sigint_handler)
if handler_args is not None:
old_sigint_handler(*handler_args)
Loading