[Python-checkins] python/dist/src/Modules readline.c,2.82,2.83
mwh at users.sourceforge.net
mwh at users.sourceforge.net
Wed Mar 30 13:22:05 CEST 2005
Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10274
Modified Files:
readline.c
Log Message:
Fixes for
[ 1166660 ] The readline module can cause python to segfault
It seems to me that the code I'm rewriting here attempted to call any
user-supplied hook functions using the thread state of the thread that
called the hook-setting function, as opposed to that of the thread
that is currently executing. This doesn't work, in general.
Fix this by using the PyGILState API (It wouldn't be that hard to
define a dummy version of said API when #ifndef WITH_THREAD, would
it?).
Also, check the conversion to integer of the return value of a hook
function for errors (this problem was mentioned in the ipython bug
report linked to in the above bug).
Index: readline.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/readline.c,v
retrieving revision 2.82
retrieving revision 2.83
diff -u -d -r2.82 -r2.83
--- readline.c 30 Mar 2005 10:09:12 -0000 2.82
+++ readline.c 30 Mar 2005 11:21:53 -0000 2.83
@@ -159,8 +159,7 @@
/* Generic hook function setter */
static PyObject *
-set_hook(const char *funcname, PyObject **hook_var,
- PyThreadState **tstate, PyObject *args)
+set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
{
PyObject *function = Py_None;
char buf[80];
@@ -170,14 +169,12 @@
if (function == Py_None) {
Py_XDECREF(*hook_var);
*hook_var = NULL;
- *tstate = NULL;
}
else if (PyCallable_Check(function)) {
PyObject *tmp = *hook_var;
Py_INCREF(function);
*hook_var = function;
Py_XDECREF(tmp);
- *tstate = PyThreadState_GET();
}
else {
PyOS_snprintf(buf, sizeof(buf),
@@ -194,18 +191,15 @@
/* Exported functions to specify hook functions in Python */
static PyObject *startup_hook = NULL;
-static PyThreadState *startup_hook_tstate = NULL;
#ifdef HAVE_RL_PRE_INPUT_HOOK
static PyObject *pre_input_hook = NULL;
-static PyThreadState *pre_input_hook_tstate = NULL;
#endif
static PyObject *
set_startup_hook(PyObject *self, PyObject *args)
{
- return set_hook("startup_hook", &startup_hook,
- &startup_hook_tstate, args);
+ return set_hook("startup_hook", &startup_hook, args);
}
PyDoc_STRVAR(doc_set_startup_hook,
@@ -222,8 +216,7 @@
static PyObject *
set_pre_input_hook(PyObject *self, PyObject *args)
{
- return set_hook("pre_input_hook", &pre_input_hook,
- &pre_input_hook_tstate, args);
+ return set_hook("pre_input_hook", &pre_input_hook, args);
}
PyDoc_STRVAR(doc_set_pre_input_hook,
@@ -239,7 +232,6 @@
/* Exported function to specify a word completer in Python */
static PyObject *completer = NULL;
-static PyThreadState *completer_tstate = NULL;
static PyObject *begidx = NULL;
static PyObject *endidx = NULL;
@@ -403,7 +395,7 @@
static PyObject *
set_completer(PyObject *self, PyObject *args)
{
- return set_hook("completer", &completer, &completer_tstate, args);
+ return set_hook("completer", &completer, args);
}
PyDoc_STRVAR(doc_set_completer,
@@ -584,28 +576,33 @@
/* C function to call the Python hooks. */
static int
-on_hook(PyObject *func, PyThreadState **tstate)
+on_hook(PyObject *func)
{
int result = 0;
if (func != NULL) {
PyObject *r;
- /* Note that readline is called with the interpreter
- lock released! */
- PyEval_RestoreThread(*tstate);
+#ifdef WITH_THREAD
+ PyGILState_STATE gilstate = PyGILState_Ensure();
+#endif
r = PyObject_CallFunction(func, NULL);
if (r == NULL)
goto error;
if (r == Py_None)
result = 0;
- else
+ else {
result = PyInt_AsLong(r);
+ if (result == -1 && PyErr_Occurred())
+ goto error;
+ }
Py_DECREF(r);
goto done;
error:
PyErr_Clear();
Py_XDECREF(r);
done:
- *tstate = PyEval_SaveThread();
+#ifdef WITH_THREAD
+ PyGILState_Release(gilstate);
+#endif
}
return result;
}
@@ -613,14 +610,14 @@
static int
on_startup_hook(void)
{
- return on_hook(startup_hook, &startup_hook_tstate);
+ return on_hook(startup_hook);
}
#ifdef HAVE_RL_PRE_INPUT_HOOK
static int
on_pre_input_hook(void)
{
- return on_hook(pre_input_hook, &pre_input_hook_tstate);
+ return on_hook(pre_input_hook);
}
#endif
@@ -633,11 +630,9 @@
char *result = NULL;
if (completer != NULL) {
PyObject *r;
- /* Note that readline is called with the interpreter
- lock released! */
- PyEval_RestoreThread(completer_tstate);
- /* Don't use the default filename completion if we
- * have a custom completion function... */
+#ifdef WITH_THREAD
+ PyGILState_STATE gilstate = PyGILState_Ensure();
+#endif
rl_attempted_completion_over = 1;
r = PyObject_CallFunction(completer, "si", text, state);
if (r == NULL)
@@ -657,7 +652,9 @@
PyErr_Clear();
Py_XDECREF(r);
done:
- completer_tstate = PyEval_SaveThread();
+#ifdef WITH_THREAD
+ PyGILState_Release(gilstate);
+#endif
}
return result;
}
More information about the Python-checkins
mailing list