Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-122273: Support PyREPL history in Windows#122274
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
devdanzin commented Jul 25, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
JeffersGlass commented Jul 29, 2024
vstinner commented Oct 9, 2024
@devdanzin: There is now a merge conflict after my latest site change. Would you mind to update your PR? |
Lib/site.py Outdated
| import readline | ||
| real_readline = True | ||
| except ImportError: | ||
| import _pyrepl.readline as readline |
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.
You should not import if PYTHON_BASIC_REPL is set.
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.
This part replaces an unconditional import of readline:
import atexit try: - import readline+ try:+ import readline+ real_readline = True+ except ImportError:+ import _pyrepl.readline as readline+ real_readline = False import rlcompleter # noqa: F401Then readline is used in a couple places without checking it's a valid module. Should I set readline = None for the PYTHON_BASIC_REPL case and check against it being falsy where needed?
Should look something like:
diff --git a/Lib/site.py b/Lib/site.py index 9d3352c70e3..a27373000fa 100644 --- a/Lib/site.py+++ b/Lib/site.py@@ -502,12 +502,13 @@ def register_readline(): import readline real_readline = True except ImportError: - import _pyrepl.readline as readline+ readline = None real_readline = False import rlcompleter # noqa: F401 if PYTHON_BASIC_REPL: CAN_USE_PYREPL = False else: + import _pyrepl.readline as readline from _pyrepl.main import CAN_USE_PYREPL if real_readline: import _pyrepl.unix_console @@ -521,9 +522,10 @@ def register_readline(): # Reading the initialization (config) file may not be enough to set a # completion key, so we set one first and then read the file. - if getattr(readline, "backend", None) == 'editline':+ backend = getattr(readline, "backend", None)+ if backend == 'editline': readline.parse_and_bind('bind ^I rl_complete') - else:+ elif backend: readline.parse_and_bind('tab: complete') try: @@ -536,7 +538,7 @@ def register_readline(): # want to ignore the exception. pass - if readline.get_current_history_length() == 0:+ if readline and readline.get_current_history_length() == 0: # If no history was loaded, default to .python_history, # or PYTHON_HISTORY. # The guard is necessary to avoid doubling history size at @@ -553,13 +555,15 @@ def register_readline(): exceptions = OSError try: - readline_module.read_history_file(history)+ if readline:+ readline_module.read_history_file(history) except exceptions: pass def write_history(): try: - readline_module.write_history_file(history)+ if readline:+ readline_module.write_history_file(history) except (FileNotFoundError, PermissionError): # home directory does not exist or is not writable # https://bugs.python.org/issue19891There 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.
I've committed a version of the approach above.
Uh oh!
There was an error while loading. Please reload this page.
vstinner commented Nov 22, 2024
I'm not fully satisfied by proposed change, so I proposed a PR based somehow on this one: PR gh-127141. |
devdanzin commented Nov 24, 2024
Closing in favor of PR #127141, which does it better. |

This PR aims to add support for recording and loading command history in Windows, using
_pyrepl.readlineto support handling the history file.