Skip to content

Commit e8d3af0

Browse files
vstinnerdevdanzin
andcommitted
pythongh-122273: Fix PyREPL history on Windows
Co-Authored-By: devdanzin <[email protected]>
1 parent 3c770e3 commit e8d3af0

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

Lib/_pyrepl/readline.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,9 @@ def read_history_file(self, filename: str = gethistoryfile()) -> None:
450450
def write_history_file(self, filename: str = gethistoryfile()) -> None:
451451
maxlength = self.saved_history_length
452452
history = self.get_reader().get_trimmed_history(maxlength)
453-
with open(os.path.expanduser(filename), "w", encoding="utf-8") as f:
453+
f = open(os.path.expanduser(filename), "w",
454+
encoding="utf-8", newline="\n")
455+
with f:
454456
for entry in history:
455457
entry = entry.replace("\n", "\r\n") # multiline history support
456458
f.write(entry + "\n")

Lib/site.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -499,39 +499,53 @@ def register_readline():
499499

500500
import atexit
501501
try:
502-
import readline
503-
import rlcompleter # noqa: F401
502+
try:
503+
import readline
504+
except ImportError:
505+
readline = None
506+
else:
507+
import rlcompleter # noqa: F401
508+
except ImportError:
509+
return
510+
511+
try:
504512
if PYTHON_BASIC_REPL:
505513
CAN_USE_PYREPL = False
506514
else:
507515
original_path = sys.path
508516
sys.path = [p for p in original_path if p != '']
509517
try:
510518
import _pyrepl.readline
511-
import _pyrepl.unix_console
519+
if os.name == "nt":
520+
import _pyrepl.windows_console
521+
console_errors = (_pyrepl.windows_console._error,)
522+
else:
523+
import _pyrepl.unix_console
524+
console_errors = _pyrepl.unix_console._error
512525
from _pyrepl.main import CAN_USE_PYREPL
513526
finally:
514527
sys.path = original_path
515528
except ImportError:
516529
return
517530

518-
# Reading the initialization (config) file may not be enough to set a
519-
# completion key, so we set one first and then read the file.
520-
if readline.backend == 'editline':
521-
readline.parse_and_bind('bind ^I rl_complete')
522-
else:
523-
readline.parse_and_bind('tab: complete')
531+
if readline is not None:
532+
# Reading the initialization (config) file may not be enough to set a
533+
# completion key, so we set one first and then read the file.
534+
if readline.backend == 'editline':
535+
readline.parse_and_bind('bind ^I rl_complete')
536+
else:
537+
readline.parse_and_bind('tab: complete')
524538

525-
try:
526-
readline.read_init_file()
527-
except OSError:
528-
# An OSError here could have many causes, but the most likely one
529-
# is that there's no .inputrc file (or .editrc file in the case of
530-
# Mac OS X + libedit) in the expected location. In that case, we
531-
# want to ignore the exception.
532-
pass
539+
try:
540+
readline.read_init_file()
541+
except OSError:
542+
# An OSError here could have many causes, but the most likely one
543+
# is that there's no .inputrc file (or .editrc file in the case of
544+
# Mac OS X + libedit) in the expected location. In that case, we
545+
# want to ignore the exception.
546+
pass
533547

534-
if readline.get_current_history_length() == 0:
548+
if readline is None or readline.get_current_history_length() == 0:
535549
# If no history was loaded, default to .python_history,
536550
# or PYTHON_HISTORY.
537551
# The guard is necessary to avoid doubling history size at
@@ -542,8 +556,10 @@ def register_readline():
542556

543557
if CAN_USE_PYREPL:
544558
readline_module = _pyrepl.readline
545-
exceptions = (OSError, *_pyrepl.unix_console._error)
559+
exceptions = (OSError, *console_errors)
546560
else:
561+
if readline is None:
562+
return
547563
readline_module = readline
548564
exceptions = OSError
549565

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix PyREPL history on Windows. Patch by devdanzin and Victor Stinner.

0 commit comments

Comments
 (0)