callling python function in c

M

masood shaik

Hi

I am trying to call python function from c code.The following
program i got from the web source while i am trying to run this
program it throws an segmentation fault.I am working on Ubuntu 10.10
version.Can anyone guide me please

The following program

call_function.c
// call_function.c - A sample of calling python functions from C code
//
#include <Python.h>
#include<stdio.h>

int main(int argc, char *argv[])
{
int i;
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;

if (argc < 3)
{
printf("Usage: exe_name python_source function_name\n");
return 1;
}

// Initialize the Python Interpreter
Py_Initialize();

// Build the name object
pName = PyString_FromString(argv[1]);

// Load the module object
pModule = PyImport_Import(pName);

// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);

// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, argv[2]);

if (PyCallable_Check(pFunc))
{
// Prepare the argument list for the call
if( argc > 3 )
{
pArgs = PyTuple_New(argc - 3);
for (i = 0; i < argc - 3; i++)
{
pValue = PyInt_FromLong(atoi(argv[i + 3]));
if (!pValue)
{
PyErr_Print();
return 1;
}
PyTuple_SetItem(pArgs, i, pValue);
}

pValue = PyObject_CallObject(pFunc, pArgs);

if (pArgs != NULL)
{
Py_DECREF(pArgs);
}
} else
{
pValue = PyObject_CallObject(pFunc, NULL);
}

if (pValue != NULL)
{
printf("Return of call : %ld\n", PyInt_AsLong(pValue));
Py_DECREF(pValue);
}
else
{
PyErr_Print();
}
} else
{
PyErr_Print();
}

// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);

// Finish the Python Interpreter
Py_Finalize();

return 0;
}

py_function.py

'''py_function.py - Python source designed to demonstrate the use of
python embedding'''

def multiply():
c = 12345*6789
print 'The result of 12345 x 6789 :', c
return c

def multiply1(a,b):
c = a*b
print 'The result of', a, 'x', b, ':', c
return c

Regards
Masood
 
U

Ulrich Eckhardt

Am 16.10.2011 10:03, schrieb masood shaik:
I am trying to call python function from c code.The following
program i got from the web source while i am trying to run this
program it throws an segmentation fault.

Try checking what functions returns instead of blindly using it.
Use a debugger to find out where exactly it segfaults.

Uli
 
A

Anssi Saari

masood shaik said:
Hi

I am trying to call python function from c code.The following
program i got from the web source while i am trying to run this
program it throws an segmentation fault.

Yes, the call to PyImport_Import fails and returns a NULL. You could use
the more complete example at
http://docs.python.org/extending/embedding.html which includes some
error handling...

Anyways, apparently PyImport_Import can't import from the current
working directory but with parameters like math sqrt 2 it returns the
square root of 2. Setting PYTHONPATH to point to the working directory
works for me, but I don't know if there's a more correct solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top