Skip to content
Open
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
36 changes: 20 additions & 16 deletions src/prompt_toolkit/application/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -1600,27 +1600,31 @@ def _restore_sigint_from_ctypes() -> Generator[None, None, None]:
try:
from ctypes import c_int, c_void_p, pythonapi
except ImportError:
# Any of the above imports don't exist? Don't do anything here.
yield
return

# PyOS_sighandler_t PyOS_getsig(int i)
pythonapi.PyOS_getsig.restype = c_void_p
pythonapi.PyOS_getsig.argtypes = (c_int,)

# PyOS_sighandler_t PyOS_setsig(int i, PyOS_sighandler_t h)
pythonapi.PyOS_setsig.restype = c_void_p
pythonapi.PyOS_setsig.argtypes = (
c_int,
c_void_p,
)
have_ctypes_signal = False
else:
# GraalPy has the functions, but they don't work
have_ctypes_signal = sys.implementation.name != 'graalpy'

if have_ctypes_signal:
# PyOS_sighandler_t PyOS_getsig(int i)
pythonapi.PyOS_getsig.restype = c_void_p
pythonapi.PyOS_getsig.argtypes = (c_int,)

# PyOS_sighandler_t PyOS_setsig(int i, PyOS_sighandler_t h)
pythonapi.PyOS_setsig.restype = c_void_p
pythonapi.PyOS_setsig.argtypes = (
c_int,
c_void_p,
)

sigint = signal.getsignal(signal.SIGINT)
sigint_os = pythonapi.PyOS_getsig(signal.SIGINT)
if have_ctypes_signal:
sigint_os = pythonapi.PyOS_getsig(signal.SIGINT)

try:
yield
finally:
if sigint is not None:
signal.signal(signal.SIGINT, sigint)
pythonapi.PyOS_setsig(signal.SIGINT, sigint_os)
if have_ctypes_signal:
pythonapi.PyOS_setsig(signal.SIGINT, sigint_os)