Re: Need info on integrating python into my application

Guido.van.Rossum@cwi.nl
Thu, 08 Oct 1992 00:30:29 +0100

>I have recently acquired python and trying to understand how to
>integrate python in with my
>application. I have looked through all the documentation but am
>still mystified by one question. How
>do I call a python function from C? This is the typical callback situation.

If you have python 0.9.6 (and there's no reason not to have it), the
file misc/EXTENDING (the place to start if you want to integrate
Python and C code even if you don't want to add new modules) has a
section on calling a Python function from C. The essential thing to
know is to use the function call_object(funcobj, argobj) and to
include <ceval.h> for its declaration (I think -- it's changed a bit
in the almost-0.9.7 version I'm currently using myself...).

>In the callback I want to execute the following bit of python code.
>
>def callback (args):
> stuff ....
>
>In C how do I call the callback function? The question is how do I
>get a handle to the function
>callback so that I can call it.

You call it using call_object(). To get a "handle" to the function,
you need a function object. Most existing modules written in C that
use callbacks have some entry point with which the Python user
passes the callback function to the module, e.g. in the above example
there might be an external module "my_extension" with a function
"call_this_later" and the user might call it as follows (assuming the
above definition of callback():

>>> my_extension.call_this_later(callback)

Cheers,

--Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
"Many of my best friends are lumberjacks and only a few of them are
transvestites"