summaryrefslogtreecommitdiff
path: root/Modules/readline.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/readline.c')
-rw-r--r--Modules/readline.c194
1 files changed, 134 insertions, 60 deletions
diff --git a/Modules/readline.c b/Modules/readline.c
index 71c24231c8..c154e1ddbd 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -6,6 +6,7 @@
/* Standard definitions */
#include "Python.h"
+#include <stddef.h>
#include <setjmp.h>
#include <signal.h>
#include <errno.h>
@@ -62,6 +63,8 @@ extern char **completion_matches(char *, CPFunction *);
*/
static int using_libedit_emulation = 0;
static const char libedit_version_tag[] = "EditLine wrapper";
+
+static int libedit_history_start = 0;
#endif /* __APPLE__ */
#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
@@ -74,6 +77,55 @@ on_completion_display_matches_hook(char **matches,
(see issue #17289 for the motivation). */
static char *completer_word_break_characters;
+typedef struct {
+ PyObject *completion_display_matches_hook;
+ PyObject *startup_hook;
+ PyObject *pre_input_hook;
+ PyObject *completer;
+ PyObject *begidx;
+ PyObject *endidx;
+} readlinestate;
+
+
+#define readline_state(o) ((readlinestate *)PyModule_GetState(o))
+
+static int
+readline_clear(PyObject *m)
+{
+ readlinestate *state = readline_state(m);
+ Py_CLEAR(state->completion_display_matches_hook);
+ Py_CLEAR(state->startup_hook);
+ Py_CLEAR(state->pre_input_hook);
+ Py_CLEAR(state->completer);
+ Py_CLEAR(state->begidx);
+ Py_CLEAR(state->endidx);
+ return 0;
+}
+
+static int
+readline_traverse(PyObject *m, visitproc visit, void *arg)
+{
+ readlinestate *state = readline_state(m);
+ Py_VISIT(state->completion_display_matches_hook);
+ Py_VISIT(state->startup_hook);
+ Py_VISIT(state->pre_input_hook);
+ Py_VISIT(state->completer);
+ Py_VISIT(state->begidx);
+ Py_VISIT(state->endidx);
+ return 0;
+}
+
+static void
+readline_free(void *m)
+{
+ readline_clear((PyObject *)m);
+}
+
+static PyModuleDef readlinemodule;
+
+#define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule)))
+
+
/* Exported function to send one line to readline's init file parser */
static PyObject *
@@ -84,12 +136,12 @@ parse_and_bind(PyObject *self, PyObject *args)
return NULL;
/* Make a copy -- rl_parse_and_bind() modifies its argument */
/* Bernard Herzog */
- copy = malloc(1 + strlen(s));
+ copy = PyMem_Malloc(1 + strlen(s));
if (copy == NULL)
return PyErr_NoMemory();
strcpy(copy, s);
rl_parse_and_bind(copy);
- free(copy); /* Free the copy */
+ PyMem_Free(copy); /* Free the copy */
Py_RETURN_NONE;
}
@@ -250,23 +302,21 @@ set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
/* Exported functions to specify hook functions in Python */
-static PyObject *completion_display_matches_hook = NULL;
-static PyObject *startup_hook = NULL;
#ifdef HAVE_RL_PRE_INPUT_HOOK
-static PyObject *pre_input_hook = NULL;
+
#endif
static PyObject *
set_completion_display_matches_hook(PyObject *self, PyObject *args)
{
PyObject *result = set_hook("completion_display_matches_hook",
- &completion_display_matches_hook, args);
+ &readlinestate_global->completion_display_matches_hook, args);
#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
/* We cannot set this hook globally, since it replaces the
default completion display. */
rl_completion_display_matches_hook =
- completion_display_matches_hook ?
+ readlinestate_global->completion_display_matches_hook ?
#if defined(_RL_FUNCTION_TYPEDEF)
(rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
#else
@@ -287,7 +337,7 @@ once each time matches need to be displayed.");
static PyObject *
set_startup_hook(PyObject *self, PyObject *args)
{
- return set_hook("startup_hook", &startup_hook, args);
+ return set_hook("startup_hook", &readlinestate_global->startup_hook, args);
}
PyDoc_STRVAR(doc_set_startup_hook,
@@ -304,7 +354,7 @@ before readline prints the first prompt.");
static PyObject *
set_pre_input_hook(PyObject *self, PyObject *args)
{
- return set_hook("pre_input_hook", &pre_input_hook, args);
+ return set_hook("pre_input_hook", &readlinestate_global->pre_input_hook, args);
}
PyDoc_STRVAR(doc_set_pre_input_hook,
@@ -319,10 +369,10 @@ characters.");
/* Exported function to specify a word completer in Python */
-static PyObject *completer = NULL;
-static PyObject *begidx = NULL;
-static PyObject *endidx = NULL;
+
+
+
/* Get the completion type for the scope of the tab-completion */
@@ -342,8 +392,8 @@ Get the type of completion being attempted.");
static PyObject *
get_begidx(PyObject *self, PyObject *noarg)
{
- Py_INCREF(begidx);
- return begidx;
+ Py_INCREF(readlinestate_global->begidx);
+ return readlinestate_global->begidx;
}
PyDoc_STRVAR(doc_get_begidx,
@@ -356,8 +406,8 @@ get the beginning index of the readline tab-completion scope");
static PyObject *
get_endidx(PyObject *self, PyObject *noarg)
{
- Py_INCREF(endidx);
- return endidx;
+ Py_INCREF(readlinestate_global->endidx);
+ return readlinestate_global->endidx;
}
PyDoc_STRVAR(doc_get_endidx,
@@ -522,7 +572,7 @@ get the readline word delimiters for tab-completion");
static PyObject *
set_completer(PyObject *self, PyObject *args)
{
- return set_hook("completer", &completer, args);
+ return set_hook("completer", &readlinestate_global->completer, args);
}
PyDoc_STRVAR(doc_set_completer,
@@ -536,11 +586,11 @@ It should return the next possible completion starting with 'text'.");
static PyObject *
get_completer(PyObject *self, PyObject *noargs)
{
- if (completer == NULL) {
+ if (readlinestate_global->completer == NULL) {
Py_RETURN_NONE;
}
- Py_INCREF(completer);
- return completer;
+ Py_INCREF(readlinestate_global->completer);
+ return readlinestate_global->completer;
}
PyDoc_STRVAR(doc_get_completer,
@@ -579,21 +629,21 @@ get_history_item(PyObject *self, PyObject *args)
return NULL;
#ifdef __APPLE__
if (using_libedit_emulation) {
- /* Libedit emulation uses 0-based indexes,
- * the real one uses 1-based indexes,
- * adjust the index to ensure that Python
- * code doesn't have to worry about the
- * difference.
+ /* Older versions of libedit's readline emulation
+ * use 0-based indexes, while readline and newer
+ * versions of libedit use 1-based indexes.
*/
int length = _py_get_history_length();
- idx --;
+
+ idx = idx - 1 + libedit_history_start;
/*
* Apple's readline emulation crashes when
* the index is out of range, therefore
* test for that and fail gracefully.
*/
- if (idx < 0 || idx >= length) {
+ if (idx < (0 + libedit_history_start)
+ || idx >= (length + libedit_history_start)) {
Py_RETURN_NONE;
}
}
@@ -744,9 +794,6 @@ on_hook(PyObject *func)
int result = 0;
if (func != NULL) {
PyObject *r;
-#ifdef WITH_THREAD
- PyGILState_STATE gilstate = PyGILState_Ensure();
-#endif
r = PyObject_CallFunction(func, NULL);
if (r == NULL)
goto error;
@@ -763,9 +810,6 @@ on_hook(PyObject *func)
PyErr_Clear();
Py_XDECREF(r);
done:
-#ifdef WITH_THREAD
- PyGILState_Release(gilstate);
-#endif
return result;
}
return result;
@@ -774,14 +818,30 @@ on_hook(PyObject *func)
static int
on_startup_hook(void)
{
- return on_hook(startup_hook);
+ int r;
+#ifdef WITH_THREAD
+ PyGILState_STATE gilstate = PyGILState_Ensure();
+#endif
+ r = on_hook(readlinestate_global->startup_hook);
+#ifdef WITH_THREAD
+ PyGILState_Release(gilstate);
+#endif
+ return r;
}
#ifdef HAVE_RL_PRE_INPUT_HOOK
static int
on_pre_input_hook(void)
{
- return on_hook(pre_input_hook);
+ int r;
+#ifdef WITH_THREAD
+ PyGILState_STATE gilstate = PyGILState_Ensure();
+#endif
+ r = on_hook(readlinestate_global->pre_input_hook);
+#ifdef WITH_THREAD
+ PyGILState_Release(gilstate);
+#endif
+ return r;
}
#endif
@@ -808,7 +868,7 @@ on_completion_display_matches_hook(char **matches,
if (PyList_SetItem(m, i, s) == -1)
goto error;
}
- r = PyObject_CallFunction(completion_display_matches_hook,
+ r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
"sOi", matches[0], m, max_length);
Py_DECREF(m); m=NULL;
@@ -838,13 +898,13 @@ static char *
on_completion(const char *text, int state)
{
char *result = NULL;
- if (completer != NULL) {
+ if (readlinestate_global->completer != NULL) {
PyObject *r;
#ifdef WITH_THREAD
PyGILState_STATE gilstate = PyGILState_Ensure();
#endif
rl_attempted_completion_over = 1;
- r = PyObject_CallFunction(completer, "si", text, state);
+ r = PyObject_CallFunction(readlinestate_global->completer, "si", text, state);
if (r == NULL)
goto error;
if (r == Py_None) {
@@ -877,24 +937,32 @@ on_completion(const char *text, int state)
static char **
flex_complete(char *text, int start, int end)
{
+ char **result;
+#ifdef WITH_THREAD
+ PyGILState_STATE gilstate = PyGILState_Ensure();
+#endif
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
rl_completion_append_character ='\0';
#endif
#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
rl_completion_suppress_append = 0;
#endif
- Py_XDECREF(begidx);
- Py_XDECREF(endidx);
- begidx = PyLong_FromLong((long) start);
- endidx = PyLong_FromLong((long) end);
- return completion_matches(text, *on_completion);
+ Py_XDECREF(readlinestate_global->begidx);
+ Py_XDECREF(readlinestate_global->endidx);
+ readlinestate_global->begidx = PyLong_FromLong((long) start);
+ readlinestate_global->endidx = PyLong_FromLong((long) end);
+ result = completion_matches(text, *on_completion);
+#ifdef WITH_THREAD
+ PyGILState_Release(gilstate);
+#endif
+ return result;
}
/* Helper to initialize GNU readline properly. */
static void
-setup_readline(void)
+setup_readline(readlinestate *mod_state)
{
#ifdef SAVE_LOCALE
char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
@@ -908,15 +976,22 @@ setup_readline(void)
*/
if (using_libedit_emulation)
rl_initialize();
+
+ /* Detect if libedit's readline emulation uses 0-based
+ * indexing or 1-based indexing.
+ */
+ add_history("1");
+ if (history_get(1) == NULL) {
+ libedit_history_start = 0;
+ } else {
+ libedit_history_start = 1;
+ }
+ clear_history();
#endif /* __APPLE__ */
using_history();
rl_readline_name = "python";
-#if defined(PYOS_OS2) && defined(PYCC_GCC)
- /* Allow $if term= in .inputrc to work */
- rl_terminal_name = getenv("TERM");
-#endif
/* Force rebind of TAB to insert-tab */
rl_bind_key('\t', rl_insert);
/* Bind both ESC-TAB and ESC-ESC to the completion function */
@@ -935,8 +1010,8 @@ setup_readline(void)
strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
/* All nonalphanums except '.' */
- begidx = PyLong_FromLong(0L);
- endidx = PyLong_FromLong(0L);
+ mod_state->begidx = PyLong_FromLong(0L);
+ mod_state->endidx = PyLong_FromLong(0L);
/* Initialize (allows .inputrc to override)
*
* XXX: A bug in the readline-2.2 library causes a memory leak
@@ -1116,11 +1191,8 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
if (length > 0)
#ifdef __APPLE__
if (using_libedit_emulation) {
- /*
- * Libedit's emulation uses 0-based indexes,
- * the real readline uses 1-based indexes.
- */
- line = (const char *)history_get(length - 1)->line;
+ /* handle older 0-based or newer 1-based indexing */
+ line = (const char *)history_get(length + libedit_history_start - 1)->line;
} else
#endif /* __APPLE__ */
line = (const char *)history_get(length)->line;
@@ -1158,12 +1230,12 @@ static struct PyModuleDef readlinemodule = {
PyModuleDef_HEAD_INIT,
"readline",
doc_module,
- -1,
+ sizeof(readlinestate),
readline_methods,
NULL,
- NULL,
- NULL,
- NULL
+ readline_traverse,
+ readline_clear,
+ readline_free
};
@@ -1171,6 +1243,7 @@ PyMODINIT_FUNC
PyInit_readline(void)
{
PyObject *m;
+ readlinestate *mod_state;
#ifdef __APPLE__
if (strncmp(rl_library_version, libedit_version_tag, strlen(libedit_version_tag)) == 0) {
@@ -1187,7 +1260,8 @@ PyInit_readline(void)
if (m == NULL)
return NULL;
+ mod_state = (readlinestate *) PyModule_GetState(m);
PyOS_ReadlineFunctionPointer = call_readline;
- setup_readline();
+ setup_readline(mod_state);
return m;
}