Need calling python from C

Jay jryansourceforge at yahoo.com
Wed Sep 19 03:43:57 CEST 2001


I have tried a few things without luck.

I am trying to enable readline.bind_key(key,function)

to be called from python and execute the GNU readline
library module that python comes with.

I am editing readline.c in order to make this
feasible.

Here is my problem...

I need to gain access to a pointer to a python
function while I am in C.  I send in my function to C
as a PyObject, I need to be able to take the PyObject
while in C and convert it to a pointer to a function
so that I can pass it to rl_bind_key(int,pointer to
function)

I cannot use the PyObject_CallFunction function
because that will actually execute the function rather
than giving me the pointer so that I can pass it to
rl_bind_key.

Here is my hacked up code, admittedly not good, but I
am just trying to get something to work right now.  I
have gotten the printme function to be passed to the
rl_bind_key function properly.

The printf's that shows "keyfunc is 0x0", cquestion
gets filled appropriately.

I know I should not be sending a PyObject pointer in
for a function pointer, but I was at a loss :)

Thanks,

J
--------------
/* prototype my hack */
static void printme(void);

static PyObject *keyfunc = NULL;
static char cquestion = NULL;
static PyObject *
bind_key(PyObject *self, PyObject *args)
{

PyObject *function = Py_None;
char c;
if (!PyArg_ParseTuple(args, "Oc:bind_key",
&function, &c))
return NULL;
if (PyCallable_Check(function)) {
PyObject *tmp = keyfunc;
Py_INCREF(function);
keyfunc = function;
Py_XDECREF(tmp);
cquestion = c;
printf("cquestions is
%c\n",&cquestion);
}
else {
PyErr_SetString(PyExc_TypeError,
"bind_key(func):
argument not callable");
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}

static char doc_bind_key[] = "\
bind_key(key,function) -> None\n\
Bind a key to a function.\n\
";


/* Table of functions exported by the module */

static struct PyMethodDef readline_methods[] =
{
{"parse_and_bind", parse_and_bind,
METH_VARARGS, doc_parse_and_bind},
{"get_line_buffer", get_line_buffer,
METH_OLDARGS, doc_get_line_buffer},
{"insert_text", insert_text, METH_VARARGS,
doc_insert_text},
{"read_init_file", read_init_file,
METH_VARARGS, doc_read_init_file},
{"read_history_file", read_history_file,
METH_VARARGS, doc_read_history_file},
{"write_history_file", write_history_file,
METH_VARARGS, doc_write_history_file},
{"set_history_length", set_history_length,
METH_VARARGS, set_history_length_doc},
{"get_history_length", get_history_length,
METH_VARARGS, get_history_length_doc},
{"set_completer", set_completer, METH_VARARGS,
doc_set_completer},
{"get_begidx", get_begidx, METH_OLDARGS,
doc_get_begidx},
{"get_endidx", get_endidx, METH_OLDARGS,
doc_get_endidx},

{"set_completer_delims", set_completer_delims,
METH_VARARGS, doc_set_completer_delims},
{"get_completer_delims", get_completer_delims,
METH_OLDARGS, doc_get_completer_delims},
{"bind_key", bind_key, METH_VARARGS,
doc_bind_key},
{0, 0}
};


/* Helper to initialize GNU readline properly. */

static void
setup_readline(void)
{
rl_readline_name = "python";
/* Force rebind of TAB to insert-tab */
rl_bind_key('\t', rl_insert);
/* Bind both ESC-TAB and ESC-ESC to the
completion function */
rl_bind_key_in_map ('\t', rl_complete,
emacs_meta_keymap);
rl_bind_key_in_map ('\033', rl_complete,
emacs_meta_keymap);
/* Set our completion function */
rl_attempted_completion_function =
(CPPFunction *)flex_complete;
/* Set Python word break characters */
rl_completer_word_break_characters =
strdup("
\t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
/* All nonalphanums except '.' */

begidx = PyInt_FromLong(0L);
endidx = PyInt_FromLong(0L);
/* Initialize (allows .inputrc to override)
*
* XXX: A bug in the readline-2.2 library
causes a memory leak
* inside this function.  Nothing we can do
about it.
*/
rl_initialize();
if(keyfunc != NULL)
{
rl_bind_key(cquestion,keyfunc);
printf ("got there\n");
}
else
{
printf ("didn't get there\n");
printf ("keyfunc is %p",keyfunc);
}
rl_bind_key('y',printme);
}

static void printme(void)
{
printf("my test\n");
}






More information about the Python-list mailing list