diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py index 5a7c8c20202640b..011d09e8013bca5 100644 --- a/Lib/_pyrepl/simple_interact.py +++ b/Lib/_pyrepl/simple_interact.py @@ -135,8 +135,8 @@ def maybe_run_command(statement: str) -> bool: except Exception: pass - ps1 = getattr(sys, "ps1", ">>> ") - ps2 = getattr(sys, "ps2", "... ") + ps1 = str(getattr(sys, "ps1", ">>> ")) + ps2 = str(getattr(sys, "ps2", "... ")) try: statement = multiline_input(more_lines, ps1, ps2) except EOFError: diff --git a/Lib/test/test_pyrepl/test_interact.py b/Lib/test/test_pyrepl/test_interact.py index 673827608abc477..1665c6f7889ef7c 100644 --- a/Lib/test/test_pyrepl/test_interact.py +++ b/Lib/test/test_pyrepl/test_interact.py @@ -1,16 +1,42 @@ import contextlib import io +import sys import warnings import unittest -from unittest.mock import patch +from unittest.mock import ANY, patch from textwrap import dedent from test.support import force_not_colorized from _pyrepl.console import InteractiveColoredConsole -from _pyrepl.simple_interact import _more_lines +from _pyrepl.simple_interact import ( + _more_lines, + run_multiline_interactive_console, +) class TestSimpleInteract(unittest.TestCase): + def test_prompts_are_converted_to_strings(self): + console = InteractiveColoredConsole() + + class Prompt: + def __init__(self, value): + self.value = value + + def __str__(self): + return self.value + + with ( + patch.object(sys, "ps1", Prompt("primary"), create=True), + patch.object(sys, "ps2", Prompt("secondary"), create=True), + patch("_pyrepl.readline._setup"), + patch( + "_pyrepl.simple_interact.multiline_input", side_effect=EOFError + ) as multiline_input, + ): + run_multiline_interactive_console(console) + + multiline_input.assert_called_once_with(ANY, "primary", "secondary") + def test_multiple_statements(self): namespace = {} code = dedent("""\ diff --git a/Misc/NEWS.d/next/Library/2026-10-05-08-35-10.gh-issue-130698.iibO9V.rst b/Misc/NEWS.d/next/Library/2026-10-05-08-35-10.gh-issue-130698.iibO9V.rst new file mode 100644 index 000000000000000..2cacb2e58f40368 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-05-08-35-10.gh-issue-130698.iibO9V.rst @@ -0,0 +1 @@ +Fix issue where PyREPL would overflow with errors when assigning a string-representable object to ``sys.ps1``.