Uh oh!
There was an error while loading. Please reload this page.
gh-122273: Support PyREPL history in Windows - #122274
Conversation
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? |
| import readline | ||
| real_readline = True | ||
| except ImportError: | ||
| import _pyrepl.readline as readline |
There was a problem hiding this comment.
You should not import if PYTHON_BASIC_REPL is set.
There was a problem hiding this comment.
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.
I've committed a version of the approach above.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Victor Stinner <vstinner@python.org>
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.