Need calling python from C

Kerim Borchaev ( WarKiD ) warkid at
Wed Sep 19 12:52:45 CEST 2001

Hello Jay,

why don't you do it like this?

PyObject *FuncObject;
void keyfunc(){

Best regards,
 Kerim                            mailto:warkid at

Wednesday, September 19, 2001, 5:43:57 AM, you wrote:

J> I have tried a few things without luck.

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

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

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

J> Here is my problem...

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

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

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

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

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

J> Thanks,

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

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

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

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

J> /* Table of functions exported by the module */

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

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

J> /* Helper to initialize GNU readline properly. */

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

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

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

More information about the Python-list mailing list