From 05efb387c86af1631ae32b21e8638d97d0318b34 Mon Sep 17 00:00:00 2001 From: HarryLHW <123lhw321@gmail.com> Date: Thu, 25 Jul 2024 01:50:45 +0800 Subject: [PATCH] fix REPL wrong auto-indentation with comments --- Lib/_pyrepl/readline.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Lib/_pyrepl/readline.py b/Lib/_pyrepl/readline.py index 3d94f91753587e..18ac70d2f91587 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 last_char = buffer[pos] else: # even if we found a non-whitespace character before @@ -258,7 +258,16 @@ def _should_auto_indent(buffer: list[str], pos: int) -> bool: if buffer[pos] == "\n": break if buffer[pos] == "#": - last_char = None + while pos: + pos -= 1 + char = buffer[pos] + if char == "\n": + return False + elif char not in " \t#": + last_char = char + break + else: + return False return last_char == ":"