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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to