gh-122622: Add PyREPL pre_execution_hook - #157241
Open
edvilme wants to merge 5 commits into
Open
Conversation
Co-authored-by: Anthony Kim <anthonykim@microsoft.com>
edvilme
requested review from
ambv,
lysnikolaou and
pablogsal
as code owners
September 9, 2026 23:12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Supersedes #144724
Adds custom hook to PyREPL that can be configured by tools such as VSCode
This pull request introduces a new
pre_execution_hookmechanism to the_pyreplinteractive console, allowing external code to register a callback that is invoked before each statement is executed. Comprehensive tests are added to verify the hook's behavior, including its default state, invocation, error handling, and interaction with REPL commands.New pre-execution hook feature:
pre_execution_hookattribute to_pyrepl.__init__, which can be set to a callable that receives each statement before execution (Lib/_pyrepl/__init__.py).run_multiline_interactive_consoleinsimple_interact.pyto call the hook before executing each statement, safely ignoring exceptions raised by the hook (Lib/_pyrepl/simple_interact.py). [1] [2]Testing and validation:
TestPreExecutionHookintest_interact.pyto verify the hook is unset by default, is called with the correct statement, does not break the REPL if it raises, and is not called for REPL commands (Lib/test/test_pyrepl/test_interact.py)._pyreplandMagicMockfor mocking and patching in tests (Lib/test/test_pyrepl/test_interact.py).CC @brettcannon @anthonykim1 @pablogsal