Skip to content

Commit afe1db0

Browse files
committed
fix for prompt-toolkit
1 parent 195e94f commit afe1db0

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/sage/repl/interpreter.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@
153153
from IPython.terminal.ipapp import TerminalIPythonApp, IPAppCrashHandler
154154
from IPython.core.crashhandler import CrashHandler
155155

156+
from ctypes import pythonapi, c_int, c_void_p
157+
# The following functions are part of the stable ABI since python 3.2
158+
# See: https://docs.python.org/3/c-api/sys.html#c.PyOS_getsig
159+
160+
# PyOS_sighandler_t PyOS_getsig(int i)
161+
pythonapi.PyOS_getsig.restype = c_void_p
162+
pythonapi.PyOS_getsig.argtypes = c_int,
163+
164+
# PyOS_sighandler_t PyOS_setsig(int i, PyOS_sighandler_t h)
165+
pythonapi.PyOS_setsig.restype = c_void_p
166+
pythonapi.PyOS_setsig.argtypes = c_int, c_void_p,
167+
156168

157169
# TODO: This global variable _do_preparse should be associated with an
158170
# IPython InteractiveShell as opposed to a global variable in this
@@ -287,6 +299,18 @@ def init_display_formatter(self):
287299
backend = BackendIPythonCommandline()
288300
backend.get_display_manager().switch_backend(backend, shell=self)
289301

302+
def prompt_for_code(self):
303+
# save sigint handlers (python and os level)
304+
# https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1576
305+
# https://github.com/sagemath/sage/issues/33428
306+
# https://github.com/sagemath/sage/pull/35251
307+
import signal
308+
sigint = signal.getsignal(signal.SIGINT)
309+
sigint_os = pythonapi.PyOS_getsig(signal.SIGINT)
310+
text = TerminalInteractiveShell.prompt_for_code(self)
311+
signal.signal(signal.SIGINT, sigint)
312+
pythonapi.PyOS_setsig(signal.SIGINT, sigint_os)
313+
return text
290314

291315
class SageTestShell(SageShellOverride, TerminalInteractiveShell):
292316
"""

0 commit comments

Comments
 (0)