From dbec7927bac16f90f2f4bc72745b6a8281989ac4 Mon Sep 17 00:00:00 2001 From: Arnon Yaari Date: Mon, 19 Aug 2024 12:20:50 +0300 Subject: [PATCH] gh-111201: fix auto-indent in pyrepl for muliple pound comments --- Lib/_pyrepl/readline.py | 2 +- Lib/test/test_pyrepl/test_pyrepl.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Lib/_pyrepl/readline.py b/Lib/_pyrepl/readline.py index 3d94f91753587e..5c19e34bc5cc21 100644 --- a/Lib/_pyrepl/readline.py +++ b/Lib/_pyrepl/readline.py @@ -249,7 +249,7 @@ def _should_auto_indent(buffer: list[str], pos: int) -> bool: while pos > 0: pos -= 1 if last_char is None: - if buffer[pos] not in " \t\n": # ignore whitespaces + if buffer[pos] not in " \t\n#": # ignore whitespaces and comments last_char = buffer[pos] else: # even if we found a non-whitespace character before diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index d5eafc5eb58cac..174228e59e02df 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -465,6 +465,24 @@ def test_auto_indent_with_comment(self): output = multiline_input(reader) self.assertEqual(output, output_code) + def test_auto_indent_with_multicomment(self): + # fmt: off + events = code_to_events( + "def f(): ## foo\n" + "pass\n\n" + ) + + output_code = ( + "def f(): ## foo\n" + " pass\n" + " " + ) + # fmt: on + + reader = self.prepare_reader(events) + output = multiline_input(reader) + self.assertEqual(output, output_code) + def test_auto_indent_ignore_comments(self): # fmt: off events = code_to_events(