From 072b3db6522b60160a16075c92187faa1723ffaf Mon Sep 17 00:00:00 2001 From: Anson Trapani <77693367+AnsonTrapani@users.noreply.github.com> Date: Sun, 28 Jul 2024 23:50:42 -0400 Subject: [PATCH 1/3] fixed auto indentation size for lines with comments --- Lib/_pyrepl/readline.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/_pyrepl/readline.py b/Lib/_pyrepl/readline.py index 3d94f91753587e..2ee216b31d4b75 100644 --- a/Lib/_pyrepl/readline.py +++ b/Lib/_pyrepl/readline.py @@ -258,7 +258,9 @@ def _should_auto_indent(buffer: list[str], pos: int) -> bool: if buffer[pos] == "\n": break if buffer[pos] == "#": - last_char = None + last_char = "#" + elif last_char == "#" and buffer[pos] == ":": + last_char = ":" return last_char == ":" From cf7a6271f716dc49bcce1674d7a6a0f04049d438 Mon Sep 17 00:00:00 2001 From: Anson Trapani <77693367+AnsonTrapani@users.noreply.github.com> Date: Sun, 28 Jul 2024 23:52:11 -0400 Subject: [PATCH 2/3] Added test for the indentation of repl input with comments --- Lib/test/test_pyrepl/test_reader.py | 69 +++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/Lib/test/test_pyrepl/test_reader.py b/Lib/test/test_pyrepl/test_reader.py index e82c3ca0bb5cc2..f30c846968fb6d 100644 --- a/Lib/test/test_pyrepl/test_reader.py +++ b/Lib/test/test_pyrepl/test_reader.py @@ -186,6 +186,75 @@ def test_newline_within_block_trailing_whitespace(self): self.assert_screen_equals(reader, expected) self.assertTrue(reader.finished) + def test_single_comment_character_after_colon(self): + code = ( + "def foo(): #" + ) + events = itertools.chain( + code_to_events(code), + [ + # go to the end of the first line + Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), + Event(evt="key", data="\x05", raw=bytearray(b"\x1bO5")), + # new line after single comment character + Event(evt="key", data="\n", raw=bytearray(b"\n")), + # go to end of second line + Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), + Event(evt="key", data="\x05", raw=bytearray(b"\x1bO5")), + # write an "a" + Event(evt="key", data="a", raw=bytearray(b"a")) + ], + ) + no_paste_reader = functools.partial(prepare_reader, paste_mode=False) + reader, _ = handle_all_events(events, prepare_reader=no_paste_reader) + expected = ( + "def foo(): #\n" + " a" + ) + self.assert_screen_equals(reader, expected) + + def test_no_code_only_comment_on_single_line(self): + code = ( + "def foo():" + ) + events = itertools.chain( + code_to_events(code), + [ + # go to the end of the first line + Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), + Event(evt="key", data="\x05", raw=bytearray(b"\x1bO5")), + # new line at end of first line and write "# foo" on next line + Event(evt="key", data="\n", raw=bytearray(b"\n")), + Event(evt="key", data="#", raw=bytearray(b"#")), + Event(evt="key", data=" ", raw=bytearray(b" ")), + Event(evt="key", data="f", raw=bytearray(b"f")), + Event(evt="key", data="o", raw=bytearray(b"o")), + Event(evt="key", data="o", raw=bytearray(b"o")), + Event(evt="key", data="\n", raw=bytearray(b"\n")), + # go to end of second line + Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), + Event(evt="key", data="\x05", raw=bytearray(b"\x1bO5")), + # write an "a" + Event(evt="key", data="a", raw=bytearray(b"a")), + Event(evt="key", data=" ", raw=bytearray(b" ")), + Event(evt="key", data="#", raw=bytearray(b"#")), + Event(evt="key", data=" ", raw=bytearray(b" ")), + Event(evt="key", data="f", raw=bytearray(b"f")), + Event(evt="key", data="o", raw=bytearray(b"o")), + Event(evt="key", data="o", raw=bytearray(b"o")), + Event(evt="key", data="\n", raw=bytearray(b"\n")), + ], + ) + no_paste_reader = functools.partial(prepare_reader, paste_mode=False) + reader, _ = handle_all_events(events, prepare_reader=no_paste_reader) + expected = ( + "def foo():\n" + " # foo\n" + " a # foo\n" + " " + ) + self.assert_screen_equals(reader, expected) + def test_input_hook_is_called_if_set(self): input_hook = MagicMock() def _prepare_console(events): From ae1455a1580898231183ad12e7519b9af952770f Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 29 Jul 2024 06:42:02 +0000 Subject: [PATCH 3/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 --- ...-07-29-06-42-01.gh-issue-122237.7w1yfJ.rst | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-07-29-06-42-01.gh-issue-122237.7w1yfJ.rst diff --git a/Misc/NEWS.d/next/Library/2024-07-29-06-42-01.gh-issue-122237.7w1yfJ.rst b/Misc/NEWS.d/next/Library/2024-07-29-06-42-01.gh-issue-122237.7w1yfJ.rst new file mode 100644 index 00000000000000..3ff8b3e9d14d9e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-07-29-06-42-01.gh-issue-122237.7w1yfJ.rst @@ -0,0 +1,27 @@ +# +# Please enter the relevant GitHub issue number here: +# +.. gh-issue: 122237 + +# +# Uncomment one of these "section:" lines to specify which section +# this entry should go in in Misc/NEWS. +# +#.. section: Security +#.. section: Core and Builtins +section: Library +#.. section: Documentation +#.. section: Tests +#.. section: Build +#.. section: Windows +#.. section: macOS +#.. section: IDLE +#.. section: Tools/Demos +#.. section: C API + +# Write your Misc/NEWS entry below. It should be a simple ReST paragraph. +# Don't start with "- Issue #: " or "- gh-issue: " or that sort of stuff. +########################################################################### +Fixed auto-indentation in the REPL when a single line comment character followed a colon on the +same line or when a line containing no code and only a comment followed (directly or indirectly) a +line containing a colon. Patch by Anson Trapani.