diff --git a/Lib/_pyrepl/readline.py b/Lib/_pyrepl/readline.py index 143770a885a2c2..483eb1039fa062 100644 --- a/Lib/_pyrepl/readline.py +++ b/Lib/_pyrepl/readline.py @@ -479,15 +479,14 @@ def add_history(self, line: str) -> None: def set_startup_hook(self, function: Callback | None = None) -> None: self.startup_hook = function - def get_line_buffer(self) -> bytes: - buf_str = self.get_reader().get_unicode() - return buf_str.encode(ENCODING) + def get_line_buffer(self) -> str: + return self.get_reader().get_unicode() def _get_idxs(self) -> tuple[int, int]: start = cursor = self.get_reader().pos buf = self.get_line_buffer() for i in range(cursor - 1, -1, -1): - if str(buf[i]) in self.get_completer_delims(): + if buf[i] in self.get_completer_delims(): break start = i return start, cursor diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index b03cf136ec5c78..58078d6ff11b39 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -26,7 +26,8 @@ make_clean_env, ) from _pyrepl.console import Event -from _pyrepl.readline import ReadlineAlikeReader, ReadlineConfig +from _pyrepl.readline import (ReadlineAlikeReader, ReadlineConfig, + _ReadlineWrapper) from _pyrepl.readline import multiline_input as readline_multiline_input try: @@ -516,6 +517,11 @@ def test_basic(self): self.assertEqual(output, "1+1") self.assertEqual(clean_screen(reader.screen), "1+1") + def test_get_line_buffer_returns_str(self): + reader = self.prepare_reader(code_to_events("\n")) + wrapper = _ReadlineWrapper(reader=reader) + self.assertIs(type(wrapper.get_line_buffer()), str) + def test_multiline_edit(self): events = itertools.chain( code_to_events("def f():\n...\n\n"), diff --git a/Misc/NEWS.d/next/Library/2024-08-24-06-05-41.gh-issue-123228.jR_5O5.rst b/Misc/NEWS.d/next/Library/2024-08-24-06-05-41.gh-issue-123228.jR_5O5.rst new file mode 100644 index 00000000000000..99b3c0ca5eef28 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-08-24-06-05-41.gh-issue-123228.jR_5O5.rst @@ -0,0 +1,3 @@ +Fix return type for +:func:`!_pyrepl.readline._ReadlineWrapper.get_line_buffer` to be +:func:`str`. Patch by Sergey B Kirpichev.