471,319 Members | 1,381 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

access to python function from C code

Vin
Is it possible to access a function defined in python shell from c-code?

For example if I have defined
def c(x): .... return x*x
....

and defined in some module foo

static PyObject *
ptk_doit(PyObject *self, PyObject *args)
{

get reference to a function here from python interpreter and apply the
function to a given value ...

return Py_BuildValue("d",result);
}
then I can do ...
import foo
foo.doit(c,4)

16

Stuff like this can be done in C when one just transfers the pointer to a
function through input arguments
but then the transfered functions are always set at the time of compilation.
It would be useful if the above can be done, then any function can be
supplied to an underlying C code
without compilation. Probably the performance would be affected but that my
not be a problem in some cases.

Needs like this arise in Optimization for example when
objective function can be arbitrary and optimization is done with general
optimization routines written in C.

Vin
Jul 18 '05 #1
1 1404
Vin wrote:
Is it possible to access a function defined in python shell from c-code?

For example if I have defined
def c(x): ... return x*x
...

and defined in some module foo

static PyObject *
ptk_doit(PyObject *self, PyObject *args)
{

get reference to a function here from python interpreter and apply the
function to a given value ...
Yes, it can be done. Exactly how depends on how you want to call this
C-coded function, see below.

return Py_BuildValue("d",result);
}
then I can do ...
import foo
foo.doit(c,4)
16


So you want to pass the function object and its single argument? OK, BUT:
function can be supplied to an underlying C code
without compilation. Probably the performance would be affected but that
my not be a problem in some cases.

Needs like this arise in Optimization for example when
objective function can be arbitrary and optimization is done with general
optimization routines written in C.


....that's very unlikely to be a sensible tack because you'll call that
function so MANY times the (big) performance impact will hurt.

Still, whatever makes you happy (warning, untested code)...:

PyObject *func, *arg, *resultobj, *floatresult;
double result;

if(!PyArg_ParseTuple(args, "OO", &func, &arg))
return NULL;
resultobj = PyObject_CallFunctionObjArgs(func, arg, NULL);
if (!resultobj)
return NULL;
floatresult = PyNumber_Float(resultobj);
Py_DECREF(resultobj);
if (!floatresult)
return NULL;
result = PyFloat_AS_DOUBLE(floatresult);
Py_DECREF(floatresult);

this should be an acceptable body for ptk_doit before its return
statement.
Alex

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Uggen Kristin T | last post: by
166 posts views Thread by Graham | last post: by
6 posts views Thread by Adam Donahue | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.