Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve REPL KI #3030

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/trio/_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,42 @@
# We always use sys.excepthook, unlike other implementations.
# This means that overriding self.write also does nothing to tbs.
sys.excepthook(sys.last_type, sys.last_value, sys.last_traceback)
# clear any residual KI
trio.from_thread.run(trio.lowlevel.checkpoint_if_cancelled)
# trio.from_thread.check_cancelled() has too long of a memory

if sys.platform == "win32":

def raw_input(self, prompt: str = "") -> str:
try:
return input(prompt)
except EOFError:

Check warning on line 65 in src/trio/_repl.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_repl.py#L65

Added line #L65 was not covered by tests
# check if trio has a pending KI
trio.from_thread.run(trio.lowlevel.checkpoint_if_cancelled)
raise

Check warning on line 68 in src/trio/_repl.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_repl.py#L67-L68

Added lines #L67 - L68 were not covered by tests

else:

def raw_input(self, prompt: str = "") -> str:
import fcntl
import termios
from signal import SIGINT, signal

interrupted = False

def handler(sig: int, frame: types.FrameType | None) -> None:
nonlocal interrupted
interrupted = True

Check warning on line 81 in src/trio/_repl.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_repl.py#L81

Added line #L81 was not covered by tests
# Fake up a newline char as if user had typed it at terminal
fcntl.ioctl(sys.stdin, termios.TIOCSTI, b"\n")

Check warning on line 83 in src/trio/_repl.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_repl.py#L83

Added line #L83 was not covered by tests

prev_handler = trio.from_thread.run_sync(signal, SIGINT, handler)
try:
return input(prompt)
finally:
trio.from_thread.run_sync(signal, SIGINT, prev_handler)
if interrupted:
raise KeyboardInterrupt

Check warning on line 91 in src/trio/_repl.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_repl.py#L91

Added line #L91 was not covered by tests


async def run_repl(console: TrioInteractiveConsole) -> None:
Expand Down
Loading