From d20d6dfa4901ee85958e32c4c6143eb1fda49c7e Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Mon, 5 Oct 2026 01:03:36 -0700 Subject: [PATCH 1/3] gh-158827: Convert `sys.ps1` and `sys.ps2` to strings before PyREPL use Users may be able to assign non-string, but string representable values to ps1, but these should be converted to their string representations before use by PyREPL rendering. --- Lib/_pyrepl/simple_interact.py | 4 ++-- Lib/test/test_pyrepl/test_interact.py | 30 +++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py index 5a7c8c20202640..011d09e8013bca 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 673827608abc47..1665c6f7889ef7 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("""\ From 9d68e4dce6dfcb395106703015a7daf253e6ccc0 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 5 Oct 2026 08:35:11 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-10-05-08-35-10.gh-issue-130698.iibO9V.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-10-05-08-35-10.gh-issue-130698.iibO9V.rst 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 00000000000000..0c78a0a3cd1768 --- /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`` From 392d59bb8f75a379ecd4dd0f812b083b837d3cfa Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Mon, 5 Oct 2026 09:29:18 -0700 Subject: [PATCH 3/3] Update Misc/NEWS.d/next/Library/2026-10-05-08-35-10.gh-issue-130698.iibO9V.rst Co-authored-by: Brett Cannon --- .../next/Library/2026-10-05-08-35-10.gh-issue-130698.iibO9V.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 0c78a0a3cd1768..2cacb2e58f4036 100644 --- 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 @@ -1 +1 @@ -Fix issue where PyREPL would overflow with errors when assigning a string-representable object to ``sys.ps1`` +Fix issue where PyREPL would overflow with errors when assigning a string-representable object to ``sys.ps1``.