17
17
sleep ,
18
18
)
19
19
from contextlib import ExitStack , contextmanager
20
+ from ctypes import c_int , c_void_p , pythonapi
20
21
from subprocess import Popen
21
22
from traceback import format_tb
22
23
from typing import (
102
103
_SIGTSTP = getattr (signal , "SIGTSTP" , None )
103
104
104
105
106
+ # The following functions are part of the stable ABI since python 3.2
107
+ # See: https://docs.python.org/3/c-api/sys.html#c.PyOS_getsig
108
+
109
+ # PyOS_sighandler_t PyOS_getsig(int i)
110
+ pythonapi .PyOS_getsig .restype = c_void_p
111
+ pythonapi .PyOS_getsig .argtypes = (c_int ,)
112
+
113
+ # PyOS_sighandler_t PyOS_setsig(int i, PyOS_sighandler_t h)
114
+ pythonapi .PyOS_setsig .restype = c_void_p
115
+ pythonapi .PyOS_setsig .argtypes = (
116
+ c_int ,
117
+ c_void_p ,
118
+ )
119
+
120
+
105
121
class Application (Generic [_AppResult ]):
106
122
"""
107
123
The main Application class!
@@ -807,6 +823,10 @@ def set_is_running() -> Iterator[None]:
807
823
@contextmanager
808
824
def set_handle_sigint (loop : AbstractEventLoop ) -> Iterator [None ]:
809
825
if handle_sigint :
826
+ # save sigint handlers (python and os level)
827
+ # See: https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1576
828
+ sigint = signal .getsignal (signal .SIGINT )
829
+ sigint_os = pythonapi .PyOS_getsig (signal .SIGINT )
810
830
loop .add_signal_handler (
811
831
signal .SIGINT ,
812
832
lambda * _ : loop .call_soon_threadsafe (
@@ -817,6 +837,8 @@ def set_handle_sigint(loop: AbstractEventLoop) -> Iterator[None]:
817
837
yield
818
838
finally :
819
839
loop .remove_signal_handler (signal .SIGINT )
840
+ signal .signal (signal .SIGINT , sigint )
841
+ pythonapi .PyOS_setsig (signal .SIGINT , sigint_os )
820
842
else :
821
843
yield
822
844
0 commit comments