Skip to content

Commit

Permalink
pythongh-119102: Fix REPL for dumb terminal
Browse files Browse the repository at this point in the history
Move CAN_USE_PYREPL variable from _pyrepl.__main__ to _pyrepl and use
it in the site module to decide if _pyrepl.write_history_file() can
be used.
  • Loading branch information
vstinner committed May 20, 2024
1 parent 72d07dd commit b07fabe
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Lib/_pyrepl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import sys

CAN_USE_PYREPL = sys.platform != "win32"
8 changes: 3 additions & 5 deletions Lib/_pyrepl/__main__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import os
import sys

CAN_USE_PYREPL = sys.platform != "win32"
import _pyrepl


def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
global CAN_USE_PYREPL
if not CAN_USE_PYREPL:
if not _pyrepl.CAN_USE_PYREPL:
return sys._baserepl()

startup_path = os.getenv("PYTHONSTARTUP")
Expand Down Expand Up @@ -38,7 +36,7 @@ def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
msg = f"warning: can't use pyrepl: {e}"
trace(msg)
print(msg, file=sys.stderr)
CAN_USE_PYREPL = False
_pyrepl.CAN_USE_PYREPL = False
if run_interactive is None:
return sys._baserepl()
return run_interactive(mainmodule)
Expand Down
3 changes: 2 additions & 1 deletion Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,9 @@ def register_readline():
pass

def write_history():
import _pyrepl
try:
if os.getenv("PYTHON_BASIC_REPL"):
if os.getenv("PYTHON_BASIC_REPL") or not _pyrepl.CAN_USE_PYREPL:
readline.write_history_file(history)
else:
_pyrepl.readline.write_history_file(history)
Expand Down

0 comments on commit b07fabe

Please sign in to comment.