|
153 | 153 | from IPython.terminal.ipapp import TerminalIPythonApp, IPAppCrashHandler |
154 | 154 | from IPython.core.crashhandler import CrashHandler |
155 | 155 |
|
| 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 | + |
156 | 168 |
|
157 | 169 | # TODO: This global variable _do_preparse should be associated with an |
158 | 170 | # IPython InteractiveShell as opposed to a global variable in this |
@@ -287,6 +299,18 @@ def init_display_formatter(self): |
287 | 299 | backend = BackendIPythonCommandline() |
288 | 300 | backend.get_display_manager().switch_backend(backend, shell=self) |
289 | 301 |
|
| 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 |
290 | 314 |
|
291 | 315 | class SageTestShell(SageShellOverride, TerminalInteractiveShell): |
292 | 316 | """ |
|
0 commit comments