Skip to content

Commit 492e961

Browse files
committed
Prompt fallback when __str__ raises
1 parent 82fd0c0 commit 492e961

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎Lib/_pyrepl/simple_interact.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,14 @@ def maybe_run_command(statement: str) -> bool:
134134
sys.stdout.flush()
135135
except Exception:
136136
pass
137-
138-
ps1 = str(getattr(sys, "ps1", ">>> "))
139-
ps2 = str(getattr(sys, "ps2", "... "))
137+
try:
138+
ps1 = str(getattr(sys, "ps1", ">>> "))
139+
except Exception:
140+
ps1 = ">>> "
141+
try:
142+
ps2 = str(getattr(sys, "ps2", "... "))
143+
except Exception:
144+
ps2 = "... "
140145
try:
141146
statement = multiline_input(more_lines, ps1, ps2)
142147
except EOFError:

‎Lib/test/test_pyrepl/test_interact.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ def __str__(self):
3737

3838
multiline_input.assert_called_once_with(ANY, "primary", "secondary")
3939

40+
def test_ps1_str_error_uses_default_prompt(self):
41+
class Prompt:
42+
def __str__(self):
43+
return 1 / 0
44+
45+
with (
46+
patch.object(sys, "ps1", Prompt(), create=True),
47+
patch("_pyrepl.readline._setup"),
48+
patch(
49+
"_pyrepl.simple_interact.multiline_input", side_effect=EOFError
50+
) as multiline_input,
51+
):
52+
run_multiline_interactive_console(InteractiveColoredConsole())
53+
54+
multiline_input.assert_called_once_with(ANY, ">>> ", ANY)
55+
4056
def test_multiple_statements(self):
4157
namespace = {}
4258
code = dedent("""\

0 commit comments

Comments
 (0)