Repository navigation
gh-119512: Use new REPL for the code module #148261
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
Open
tanloong
wants to merge
10
commits into
python:main
Choose a base branch
from
tanloong:code-pyrepl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d0ca4a8
Use new REPL for the code module
tanloong 83086b2
blurb
tanloong 355ae0a
fix typo in news entry
tanloong 970032b
Disable PyREPL by default to avoid breaking third party applications
tanloong a90ad04
Add documentation
tanloong 5e45ca6
Revert use_pyrepl=False in sqlite3 as use_pyrepl is False by default
tanloong d77fa60
Add whatsnew
tanloong c9dbd8c
Doc: fix py:meth reference target not found
tanloong c2463fd
Revert irrelevant change
tanloong d5d2a4a
Merge branch 'main' into code-pyrepl
tanloong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
|
|
||
|
|
||
| import builtins | ||
| import os | ||
| import sys | ||
| import traceback | ||
| from codeop import CommandCompiler, compile_command | ||
|
|
@@ -201,7 +202,7 @@ def resetbuffer(self): | |
| """Reset the input buffer.""" | ||
| self.buffer = [] | ||
|
|
||
| def interact(self, banner=None, exitmsg=None): | ||
| def interact(self, banner=None, exitmsg=None, *, use_pyrepl=False): | ||
| """Closely emulate the interactive Python console. | ||
|
|
||
| The optional banner argument specifies the banner to print | ||
|
|
@@ -216,7 +217,29 @@ def interact(self, banner=None, exitmsg=None): | |
| printing an exit message. If exitmsg is not given or None, | ||
| a default message is printed. | ||
|
|
||
| The use_pyrepl argument controls whether to use the pyrepl-based REPL | ||
| when available. When True, pyrepl is used. When False (the default), | ||
| the basic readline-based REPL is used. When None, pyrepl is used | ||
| automatically if available and the PYTHON_BASIC_REPL environment | ||
| variable is not set. | ||
|
|
||
| """ | ||
| if use_pyrepl is None: | ||
| use_pyrepl = not os.getenv('PYTHON_BASIC_REPL') | ||
|
|
||
| if use_pyrepl: | ||
| try: | ||
| from _pyrepl.main import CAN_USE_PYREPL | ||
| if CAN_USE_PYREPL: | ||
| from _pyrepl.simple_interact import ( | ||
| run_multiline_interactive_console, | ||
| ) | ||
| run_multiline_interactive_console(self) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This returns before the code that handles the banner, exit message, and |
||
| return | ||
| except ImportError: | ||
| pass | ||
| # Fall through to basic REPL if pyrepl is unavailable | ||
|
|
||
| try: | ||
| sys.ps1 | ||
| delete_ps1_after = False | ||
|
|
@@ -355,7 +378,7 @@ def __call__(self, code=None): | |
| raise SystemExit(code) | ||
|
|
||
|
|
||
| def interact(banner=None, readfunc=None, local=None, exitmsg=None, local_exit=False): | ||
| def interact(banner=None, readfunc=None, local=None, exitmsg=None, local_exit=False, *, use_pyrepl=False): | ||
| """Closely emulate the interactive Python interpreter. | ||
|
|
||
| This is a backwards compatible interface to the InteractiveConsole | ||
|
|
@@ -369,6 +392,7 @@ def interact(banner=None, readfunc=None, local=None, exitmsg=None, local_exit=Fa | |
| local -- passed to InteractiveInterpreter.__init__() | ||
| exitmsg -- passed to InteractiveConsole.interact() | ||
| local_exit -- passed to InteractiveConsole.__init__() | ||
| use_pyrepl -- passed to InteractiveConsole.interact() | ||
|
|
||
| """ | ||
| console = InteractiveConsole(local, local_exit=local_exit) | ||
|
|
@@ -379,7 +403,7 @@ def interact(banner=None, readfunc=None, local=None, exitmsg=None, local_exit=Fa | |
| import readline # noqa: F401 | ||
| except ImportError: | ||
| pass | ||
| console.interact(banner, exitmsg) | ||
| console.interact(banner, exitmsg, use_pyrepl=use_pyrepl) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Why not make
Nonebehavior - default, and get rid of theuse_pyreploption? Users can override this by setting PYTHON_BASIC_REPL.I presume, new option was chosen on ground backward-compatibility, but new REPL is an incompatible change by itself, this is just a continuation.
So, I think it's fine to remove added option, but please wait first feedback from core developers.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks for your review!
I believe adding the
use_pyreplparameter is necessary, and it should default toFalserather thanNone.Many third-party projects subclass
code.InteractiveConsoleto implement custom shells (1.2k search results on GitHub). CPython's ownsqlite3CLI also builds oncode.InteractiveConsoleand relies on GNU Readline for completion. If we enable the new REPL by default, its completion will break, and we will also see incorrect syntax highlighting: tokens in SQLite statements that coincide with Python keywords will be highlighted mistakenly by PyREPL. Similar issues would affect those among the 1.2k subclasses that implemented their own custom syntax and completions.For backward compatibility, I think keeping
use_pyrepl=Falseas the default is the safest approach.