Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-122237: Fix repl auto indentation wrong with comments #122383

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/_pyrepl/readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == ":"


Expand Down
69 changes: 69 additions & 0 deletions Lib/test/test_pyrepl/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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 #<n>: " or "- gh-issue<n>: " 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.
Loading