Skip to content

Commit

Permalink
Implement readline.set_auto_history()
Browse files Browse the repository at this point in the history
As in cPython 3.5, from https://bugs.python.org/issue26870
  • Loading branch information
stefanor committed May 31, 2021
1 parent f1e5513 commit e22f4a2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
14 changes: 8 additions & 6 deletions pyrepl/historical_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions pyrepl/readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
'redisplay',
'remove_history_item',
'replace_history_item',
'set_auto_history',
'set_completer',
'set_completer_delims',
'set_history_length',
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions testing/test_readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e22f4a2

Please sign in to comment.