Skip to content

Commit 7d4f67e

Browse files
pythongh-141998: Do not syntax highlight text read by input() in the REPL
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent dcf9087 commit 7d4f67e

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎Lib/_pyrepl/readline.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ def input(self, prompt: object = "") -> str:
377377
prompt_str = str(prompt)
378378
reader.ps1 = prompt_str
379379
sys.audit("builtins.input", prompt_str)
380-
with reader.suspend_history_writes():
380+
# The input is not Python code, so do not syntax highlight it.
381+
with reader.suspend_history_writes(), reader.suspend_colorization():
381382
result = reader.readline(startup_hook=self.startup_hook)
382383
sys.audit("builtins.input/result", result)
383384
return result

‎Lib/test/test_pyrepl/test_pyrepl.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,21 @@ def test_input_does_not_modify_history(self):
732732
self.assertEqual(wrapper.input(), "new input")
733733
self.assertEqual(reader.history, ["previous input"])
734734

735+
def test_input_is_not_syntax_highlighted(self):
736+
# gh-141998: Text read with input() is not Python code, so it must
737+
# not be syntax highlighted, but the REPL input still is.
738+
reader = self.prepare_reader(code_to_events("def f(): pass\n1+1\n"))
739+
reader.can_colorize = True
740+
gen_colors = Mock(wraps=reader.gen_colors)
741+
reader.gen_colors = gen_colors
742+
wrapper = _ReadlineWrapper(f_in=None, f_out=None, reader=reader)
743+
744+
self.assertEqual(wrapper.input(), "def f(): pass")
745+
gen_colors.assert_not_called()
746+
self.assertTrue(reader.can_colorize)
747+
self.assertEqual(multiline_input(reader), "1+1")
748+
gen_colors.assert_called()
749+
735750
def test_input_error_does_not_modify_history(self):
736751
for text, exception in (
737752
("partial input\x03", KeyboardInterrupt),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Do not syntax highlight text typed in response to :func:`input` in the
2+
:term:`REPL`.

0 commit comments

Comments
 (0)