Skip to content

Conversation

@ambv
Copy link
Contributor

@ambvambv commented Sep 6, 2024

This switches the main pyrepl event loop to always be non-blocking so that it can listen to incoming interruptions from other threads.

This also resolves invalid display of exceptions from other threads (gh-123178).

This also fixes freezes with pasting and an active input hook.

This switches the main pyrepl event loop to always be non-blocking so that it can listen to incoming interruptions from other threads. This also resolves invalid display of exceptions from other threads (pythongh-123178).
@@ -0,0 +1,2 @@
asyncio REPL is now again properly recognizing KeyboardInterrupts. Display
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
asyncio REPL is now again properly recognizing KeyboardInterrupts. Display
asyncio REPL is now again properly recognizing :exc:`KeyboardInterrupt`. Display

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not the same word. Let's do stuff like that in a subsequent PR please, so we don't waste time on CI now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. The upcoming release is more important now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FTR, to put the s, you would do :exc:`KeyboardInterrupt`\s

@ambv
Copy link
ContributorAuthor

ambv commented Sep 6, 2024

Screenshot 2024-09-06 at 17 48 06


handler=ExceptHookHandler()
reader.threading_hook=handler
threading.excepthook=handler.exception
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This except hook essentially queues tracebacks for nicer display whenever the REPL is actually ready to do it.


defrun_hooks(self) ->None:
threading_hook=self.threading_hook
ifthreading_hookisNoneand'threading'insys.modules:
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This automatic installation is such that when people create threads in the REPL directly or indirectly (via libraries), they get correctly formatted tracebacks.

It just so happens that asyncio REPL uses threads so it triggers this condition, too.

Comment on lines +423 to +427
return (
notself.event_queue.empty()
orself.more_in_buffer()
orbool(self.pollob.poll(timeout))
)
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous behavior caused pyrepl to lose reads in non-blocking mode (i.e. with an input hook) if more than 1 character was read previously to the input_buffer (only 1 character is turned into an event at a time). This made tests flaky. Now they pass every time.

from_pyrepl.simple_interactimport_get_reader
r=_get_reader()
ifr.threading_hookisnotNone:
r.threading_hook.add("") # type: ignore
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how we add a KeyboardInterrupt to the other thread. We don't want a traceback, and the "KeyboardInterrupt" message will already appear.

ifmsvcrt.kbhit(): # type: ignore[attr-defined]
returnTrue
iftimeoutandtime.time() -start_time>timeout:
iftimeoutandtime.time() -start_time>timeout/1000:
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol



definstall_threading_hook(reader: Reader) ->None:
importthreading
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put this import here so that pyrepl will continue to work in thread-free environments like iOS and the browser.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: iOS and Android do support threads, but not subprocesses. The only platform that doesn't support threads is WASM.

Copy link
Contributor

@willingcwillingc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great @ambv. Thanks for making the PR easy to review with explanatory comments. So excited for the new REPL @ambv and @pablogsal. 🎉

@ambvambv merged commit 033510e into python:mainSep 6, 2024
@ambvambv added the needs backport to 3.13 bugs and security fixes label Sep 6, 2024
@miss-islington-app
Copy link

Thanks @ambv for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 6, 2024
…123795) This switches the main pyrepl event loop to always be non-blocking so that it can listen to incoming interruptions from other threads. This also resolves invalid display of exceptions from other threads (pythongh-123178). This also fixes freezes with pasting and an active input hook. (cherry picked from commit 033510e) Co-authored-by: Łukasz Langa <lukasz@langa.pl>
@bedevere-app
Copy link

GH-123799 is a backport of this pull request to the 3.13 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.13 bugs and security fixes label Sep 6, 2024
ambv added a commit that referenced this pull request Sep 6, 2024
… (#123799) This switches the main pyrepl event loop to always be non-blocking so that it can listen to incoming interruptions from other threads. This also resolves invalid display of exceptions from other threads (gh-123178). This also fixes freezes with pasting and an active input hook. (cherry picked from commit 033510e) Co-authored-by: Łukasz Langa <lukasz@langa.pl>
@ambvambv deleted the gh-120221 branch October 7, 2024 18:30
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@ambv@mhsmith@willingc@picnixz@pablogsal@Eclips4