-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-121245: Refactor site.register_readline() #121659
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
Merged
ambv
merged 3 commits into
python:main
from
skirpichev:refactor-register_readline-121245
Jul 15, 2024
Merged
Changes from 1 commit
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,26 @@ | |
else: | ||
CAN_USE_PYREPL = sys.getwindowsversion().build >= 10586 # Windows 10 TH2 | ||
|
||
FAIL_MSG = "" | ||
try: | ||
import errno | ||
if not os.isatty(sys.stdin.fileno()): | ||
raise OSError(errno.ENOTTY, "tty required", "stdin") | ||
from .simple_interact import check | ||
if err := check(): | ||
raise RuntimeError(err) | ||
except Exception as e: | ||
FAIL_MSG = f"warning: can't use pyrepl: {e}" | ||
CAN_USE_PYREPL = False | ||
|
||
|
||
def interactive_console(mainmodule=None, quiet=False, pythonstartup=False): | ||
global CAN_USE_PYREPL | ||
global CAN_USE_PYREPL, FAIL_MSG | ||
if not CAN_USE_PYREPL: | ||
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 old case only happened for old Windows, in which case we didn't print any warning. This is now changing but I think that's fine. |
||
if not os.environ.get('PYTHON_BASIC_REPL', None): | ||
from .trace import trace | ||
trace(FAIL_MSG) | ||
print(FAIL_MSG, file=sys.stderr) | ||
return sys._baserepl() | ||
|
||
if mainmodule: | ||
|
@@ -34,22 +50,5 @@ def interactive_console(mainmodule=None, quiet=False, pythonstartup=False): | |
if not hasattr(sys, "ps2"): | ||
sys.ps2 = "... " | ||
|
||
run_interactive = None | ||
try: | ||
import errno | ||
if not os.isatty(sys.stdin.fileno()): | ||
raise OSError(errno.ENOTTY, "tty required", "stdin") | ||
from .simple_interact import check | ||
if err := check(): | ||
raise RuntimeError(err) | ||
from .simple_interact import run_multiline_interactive_console | ||
run_interactive = run_multiline_interactive_console | ||
except Exception as e: | ||
from .trace import trace | ||
msg = f"warning: can't use pyrepl: {e}" | ||
trace(msg) | ||
print(msg, file=sys.stderr) | ||
CAN_USE_PYREPL = False | ||
if run_interactive is None: | ||
return sys._baserepl() | ||
run_interactive(namespace) | ||
from .simple_interact import run_multiline_interactive_console | ||
run_multiline_interactive_console(namespace) |
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 | ||||
---|---|---|---|---|---|---|
|
@@ -509,32 +509,29 @@ def register_readline(): | |||||
pass | ||||||
|
||||||
if readline.get_current_history_length() == 0: | ||||||
try: | ||||||
from _pyrepl.main import CAN_USE_PYREPL | ||||||
except ImportError: | ||||||
CAN_USE_PYREPL = False | ||||||
# If no history was loaded, default to .python_history, | ||||||
# or PYTHON_HISTORY. | ||||||
# The guard is necessary to avoid doubling history size at | ||||||
# each interpreter exit when readline was already configured | ||||||
# through a PYTHONSTARTUP hook, see: | ||||||
# http://bugs.python.org/issue5845#msg198636 | ||||||
history = gethistoryfile() | ||||||
if os.getenv("PYTHON_BASIC_REPL") or not CAN_USE_PYREPL: | ||||||
my_readline = readline | ||||||
else: | ||||||
my_readline = _pyrepl.readline | ||||||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
try: | ||||||
if os.getenv("PYTHON_BASIC_REPL"): | ||||||
readline.read_history_file(history) | ||||||
else: | ||||||
_pyrepl.readline.read_history_file(history) | ||||||
my_readline.read_history_file(history) | ||||||
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.
Suggested change
|
||||||
except (OSError,* _pyrepl.unix_console._error): | ||||||
pass | ||||||
|
||||||
def write_history(): | ||||||
try: | ||||||
from _pyrepl.main import CAN_USE_PYREPL | ||||||
except ImportError: | ||||||
CAN_USE_PYREPL = False | ||||||
|
||||||
try: | ||||||
if os.getenv("PYTHON_BASIC_REPL") or not CAN_USE_PYREPL: | ||||||
readline.write_history_file(history) | ||||||
else: | ||||||
_pyrepl.readline.write_history_file(history) | ||||||
my_readline.write_history_file(history) | ||||||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
except (FileNotFoundError, PermissionError): | ||||||
# home directory does not exist or is not writable | ||||||
# https://bugs.python.org/issue19891 | ||||||
|
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2024-07-13-06-23-24.gh-issue-121245.RfOgf4.rst
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Simplify handling of the history file in ``site.register_readline()`` | ||
ambv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
helper. The ``CAN_USE_PYREPL`` variable now will be initialized, when | ||
imported. Patch by Sergey B Kirpichev. |
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.
Uh oh!
There was an error while loading. Please reload this page.