Skip to content

Commit b506de4

Browse files
miss-islingtonskirpichevambv
authored
[3.13] gh-121245: Refactor site.register_readline() (GH-121659) (GH-121816)
(cherry picked from commit 05d4137) Co-authored-by: Sergey B Kirpichev <[email protected]> Co-authored-by: Łukasz Langa <[email protected]>
1 parent 2ee2bfe commit b506de4

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

Lib/_pyrepl/main.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1+
import errno
12
import os
23
import sys
34

5+
46
CAN_USE_PYREPL: bool
5-
if sys.platform != "win32":
6-
CAN_USE_PYREPL = True
7+
FAIL_REASON: str
8+
try:
9+
if sys.platform == "win32" and sys.getwindowsversion().build < 10586:
10+
raise RuntimeError("Windows 10 TH2 or later required")
11+
if not os.isatty(sys.stdin.fileno()):
12+
raise OSError(errno.ENOTTY, "tty required", "stdin")
13+
from .simple_interact import check
14+
if err := check():
15+
raise RuntimeError(err)
16+
except Exception as e:
17+
CAN_USE_PYREPL = False
18+
FAIL_REASON = f"warning: can't use pyrepl: {e}"
719
else:
8-
CAN_USE_PYREPL = sys.getwindowsversion().build >= 10586 # Windows 10 TH2
20+
CAN_USE_PYREPL = True
21+
FAIL_REASON = ""
922

1023

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

1632
if mainmodule:
@@ -20,6 +36,7 @@ def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
2036
namespace = __main__.__dict__
2137
namespace.pop("__pyrepl_interactive_console", None)
2238

39+
# sys._baserepl() above does this internally, we do it here
2340
startup_path = os.getenv("PYTHONSTARTUP")
2441
if pythonstartup and startup_path:
2542
import tokenize
@@ -34,22 +51,5 @@ def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
3451
if not hasattr(sys, "ps2"):
3552
sys.ps2 = "... "
3653

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)
54+
from .simple_interact import run_multiline_interactive_console
55+
run_multiline_interactive_console(namespace)

Lib/site.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -517,32 +517,29 @@ def register_readline():
517517
pass
518518

519519
if readline.get_current_history_length() == 0:
520+
try:
521+
from _pyrepl.main import CAN_USE_PYREPL
522+
except ImportError:
523+
CAN_USE_PYREPL = False
520524
# If no history was loaded, default to .python_history,
521525
# or PYTHON_HISTORY.
522526
# The guard is necessary to avoid doubling history size at
523527
# each interpreter exit when readline was already configured
524528
# through a PYTHONSTARTUP hook, see:
525529
# http://bugs.python.org/issue5845#msg198636
526530
history = gethistoryfile()
531+
if os.getenv("PYTHON_BASIC_REPL") or not CAN_USE_PYREPL:
532+
readline_module = readline
533+
else:
534+
readline_module = _pyrepl.readline
527535
try:
528-
if os.getenv("PYTHON_BASIC_REPL"):
529-
readline.read_history_file(history)
530-
else:
531-
_pyrepl.readline.read_history_file(history)
536+
readline_module.read_history_file(history)
532537
except (OSError,* _pyrepl.unix_console._error):
533538
pass
534539

535540
def write_history():
536541
try:
537-
from _pyrepl.main import CAN_USE_PYREPL
538-
except ImportError:
539-
CAN_USE_PYREPL = False
540-
541-
try:
542-
if os.getenv("PYTHON_BASIC_REPL") or not CAN_USE_PYREPL:
543-
readline.write_history_file(history)
544-
else:
545-
_pyrepl.readline.write_history_file(history)
542+
readline_module.write_history_file(history)
546543
except (FileNotFoundError, PermissionError):
547544
# home directory does not exist or is not writable
548545
# https://bugs.python.org/issue19891
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Simplify handling of the history file in ``site.register_readline()``
2+
helper. The ``CAN_USE_PYREPL`` variable now will be initialized, when
3+
imported. Patch by Sergey B Kirpichev.

0 commit comments

Comments
 (0)