Skip to content

Commit e22f4a2

Browse files
committed
Implement readline.set_auto_history()
As in cPython 3.5, from https://bugs.python.org/issue26870
1 parent f1e5513 commit e22f4a2

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

pyrepl/historical_reader.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def __init__(self, console):
207207
self.transient_history = {}
208208
self.next_history = None
209209
self.isearch_direction = ISEARCH_DIRECTION_NONE
210+
self.should_auto_add_history = True
210211
for c in [next_history, previous_history, restore_history,
211212
first_history, last_history, yank_arg,
212213
forward_history_isearch, reverse_history_isearch,
@@ -291,12 +292,13 @@ def isearch_next(self):
291292

292293
def finish(self):
293294
super(HistoricalReader, self).finish()
294-
ret = self.get_unicode()
295-
for i, t in self.transient_history.items():
296-
if i < len(self.history) and i != self.historyi:
297-
self.history[i] = t
298-
if ret:
299-
self.history.append(ret)
295+
if self.should_auto_add_history:
296+
ret = self.get_unicode()
297+
for i, t in self.transient_history.items():
298+
if i < len(self.history) and i != self.historyi:
299+
self.history[i] = t
300+
if ret:
301+
self.history.append(ret)
300302

301303
def test():
302304
from pyrepl.unix_console import UnixConsole

pyrepl/readline.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
'redisplay',
6363
'remove_history_item',
6464
'replace_history_item',
65+
'set_auto_history',
6566
'set_completer',
6667
'set_completer_delims',
6768
'set_history_length',
@@ -252,6 +253,9 @@ def multiline_input(self, more_lines, ps1, ps2, returns_unicode=False):
252253
def parse_and_bind(self, string):
253254
pass # XXX we don't support parsing GNU-readline-style init files
254255

256+
def set_auto_history(self, enabled):
257+
self.get_reader().should_auto_add_history = enabled
258+
255259
def set_completer(self, function=None):
256260
self.config.readline_completer = function
257261

@@ -400,6 +404,7 @@ def insert_text(self, text):
400404
get_history_item = _wrapper.get_history_item
401405
remove_history_item = _wrapper.remove_history_item
402406
replace_history_item = _wrapper.replace_history_item
407+
set_auto_history = _wrapper.set_auto_history
403408
add_history = _wrapper.add_history
404409
set_startup_hook = _wrapper.set_startup_hook
405410
get_line_buffer = _wrapper.get_line_buffer

testing/test_readline.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,15 @@ def replace(cls, *args):
105105

106106
with open(str(histfile), "r") as f:
107107
assert f.readlines() == ["foo\n", "bar\n"]
108+
109+
110+
@pytest.mark.parametrize('auto_history,expected', [(True, 1), (False, 0)])
111+
def test_set_auto_history(auto_history, expected):
112+
master, slave = pty.openpty()
113+
readline_wrapper = _ReadlineWrapper(slave, slave)
114+
readline_wrapper.set_auto_history(auto_history)
115+
116+
os.write(master, b'input\n')
117+
readline_wrapper.get_reader().readline()
118+
119+
assert readline_wrapper.get_current_history_length() == expected

0 commit comments

Comments
 (0)