Skip to content

gh-122622: Add PyREPL simple_interact.statement_submitted_hook - #157241

Open
edvilme wants to merge 29 commits into
python:mainfrom
edvilme:pyrepl-hook
Open

edvilme wants to merge 29 commits into
python:mainfrom
edvilme:pyrepl-hook

Conversation

@edvilme

@edvilme edvilme commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Supersedes #144724

Adds a statement_submitted_hook hook to _pyrepl.simple_interact that gets triggered after the user has submitted a statement (not necessarily a line) but before it is executed.

Rationale

This should be configurable by external tools such as IDEs who may want to perform an action before a statement is executed, such as displaying custom escape sequences for terminal integration, handling custom logging logic, performing analysis, etc.
This hook should allow said tools to perform an action.

Example (Timer)

>>> import _pyrepl
>>> import sys
>>> import time
>>> class PS1:
...     start_time = None
...     def reset_start_time(self):
...         self.start_time = time.perf_counter()
...     def __init__(self):
...         _pyrepl.simple_interact.statement_submitted_hook = lambda x: self.reset_start_time()
...     def __str__(self):
...         end_time = time.perf_counter()
...         time_delta = end_time - self.start_time
...         return f"# Execution took {time_delta}s \n>>> "
...
>>> sys.ps1 = PS1()
>>> 1 + 1 
2
# Execution took 0.0021207999670878053s
>>> [i for i in range(100)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
# Execution took 0.003159299958497286s

Example (VSCode)

VS Code uses custom escape sequences to indicate when a command execution has started and finished. Here, the hook could be used to print an invisible escape sequence to the terminal. (Note that the example uses ### EXEC ### to represent it visually, but escape sequences are invisible)

image

With error reporting

image

AI Usage

AI was used to help with merge conflicts

@brettcannon
brettcannon self-requested a review September 17, 2026 21:15
Comment thread Lib/_pyrepl/__init__.py Resolved
Comment thread Lib/_pyrepl/simple_interact.py Resolved
Comment thread Lib/_pyrepl/simple_interact.py Resolved
Comment thread Lib/_pyrepl/simple_interact.py Resolved
Comment thread Lib/_pyrepl/simple_interact.py Resolved
@bedevere-app

bedevere-app Bot commented Sep 17, 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.

Comment thread Lib/_pyrepl/simple_interact.py Resolved
@edvilme edvilme changed the title gh-122622: Add PyREPL pre_execution_hook gh-122622: Add PyREPL simple_interact.statement_submitted_hook Sep 19, 2026
@edvilme

edvilme commented Sep 19, 2026

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again

@bedevere-app

bedevere-app Bot commented Sep 19, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

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

@bedevere-app
bedevere-app Bot requested a review from brettcannon September 19, 2026 01:20
@brettcannon brettcannon added type-bug An unexpected behavior, bug, or error needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Oct 1, 2026
@brettcannon

Copy link
Copy Markdown
Member

@pablogsal @ambv feel free to review, but if you're too busy I will just plan to merge in about a week.

@johnslavik johnslavik left a comment •

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.

As this is (primarily) a _pyrepl convenience for VS Code, I'd try to make this PR smaller.

Comment thread Lib/test/test_pyrepl/test_interact.py Resolved
Comment thread Lib/test/test_pyrepl/test_interact.py Resolved
Comment thread Lib/_pyrepl/simple_interact.py Resolved
Comment thread Lib/_pyrepl/simple_interact.py Resolved
Comment thread Lib/_pyrepl/simple_interact.py Resolved
Comment thread Lib/_pyrepl/simple_interact.py Resolved
@edvilme

edvilme commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Hello, when you have the chance, can I please get another review here? @pablogsal @ambv @johnslavik

@johnslavik

Copy link
Copy Markdown
Member

@edvilme Could you please use your own words when replying? Thanks

Comment thread Lib/_pyrepl/simple_interact.py Resolved
Comment thread Lib/_pyrepl/simple_interact.py Outdated
Comment on lines +161 to +162
console.write("Error in statement_submitted_hook:\n")
console._excepthook(type(exc), exc, exc.__traceback__)

@johnslavik johnslavik Nov 17, 2026 •

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.

_excepthook writes to console directly, but it would make sense to me if we reused sys.excepthook like _showtraceback, just without writing to sys attributes. Maybe add a param to showtraceback and _showtraceback for REPL-specific errors to just avoid writing to sys attributes but go through the traceback display pipeline inside _showtraceback? Or simply move the pipeline to _excepthook, since no call sites (other than _showtraceback) use _excepthook directly

WDYT @pablogsal?

johnslavik added a commit to johnslavik/cpython that referenced this pull request Nov 21, 2026
I've included `test_keyboard_interrupt_preserves_last_exception` as an
invariant test, inspired by a catch in pythonGH-157241.
Comment thread Lib/_pyrepl/simple_interact.py Resolved
@edvilme
edvilme requested a review from johnslavik November 24, 2026 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants