diff options
author | Lysandros Nikolaou <lisandrosnik@gmail.com> | 2024-05-23 04:12:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-23 04:12:26 (GMT) |
commit | 9fa1b4fc466e9857210fd6e87f1cbf0c234886ee (patch) | |
tree | 94c20867abccc8fbe4e2b94cb304f0f4d3eb9963 /Lib/_pyrepl | |
parent | dbff1f107731c76770ed279170a709a54601f8be (diff) | |
download | cpython-9fa1b4fc466e9857210fd6e87f1cbf0c234886ee.zip cpython-9fa1b4fc466e9857210fd6e87f1cbf0c234886ee.tar.gz cpython-9fa1b4fc466e9857210fd6e87f1cbf0c234886ee.tar.bz2 |
[3.13] gh-118911: Trailing whitespace in a block shouldn't prevent the user from terminating the code block (GH-119355) (#119404)
(cherry picked from commit 5091c4400c9ea2a2d1e4d89a28c9d0de2651fa6d)
Co-authored-by: Aya Elsayed <ayah.ehab11@gmail.com>
Co-authored-by: Ćukasz Langa <lukasz@langa.pl>
Diffstat (limited to 'Lib/_pyrepl')
-rw-r--r-- | Lib/_pyrepl/historical_reader.py | 2 | ||||
-rw-r--r-- | Lib/_pyrepl/readline.py | 17 |
2 files changed, 16 insertions, 3 deletions
diff --git a/Lib/_pyrepl/historical_reader.py b/Lib/_pyrepl/historical_reader.py index eef7d90..121de33 100644 --- a/Lib/_pyrepl/historical_reader.py +++ b/Lib/_pyrepl/historical_reader.py @@ -259,7 +259,7 @@ class HistoricalReader(Reader): self.transient_history[self.historyi] = self.get_unicode() buf = self.transient_history.get(i) if buf is None: - buf = self.history[i] + buf = self.history[i].rstrip() self.buffer = list(buf) self.historyi = i self.pos = len(self.buffer) diff --git a/Lib/_pyrepl/readline.py b/Lib/_pyrepl/readline.py index 054a39b..57e00a6 100644 --- a/Lib/_pyrepl/readline.py +++ b/Lib/_pyrepl/readline.py @@ -244,14 +244,27 @@ class maybe_accept(commands.Command): r: ReadlineAlikeReader r = self.reader # type: ignore[assignment] r.dirty = True # this is needed to hide the completion menu, if visible - # + # if there are already several lines and the cursor # is not on the last one, always insert a new \n. text = r.get_unicode() + if "\n" in r.buffer[r.pos :] or ( r.more_lines is not None and r.more_lines(text) ): - # + def _newline_before_pos(): + before_idx = r.pos - 1 + while before_idx > 0 and text[before_idx].isspace(): + before_idx -= 1 + return text[before_idx : r.pos].count("\n") > 0 + + # if there's already a new line before the cursor then + # even if the cursor is followed by whitespace, we assume + # the user is trying to terminate the block + if _newline_before_pos() and text[r.pos:].isspace(): + self.finish = True + return + # auto-indent the next line like the previous line prevlinestart, indent = _get_previous_line_indent(r.buffer, r.pos) r.insert("\n") |