Skip to content

Commit b926c8e

Browse files
vstinnerefimov-mikhail
authored andcommitted
pythongh-125096: Don't import _pyrepl in site if PYTHON_BASIC_REPL (python#125097)
If the PYTHON_BASIC_REPL environment variable is set, the site module no longer imports the _pyrepl module. Moreover, the site module now respects -E and -I command line options: ignore PYTHON_BASIC_REPL in this case.
1 parent 6b6d27f commit b926c8e

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

Lib/site.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,21 @@ def register_readline():
491491
This can be overridden in the sitecustomize or usercustomize module,
492492
or in a PYTHONSTARTUP file.
493493
"""
494+
if not sys.flags.ignore_environment:
495+
PYTHON_BASIC_REPL = os.getenv("PYTHON_BASIC_REPL")
496+
else:
497+
PYTHON_BASIC_REPL = False
498+
494499
import atexit
495500
try:
496501
import readline
497502
import rlcompleter # noqa: F401
498-
import _pyrepl.readline
499-
import _pyrepl.unix_console
503+
if PYTHON_BASIC_REPL:
504+
CAN_USE_PYREPL = False
505+
else:
506+
import _pyrepl.readline
507+
import _pyrepl.unix_console
508+
from _pyrepl.main import CAN_USE_PYREPL
500509
except ImportError:
501510
return
502511

@@ -517,21 +526,24 @@ def register_readline():
517526
pass
518527

519528
if readline.get_current_history_length() == 0:
520-
from _pyrepl.main import CAN_USE_PYREPL
521529
# If no history was loaded, default to .python_history,
522530
# or PYTHON_HISTORY.
523531
# The guard is necessary to avoid doubling history size at
524532
# each interpreter exit when readline was already configured
525533
# through a PYTHONSTARTUP hook, see:
526534
# http://bugs.python.org/issue5845#msg198636
527535
history = gethistoryfile()
528-
if os.getenv("PYTHON_BASIC_REPL") or not CAN_USE_PYREPL:
529-
readline_module = readline
530-
else:
536+
537+
if CAN_USE_PYREPL:
531538
readline_module = _pyrepl.readline
539+
exceptions = (OSError, *_pyrepl.unix_console._error)
540+
else:
541+
readline_module = readline
542+
exceptions = OSError
543+
532544
try:
533545
readline_module.read_history_file(history)
534-
except (OSError,* _pyrepl.unix_console._error):
546+
except exceptions:
535547
pass
536548

537549
def write_history():

Lib/test/test_pyrepl/test_pyrepl.py

+12
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,18 @@ def test_python_basic_repl(self):
12041204
self.assertNotIn("Exception", output)
12051205
self.assertNotIn("Traceback", output)
12061206

1207+
# The site module must not load _pyrepl if PYTHON_BASIC_REPL is set
1208+
commands = ("import sys\n"
1209+
"print('_pyrepl' in sys.modules)\n"
1210+
"exit()\n")
1211+
env["PYTHON_BASIC_REPL"] = "1"
1212+
output, exit_code = self.run_repl(commands, env=env)
1213+
self.assertEqual(exit_code, 0)
1214+
self.assertIn("False", output)
1215+
self.assertNotIn("True", output)
1216+
self.assertNotIn("Exception", output)
1217+
self.assertNotIn("Traceback", output)
1218+
12071219
@force_not_colorized
12081220
def test_bad_sys_excepthook_doesnt_crash_pyrepl(self):
12091221
env = os.environ.copy()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
If the :envvar:`PYTHON_BASIC_REPL` environment variable is set, the
2+
:mod:`site` module no longer imports the :mod:`!_pyrepl` module. Moreover,
3+
the :mod:`site` module now respects :option:`-E` and :option:`-I` command
4+
line options: ignore :envvar:`PYTHON_BASIC_REPL` in this case. Patch by
5+
Victor Stinner.

0 commit comments

Comments
 (0)