Skip to content

Commit 301f361

Browse files
committed
Don't add extra newlines on KI
1 parent 611a89d commit 301f361

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/trio/_repl.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def __init__(self, repl_locals: dict[str, object] | None = None) -> None:
3636
super().__init__(locals=repl_locals)
3737
self.token: trio.lowlevel.TrioToken | None = None
3838
self.compile.compiler.flags |= ast.PyCF_ALLOW_TOP_LEVEL_AWAIT
39+
self.interrupted = False
3940

4041
def runcode(self, code: CodeType) -> None:
4142
# https://github.com/python/typeshed/issues/13768
@@ -84,14 +85,13 @@ def raw_input(self, prompt: str = "") -> str:
8485
def raw_input(self, prompt: str = "") -> str:
8586
from signal import SIGINT, signal
8687

87-
interrupted = False
88+
assert not self.interrupted
8889

8990
def install_handler() -> (
9091
Callable[[int, FrameType | None], None] | int | None
9192
):
9293
def handler(sig: int, frame: FrameType | None) -> None:
93-
nonlocal interrupted
94-
interrupted = True
94+
self.interrupted = True
9595
token.run_sync_soon(terminal_newline, idempotent=True)
9696

9797
token = trio.lowlevel.current_trio_token()
@@ -103,9 +103,17 @@ def handler(sig: int, frame: FrameType | None) -> None:
103103
return input(prompt)
104104
finally:
105105
trio.from_thread.run_sync(signal, SIGINT, prev_handler)
106-
if interrupted:
106+
if self.interrupted:
107107
raise KeyboardInterrupt
108108

109+
def write(self, output: str) -> None:
110+
if self.interrupted:
111+
assert output == "\nKeyboardInterrupt\n"
112+
sys.stderr.write(output[1:])
113+
self.interrupted = False
114+
else:
115+
sys.stderr.write(output)
116+
109117

110118
async def run_repl(console: TrioInteractiveConsole) -> None:
111119
banner = (

0 commit comments

Comments
 (0)