mirror of
https://github.com/python/cpython.git
synced 2025-11-12 07:02:33 +00:00
added history file truncation based upon code from Johannes Zellner.
This commit is contained in:
parent
baf2663e44
commit
49bd24d4e4
1 changed files with 42 additions and 0 deletions
|
|
@ -105,6 +105,7 @@ read_history_file(PyObject *self, PyObject *args)
|
||||||
return Py_None;
|
return Py_None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int history_length = -1; /* do not truncate history by default */
|
||||||
static char doc_read_history_file[] = "\
|
static char doc_read_history_file[] = "\
|
||||||
read_history_file([filename]) -> None\n\
|
read_history_file([filename]) -> None\n\
|
||||||
Load a readline history file.\n\
|
Load a readline history file.\n\
|
||||||
|
|
@ -121,6 +122,8 @@ write_history_file(PyObject *self, PyObject *args)
|
||||||
if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
|
if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
|
||||||
return NULL;
|
return NULL;
|
||||||
errno = write_history(s);
|
errno = write_history(s);
|
||||||
|
if (!errno && history_length >= 0)
|
||||||
|
history_truncate_file(s, history_length);
|
||||||
if (errno)
|
if (errno)
|
||||||
return PyErr_SetFromErrno(PyExc_IOError);
|
return PyErr_SetFromErrno(PyExc_IOError);
|
||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
|
|
@ -134,6 +137,43 @@ The default filename is ~/.history.\
|
||||||
";
|
";
|
||||||
|
|
||||||
|
|
||||||
|
static char set_history_length_doc[] = "\
|
||||||
|
set_history_length(length) -> None\n\
|
||||||
|
set the maximal number of items which will be written to\n\
|
||||||
|
the history file. A negative length is used to inhibit\n\
|
||||||
|
history truncation.\n\
|
||||||
|
";
|
||||||
|
|
||||||
|
static PyObject*
|
||||||
|
set_history_length(PyObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
int length = history_length;
|
||||||
|
PyObject* ob;
|
||||||
|
if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
|
||||||
|
return NULL;
|
||||||
|
history_length = length;
|
||||||
|
Py_INCREF(Py_None);
|
||||||
|
return Py_None;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static char get_history_length_doc[] = "\
|
||||||
|
get_history_length() -> int\n\
|
||||||
|
return the current history length value.\n\
|
||||||
|
";
|
||||||
|
|
||||||
|
static PyObject*
|
||||||
|
get_history_length(PyObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
PyObject* ob;
|
||||||
|
if (!PyArg_ParseTuple(args, ":get_history_length"))
|
||||||
|
return NULL;
|
||||||
|
return Py_BuildValue("i", history_length);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Exported function to specify a word completer in Python */
|
/* Exported function to specify a word completer in Python */
|
||||||
|
|
||||||
static PyObject *completer = NULL;
|
static PyObject *completer = NULL;
|
||||||
|
|
@ -289,6 +329,8 @@ static struct PyMethodDef readline_methods[] =
|
||||||
{"read_init_file", read_init_file, 1, doc_read_init_file},
|
{"read_init_file", read_init_file, 1, doc_read_init_file},
|
||||||
{"read_history_file", read_history_file, 1, doc_read_history_file},
|
{"read_history_file", read_history_file, 1, doc_read_history_file},
|
||||||
{"write_history_file", write_history_file, 1, doc_write_history_file},
|
{"write_history_file", write_history_file, 1, doc_write_history_file},
|
||||||
|
{"set_history_length", set_history_length, 1, set_history_length_doc},
|
||||||
|
{"get_history_length", get_history_length, 1, get_history_length_doc},
|
||||||
{"set_completer", set_completer, 1, doc_set_completer},
|
{"set_completer", set_completer, 1, doc_set_completer},
|
||||||
{"get_begidx", get_begidx, 0, doc_get_begidx},
|
{"get_begidx", get_begidx, 0, doc_get_begidx},
|
||||||
{"get_endidx", get_endidx, 0, doc_get_endidx},
|
{"get_endidx", get_endidx, 0, doc_get_endidx},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue