@@ -36,6 +36,7 @@ def __init__(self, repl_locals: dict[str, object] | None = None) -> None:
36
36
super ().__init__ (locals = repl_locals )
37
37
self .token : trio .lowlevel .TrioToken | None = None
38
38
self .compile .compiler .flags |= ast .PyCF_ALLOW_TOP_LEVEL_AWAIT
39
+ self .interrupted = False
39
40
40
41
def runcode (self , code : CodeType ) -> None :
41
42
# https://github.com/python/typeshed/issues/13768
@@ -84,14 +85,13 @@ def raw_input(self, prompt: str = "") -> str:
84
85
def raw_input (self , prompt : str = "" ) -> str :
85
86
from signal import SIGINT , signal
86
87
87
- interrupted = False
88
+ assert not self . interrupted
88
89
89
90
def install_handler () -> (
90
91
Callable [[int , FrameType | None ], None ] | int | None
91
92
):
92
93
def handler (sig : int , frame : FrameType | None ) -> None :
93
- nonlocal interrupted
94
- interrupted = True
94
+ self .interrupted = True
95
95
token .run_sync_soon (terminal_newline , idempotent = True )
96
96
97
97
token = trio .lowlevel .current_trio_token ()
@@ -103,9 +103,17 @@ def handler(sig: int, frame: FrameType | None) -> None:
103
103
return input (prompt )
104
104
finally :
105
105
trio .from_thread .run_sync (signal , SIGINT , prev_handler )
106
- if interrupted :
106
+ if self . interrupted :
107
107
raise KeyboardInterrupt
108
108
109
+ def write (self , output : str ) -> None :
110
+ if self .interrupted :
111
+ assert output == "\n KeyboardInterrupt\n "
112
+ sys .stderr .write (output [1 :])
113
+ self .interrupted = False
114
+ else :
115
+ sys .stderr .write (output )
116
+
109
117
110
118
async def run_repl (console : TrioInteractiveConsole ) -> None :
111
119
banner = (
0 commit comments