From e22f4a29cfcc13f37e597ec72f95d695d6981108 Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Tue, 13 Oct 2020 20:42:36 -0700 Subject: [PATCH] Implement readline.set_auto_history() As in cPython 3.5, from https://bugs.python.org/issue26870 --- pyrepl/historical_reader.py | 14 ++++++++------ pyrepl/readline.py | 5 +++++ testing/test_readline.py | 12 ++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pyrepl/historical_reader.py b/pyrepl/historical_reader.py index 01522ee..7aee968 100644 --- a/pyrepl/historical_reader.py +++ b/pyrepl/historical_reader.py @@ -207,6 +207,7 @@ def __init__(self, console): self.transient_history = {} self.next_history = None self.isearch_direction = ISEARCH_DIRECTION_NONE + self.should_auto_add_history = True for c in [next_history, previous_history, restore_history, first_history, last_history, yank_arg, forward_history_isearch, reverse_history_isearch, @@ -291,12 +292,13 @@ def isearch_next(self): def finish(self): super(HistoricalReader, self).finish() - ret = self.get_unicode() - for i, t in self.transient_history.items(): - if i < len(self.history) and i != self.historyi: - self.history[i] = t - if ret: - self.history.append(ret) + if self.should_auto_add_history: + ret = self.get_unicode() + for i, t in self.transient_history.items(): + if i < len(self.history) and i != self.historyi: + self.history[i] = t + if ret: + self.history.append(ret) def test(): from pyrepl.unix_console import UnixConsole diff --git a/pyrepl/readline.py b/pyrepl/readline.py index 8ac466d..762f14f 100644 --- a/pyrepl/readline.py +++ b/pyrepl/readline.py @@ -62,6 +62,7 @@ 'redisplay', 'remove_history_item', 'replace_history_item', + 'set_auto_history', 'set_completer', 'set_completer_delims', 'set_history_length', @@ -252,6 +253,9 @@ def multiline_input(self, more_lines, ps1, ps2, returns_unicode=False): def parse_and_bind(self, string): pass # XXX we don't support parsing GNU-readline-style init files + def set_auto_history(self, enabled): + self.get_reader().should_auto_add_history = enabled + def set_completer(self, function=None): self.config.readline_completer = function @@ -400,6 +404,7 @@ def insert_text(self, text): get_history_item = _wrapper.get_history_item remove_history_item = _wrapper.remove_history_item replace_history_item = _wrapper.replace_history_item +set_auto_history = _wrapper.set_auto_history add_history = _wrapper.add_history set_startup_hook = _wrapper.set_startup_hook get_line_buffer = _wrapper.get_line_buffer diff --git a/testing/test_readline.py b/testing/test_readline.py index a40aed0..f126aba 100644 --- a/testing/test_readline.py +++ b/testing/test_readline.py @@ -105,3 +105,15 @@ def replace(cls, *args): with open(str(histfile), "r") as f: assert f.readlines() == ["foo\n", "bar\n"] + + +@pytest.mark.parametrize('auto_history,expected', [(True, 1), (False, 0)]) +def test_set_auto_history(auto_history, expected): + master, slave = pty.openpty() + readline_wrapper = _ReadlineWrapper(slave, slave) + readline_wrapper.set_auto_history(auto_history) + + os.write(master, b'input\n') + readline_wrapper.get_reader().readline() + + assert readline_wrapper.get_current_history_length() == expected