diff options
author | Benjamin Peterson <benjamin@python.org> | 2014-11-26 19:58:16 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2014-11-26 19:58:16 (GMT) |
commit | 33f8f15bdd313b1e2d1c99d0971659e983a35672 (patch) | |
tree | dbd199022545350d249c01d659ffcb2a9f55ee27 /Modules/readline.c | |
parent | aacfcccdc39b074521d3e5d4b5a1b1e020662366 (diff) | |
download | cpython-33f8f15bdd313b1e2d1c99d0971659e983a35672.zip cpython-33f8f15bdd313b1e2d1c99d0971659e983a35672.tar.gz cpython-33f8f15bdd313b1e2d1c99d0971659e983a35672.tar.bz2 |
add readline.append_history_file (closes #22940)
patch by "bru"
Diffstat (limited to 'Modules/readline.c')
-rw-r--r-- | Modules/readline.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Modules/readline.c b/Modules/readline.c index f349d3b..b5a0eba 100644 --- a/Modules/readline.c +++ b/Modules/readline.c @@ -237,6 +237,41 @@ Save a readline history file.\n\ The default filename is ~/.history."); +/* Exported function to save part of a readline history file */ + +static PyObject * +append_history_file(PyObject *self, PyObject *args) +{ + int nelements; + PyObject *filename_obj = Py_None, *filename_bytes; + char *filename; + int err; + if (!PyArg_ParseTuple(args, "i|O:append_history_file", &nelements, &filename_obj)) + return NULL; + if (filename_obj != Py_None) { + if (!PyUnicode_FSConverter(filename_obj, &filename_bytes)) + return NULL; + filename = PyBytes_AsString(filename_bytes); + } else { + filename_bytes = NULL; + filename = NULL; + } + errno = err = append_history(nelements, filename); + if (!err && _history_length >= 0) + history_truncate_file(filename, _history_length); + Py_XDECREF(filename_bytes); + errno = err; + if (errno) + return PyErr_SetFromErrno(PyExc_IOError); + Py_RETURN_NONE; +} + +PyDoc_STRVAR(doc_append_history_file, +"append_history_file(nelements[, filename]) -> None\n\ +Append the last nelements of the history list to file.\n\ +The default filename is ~/.history."); + + /* Set history length */ static PyObject* @@ -747,6 +782,8 @@ static struct PyMethodDef readline_methods[] = METH_VARARGS, doc_read_history_file}, {"write_history_file", write_history_file, METH_VARARGS, doc_write_history_file}, + {"append_history_file", append_history_file, + METH_VARARGS, doc_append_history_file}, {"get_history_item", get_history_item, METH_VARARGS, doc_get_history_item}, {"get_current_history_length", (PyCFunction)get_current_history_length, |