Skip to content

Commit ae0999e

Browse files
committed
pythongh-121245: Refactor site.register_readline()
also initialize CAN_USE_PYREPL at import _pyrepl.main
1 parent bf74db7 commit ae0999e

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

Lib/_pyrepl/main.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,26 @@
77
else:
88
CAN_USE_PYREPL = sys.getwindowsversion().build >= 10586 # Windows 10 TH2
99

10+
FAIL_MSG = ""
11+
try:
12+
import errno
13+
if not os.isatty(sys.stdin.fileno()):
14+
raise OSError(errno.ENOTTY, "tty required", "stdin")
15+
from .simple_interact import check
16+
if err := check():
17+
raise RuntimeError(err)
18+
except Exception as e:
19+
FAIL_MSG = f"warning: can't use pyrepl: {e}"
20+
CAN_USE_PYREPL = False
21+
1022

1123
def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
1224
global CAN_USE_PYREPL
1325
if not CAN_USE_PYREPL:
26+
if not os.environ.get('PYTHON_BASIC_REPL', None):
27+
from .trace import trace
28+
trace(FAIL_MSG)
29+
print(FAIL_MSG, file=sys.stderr)
1430
return sys._baserepl()
1531

1632
if mainmodule:
@@ -34,22 +50,5 @@ def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
3450
if not hasattr(sys, "ps2"):
3551
sys.ps2 = "... "
3652

37-
run_interactive = None
38-
try:
39-
import errno
40-
if not os.isatty(sys.stdin.fileno()):
41-
raise OSError(errno.ENOTTY, "tty required", "stdin")
42-
from .simple_interact import check
43-
if err := check():
44-
raise RuntimeError(err)
45-
from .simple_interact import run_multiline_interactive_console
46-
run_interactive = run_multiline_interactive_console
47-
except Exception as e:
48-
from .trace import trace
49-
msg = f"warning: can't use pyrepl: {e}"
50-
trace(msg)
51-
print(msg, file=sys.stderr)
52-
CAN_USE_PYREPL = False
53-
if run_interactive is None:
54-
return sys._baserepl()
55-
run_interactive(namespace)
53+
from .simple_interact import run_multiline_interactive_console
54+
run_multiline_interactive_console(namespace)

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)