Skip to content

Commit c4eaf22

Browse files
committed
pythongh-121245: Refactor site.register_readline()
1 parent 6239d41 commit c4eaf22

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

Lib/site.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -509,35 +509,34 @@ def register_readline():
509509
pass
510510

511511
if readline.get_current_history_length() == 0:
512+
try:
513+
from _pyrepl.main import CAN_USE_PYREPL
514+
except ImportError:
515+
CAN_USE_PYREPL = False
512516
# If no history was loaded, default to .python_history,
513517
# or PYTHON_HISTORY.
514518
# The guard is necessary to avoid doubling history size at
515519
# each interpreter exit when readline was already configured
516520
# through a PYTHONSTARTUP hook, see:
517521
# http://bugs.python.org/issue5845#msg198636
518522
history = gethistoryfile()
523+
if os.getenv("PYTHON_BASIC_REPL") or not CAN_USE_PYREPL:
524+
my_readline = readline
525+
else:
526+
my_readline = _pyrepl.readline
519527
try:
520-
if os.getenv("PYTHON_BASIC_REPL"):
521-
readline.read_history_file(history)
522-
else:
523-
_pyrepl.readline.read_history_file(history)
528+
my_readline.read_history_file(history)
524529
except (OSError,* _pyrepl.unix_console._error):
525530
pass
526531

527532
def write_history():
528533
try:
529-
from _pyrepl.main import CAN_USE_PYREPL
530-
except ImportError:
531-
CAN_USE_PYREPL = False
532-
533-
try:
534-
if os.getenv("PYTHON_BASIC_REPL") or not CAN_USE_PYREPL:
535-
readline.write_history_file(history)
536-
else:
537-
_pyrepl.readline.write_history_file(history)
538-
except (FileNotFoundError, PermissionError):
534+
my_readline.write_history_file(history)
535+
except (FileNotFoundError, PermissionError,
536+
_pyrepl.unix_console.InvalidTerminal):
539537
# home directory does not exist or is not writable
540538
# https://bugs.python.org/issue19891
539+
# or terminal doesn't have the required capability
541540
pass
542541

543542
atexit.register(write_history)

0 commit comments

Comments
 (0)