0

My problem is as follows: I would like to call a C function from my Python file and return a value back to that Python file. I have tried the following method of using embedded C in Python (the following code is the C code called "mod1.c). I am using Python3.4 so the format follows that given in the documentation guidelines. The problem comes when I call my setup file (second code below). #include #include "sum.h"

static PyObject* 
mod_sum(PyObject *self, PyObject *args)
{
    int a;
    int b;
    int s;
    if (!PyArg_ParseTuple(args,"ii",&a,&b))                     
       return NULL;
    s = sum(a,b);
    return Py_BuildValue("i",s);                                
}

/* DECLARATION OF METHODS    */
static PyMethodDef ModMethods[] = {
    {"sum", mod_sum, METH_VARARGS, "Descirption"},          // {"methName", modName_methName, METH_VARARGS, "Description.."}, modName is name of module and methName is name  of method
    {NULL,NULL,0,NULL}
};

// Module Definition Structure
static struct PyModuleDef summodule = {
   PyModuleDef_HEAD_INIT,
   "sum",  
   NULL, 
   -1,       
   ModMethods       
};

/* INITIALIZATION FUNCTION    */
PyMODINIT_FUNC initmod(void)
{
    PyObject *m;
    m = PyModule_Create(&summodule);
    if (m == NULL)
       return m;
}

Setup.py from distutils.core import setup, Extension

setup(name='buildsum', version='1.0',  \
      ext_modules=[Extension('buildsum', ['mod1.c'])])

The result that I get when I compile my code using gcc is the following error: Cannot export PyInit_buildsum: symbol not defined

I would greatly appreciate any insight or help on this problem, or any suggestion in how to call C from Python. Thank you!

---------------------------------------EDIT --------------------------------- Thank you for the comments: I have tried the following now:

static PyObject* 
PyInit_sum(PyObject *self, PyObject *args)          
{
    int a;
    int b;
    int s;
    if (!PyArg_ParseTuple(args,"ii",&a,&b))                     
       return NULL;
    s = sum(a,b);                                               
    return Py_BuildValue("i",s);                            
}

For the first function; however, I still get the same error of PyInit_sum: symbol not defined

8
  • symbol not defined: the function PyInit_buildsum doesn't exist. In your .c, don't you have to change "sum" into "buildsum" ? Commented Dec 29, 2015 at 23:30
  • Hi, Thank you for the response. If I change buildsum to sum (in the setup file), I get the same error, just with the name "PyInit_sum:symbol not defined" Commented Dec 29, 2015 at 23:38
  • It's something like: mod_sum should be named PyInit_sum, or there is a macro PYINIT(mod_sum) or something to use, instead of writing mod_sum. Commented Dec 29, 2015 at 23:40
  • 1
    Your initmod should be called PyInit_sum or PyInit_buildsum, depending on what you want the entry point to be called. It should return the return value of PyModule_Create, the test for NULL is wrong. Commented Dec 29, 2015 at 23:42
  • @cdarke: THANK YOU!! I tried that (with my original code) and it worked. Commented Dec 29, 2015 at 23:58

1 Answer 1

1

The working code from above in case anyone runs into the same error: the answer from @dclarke is correct. The initialization function in python 3 must have PyInit_(name) as its name.

#include <Python.h>
#include "sum.h"

static PyObject* mod_sum(PyObject *self, PyObject *args)         
{
    int a;
    int b;
    int s;
    if (!PyArg_ParseTuple(args,"ii",&a,&b))                      
       return NULL;
    s = sum(a,b);                                               
    return Py_BuildValue("i",s);                                
}

/* DECLARATION OF METHODS*/
static PyMethodDef ModMethods[] = {
    {"modsum", mod_sum, METH_VARARGS, "Descirption"},           
    {NULL,NULL,0,NULL}
};

// Module Definition Structure
static struct PyModuleDef summodule = {
   PyModuleDef_HEAD_INIT,"modsum", NULL, -1, ModMethods     
};

/* INITIALIZATION FUNCTION*/
PyMODINIT_FUNC PyInit_sum(void)
{
    PyObject *m;
    m = PyModule_Create(&summodule);
    return m; 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.