I'd like to call a custom function that is defined in a python module from C. I have some preliminary code to do that, but it just prints the output to stdout.
mytest.py
import math
def myabs(x):
return math.fabs(x)
test.cpp
#include <Python.h>
int main() {
Py_Initialize();
PyRun_SimpleString("import sys; sys.path.append('.')");
PyRun_SimpleString("import mytest;");
PyRun_SimpleString("print mytest.myabs(2.0)");
Py_Finalize();
return 0;
}
How can I extract the return value into a C double and use it in C?
If you assign the return value to a variable, then you can use something like PyEval_GetGlobals() and PyDict_GetItemString() to get the PyObject. From there, PyNumber_Float can get you the value you want.
I suggest browsing through the whole API - certain things become obvious when you see the different methods that are available to you, and there may well be a better method than the one I've described.
I have done it using BOOST to embedded Python to C++ [This working C module should help]
As explained before, using PyRun_SimpleString seems to be a bad idea.
You should definitely use the methods provided by the C-API (http://docs.python.org/c-api/).
Reading the introduction is the first thing to do to understand the way it works.
First, you have to learn about PyObject that is the basic object for the C API. It can represent any kind of python basic types (string, float, int,...).
Many functions exist to convert for example python string to char* or PyFloat to double.
First, import your module :
Then getting a reference to your function :
Then getting your result :
And getting back to a double :
You should obviously check the errors (cf. link given by Mark Tolonen).
If you have any question, don't hesitate. Good luck.
Here is a sample code I wrote (with the help of various online sources) to send a string to a Python code, then return a value.
Here is the C code
call_function.c
:Here is the Python code, in file
arbName.py
:I use the command
gcc call_function.c -I/usr/include/python2.6 -lpython2.6 ; ./a.out
to run this process. I'm on redhat. I recommend using PyErr_Print(); for error checking.A complete example of calling a Python function and retrieving the result is located at http://docs.python.org/release/2.6.5/extending/embedding.html#pure-embedding:
You have to extract the python method somehow and run it with
PyObject_CallObject()
. To do that, you can provide Python a way to set the function, as the Extending and Embedding Python Tutorial example does.