Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e506901

Browse files
author
Skip Montanaro
committed
Add get_history_item and replace_history_item functions to the readline
module. Closes patch #675551. My apologies to Michal Vitecek for taking so long to process this.
1 parent f0d5f79 commit e506901

3 files changed

Lines changed: 85 additions & 3 deletions

File tree

Doc/lib/libreadline.tex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ \section{\module{readline} ---
7171
\versionadded{2.3}
7272
\end{funcdesc}
7373

74+
\begin{funcdesc}{remove_history_item}{pos}
75+
Remove history item specified by its position from the history.
76+
\versionadded{2.4}
77+
\end{funcdesc}
78+
79+
\begin{funcdesc}{replace_history_item}{pos, line}
80+
Replace history item specified by its position with the given line.
81+
\versionadded{2.4}
82+
\end{funcdesc}
83+
7484
\begin{funcdesc}{redisplay}{}
7585
Change what's displayed on the screen to reflect the current contents
7686
of the line buffer. \versionadded{2.3}
@@ -127,7 +137,6 @@ \section{\module{readline} ---
127137
Append a line to the history buffer, as if it was the last line typed.
128138
\end{funcdesc}
129139

130-
131140
\begin{seealso}
132141
\seemodule{rlcompleter}{Completion of Python identifiers at the
133142
interactive prompt.}

Misc/NEWS

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ Extension modules
4444
Library
4545
-------
4646

47+
- patch #675551: Add get_history_item and replace_history_item functions
48+
to the readline module.
49+
4750
- bug #989672: pdb.doc and the help messages for the help_d and help_u methods
48-
of the pdb.Pdb class gives have been corrected. d(own) goes to a newer frame,
49-
u(p) to an older frame, not the other way around.
51+
of the pdb.Pdb class gives have been corrected. d(own) goes to a newer
52+
frame, u(p) to an older frame, not the other way around.
5053

5154
- bug #990669: os.path.realpath() will resolve symlinks before normalizing the
5255
path, as normalizing the path may alter the meaning of the path if it
@@ -82,6 +85,9 @@ Build
8285
- The --with-tsc flag to configure to enable VM profiling with the
8386
processor's timestamp counter now works on PPC platforms.
8487

88+
- patch #1006629: Define _XOPEN_SOURCE to 500 on Solaris 8/9 to match
89+
GCC's definition and avoid redefinition warnings.
90+
8591
C API
8692
-----
8793

Modules/readline.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,71 @@ PyDoc_STRVAR(doc_set_completer_delims,
294294
"set_completer_delims(string) -> None\n\
295295
set the readline word delimiters for tab-completion");
296296

297+
static PyObject *
298+
py_remove_history(PyObject *self, PyObject *args)
299+
{
300+
int entry_number;
301+
HIST_ENTRY *entry;
302+
303+
if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
304+
return NULL;
305+
entry = remove_history(entry_number);
306+
if (!entry) {
307+
char buf[80];
308+
PyOS_snprintf(buf, sizeof(buf),
309+
"No history item at position %i",
310+
entry_number);
311+
PyErr_SetString(PyExc_ValueError, buf);
312+
return NULL;
313+
}
314+
/* free memory allocated for the history entry */
315+
if (entry->line)
316+
free(entry->line);
317+
if (entry->data)
318+
free(entry->data);
319+
free(entry);
320+
321+
Py_INCREF(Py_None);
322+
return Py_None;
323+
}
324+
325+
PyDoc_STRVAR(doc_remove_history,
326+
"remove_history(pos) -> None\n\
327+
remove history item given by its position");
328+
329+
static PyObject *
330+
py_replace_history(PyObject *self, PyObject *args)
331+
{
332+
int entry_number;
333+
char *line;
334+
HIST_ENTRY *old_entry;
335+
336+
if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number, &line)) {
337+
return NULL;
338+
}
339+
old_entry = replace_history_entry(entry_number, line, (void *)NULL);
340+
if (!old_entry) {
341+
char buf[80];
342+
PyOS_snprintf(buf, sizeof(buf),
343+
"No history item at position %i",
344+
entry_number);
345+
PyErr_SetString(PyExc_ValueError, buf);
346+
return NULL;
347+
}
348+
/* free memory allocated for the old history entry */
349+
if (old_entry->line)
350+
free(old_entry->line);
351+
if (old_entry->data)
352+
free(old_entry->data);
353+
free(old_entry);
354+
355+
Py_INCREF(Py_None);
356+
return Py_None;
357+
}
358+
359+
PyDoc_STRVAR(doc_replace_history,
360+
"replace_history(pos, line) -> None\n\
361+
replaces history item given by its position with contents of line");
297362

298363
/* Add a line to the history buffer */
299364

@@ -493,6 +558,8 @@ static struct PyMethodDef readline_methods[] =
493558
{"set_completer_delims", set_completer_delims,
494559
METH_VARARGS, doc_set_completer_delims},
495560
{"add_history", py_add_history, METH_VARARGS, doc_add_history},
561+
{"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
562+
{"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
496563
{"get_completer_delims", get_completer_delims,
497564
METH_NOARGS, doc_get_completer_delims},
498565

0 commit comments

Comments
 (0)