diff -r 66a1fbdbe4bb Doc/library/readline.rst --- a/Doc/library/readline.rst Thu May 05 16:21:35 2016 +0300 +++ b/Doc/library/readline.rst Fri May 06 04:29:35 2016 -0500 @@ -156,6 +156,19 @@ This calls :c:func:`add_history` in the underlying library. +.. function:: set_auto_history(enabled) + + Enable or disable automatic calls to :c:func:`add_history` when reading + input via readline. *enabled* should be a Boolean value that when True, + enables auto history and that when False, disables auto history. + + .. versionadded:: 3.6 + + .. impl-detail:: + Auto history is enabled by default, and changes to this do not persist + across multiple sessions. + + Startup hooks ------------- diff -r 66a1fbdbe4bb Modules/readline.c --- a/Modules/readline.c Thu May 05 16:21:35 2016 +0300 +++ b/Modules/readline.c Fri May 06 04:29:35 2016 -0500 @@ -575,6 +575,24 @@ "add_history(string) -> None\n\ add an item to the history buffer"); +static int should_auto_add_history = 1; + +/* Enable or disable automatic history */ + +static PyObject * +py_set_auto_history(PyObject *self, PyObject *args) +{ + if (!PyArg_ParseTuple(args, "p:set_auto_history", + &should_auto_add_history)) { + return NULL; + } + Py_RETURN_NONE; +} + +PyDoc_STRVAR(doc_set_auto_history, +"set_auto_history(enabled) -> None\n\ +Enables or disables automatic history."); + /* Get the tab-completion word-delimiters that readline uses */ @@ -791,6 +809,7 @@ {"set_completer_delims", set_completer_delims, METH_VARARGS, doc_set_completer_delims}, + {"set_auto_history", py_set_auto_history, METH_VARARGS, doc_set_auto_history}, {"add_history", py_add_history, METH_VARARGS, doc_add_history}, {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history}, {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history}, @@ -1279,7 +1298,7 @@ line = (const char *)history_get(length)->line; else line = ""; - if (strcmp(p, line)) + if (should_auto_add_history && strcmp(p, line)) add_history(p); } /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and