-
-
Notifications
You must be signed in to change notification settings - Fork 362
Fix Ctrl-C handling in REPL #3306
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
base: main
Are you sure you want to change the base?
Conversation
Python's readline module is unable to handle signals when it's called not on the main thread, and it's blocking so we can't call it from the same thread as Trio. Get rid of the additional thread and handle input ourselves.
for more information, see https://pre-commit.ci
This is extremely rough, probably misbehaves on Windows, and is probably incomplete in some interesting way. I think it also misbehaves with Python 3.14. All I can say is it mostly works on my machine (Ubuntu 24.04, system Python). I'm mostly posting here for thoughts on the general approach of calling readline via ctypes + inspiration if someone else wants to run with this and finish it. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3306 +/- ##
====================================================
- Coverage 100.00000% 99.75714% -0.24286%
====================================================
Files 127 127
Lines 19274 19353 +79
Branches 1304 1314 +10
====================================================
+ Hits 19274 19306 +32
- Misses 0 41 +41
- Partials 0 6 +6
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this is too much poking at readline internals (the original had too much poking into OS internals).
I think the much better solution is to use that pyrepl reimplemented readline (at least then we're not using ctypes!!), so we can support CPython 3.12+ and (at the very least) PyPy 3.10+. In fact we should probably just error on earlier versions for the REPL since supporting them is very hard... I'll leave this open until we have something working for that though.
self.rl.rl_callback_handler_install(prompt.encode(), callback) | ||
while line == b"": | ||
await trio.lowlevel.wait_readable(0) | ||
self.rl.rl_callback_read_char() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So just to understand I'm reading this correctly, this rl_callback_read_char
would fail a ctrl+c check, except it's guaranteed to have a character as stdin is readable?
Python's readline module is unable to handle signals when it's called not on the main thread, and it's blocking so we can't call it from the same thread as Trio. Get rid of the additional thread and handle input ourselves.