Skip to content

gh-154730: Add timeout in PyREPL to distinguish between bare Esc and multibyte escape sequence - #154731

Open
tanloong wants to merge 11 commits into
python:mainfrom
tanloong:esc-timeout
Open

tanloong wants to merge 11 commits into
python:mainfrom
tanloong:esc-timeout

Conversation

@tanloong

@tanloong tanloong commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

This adds a timeout to disambiguate bare Esc from the start of a longer escape sequence.

  1. Add pending() to BaseEventQueue: returns True when the queue has consumed a prefix (e.g., \x1b) but is still waiting for more bytes.
  2. Add flush() to BaseEventQueue: finalises a pending prefix as a standalone key (emits \x1b and re-feeds any remaining buffered bytes).
  3. Add esc_timeout parameter to BaseEventQueue (default 0.1 seconds; override via PYREPL_ESC_TIMEOUT env var).

In both unix_console.get_event() and windows_console.get_event(), when event_queue.pending() is True, the next read blocks only for esc_timeout seconds. If the timeout expires, call flush() and deliver the standalone Esc event.

@tanloong tanloong changed the title gh-154730: Add timeout to distinguish between bare Esc and multibyte escape sequence gh-154730: Add timeout in PyREPL to distinguish between bare Esc and multibyte escape sequence Jul 26, 2026
@tanloong
tanloong marked this pull request as ready for review July 28, 2026 16:59
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 29, 2026
Comment thread Lib/_pyrepl/base_eventqueue.py Outdated
self.encoding = encoding
self.events: deque[Event] = deque()
self.buf = bytearray()
default = float(os.environ.get("PYREPL_ESC_TIMEOUT", ESC_TIMEOUT_DEFAULT))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PYREPL_ESC_TIMEOUT=bad disables PyREPL at startup. Can we use the default when the value is invalid?

@tanloong tanloong Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added fallback when PYREPL_ESC_TIMEOUT is non-numeric / negative / NaN / infinite.

@bedevere-app

bedevere-app Bot commented Nov 26, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Revert "Ignore mypy error: "bytearray" has no attribute "take_bytes""

This reverts commit 277ada7.
@tanloong

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again

@bedevere-app

bedevere-app Bot commented Nov 26, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@pablogsal: please review the changes made to this pull request.

@bedevere-app
bedevere-app Bot requested a review from pablogsal November 26, 2026 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting change review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants