2

I am new to creating C extension modules for python

I am taking help from the book "programming python" by Mark Lutz

I have written a code to create an extention module for python using this book but I get a error while I run the setup

The code is

#include<Python.h>
#include<string.h>


//MODULE FUNCTIONS..........................................................

static PyObject* message(PyObject *self, PyObject *args)
{
    char *fromPython, result[1024];
    if(!PyArg_ParseTuple(args, "s", &fromPython))
    {
        return NULL;
    }
    else
    {
        strcpy(result, "Hello, ");
        strcat(result, fromPython);
        return Py_BuildValue("s", result);
    }
}

//___________________________________________________________________________



//METHOD REGISTRATION TABLE..................................................

static PyMethodDef hello_methods[]={
//   name        &func      fmt           doc 
    {"message", message, METH_VARARGS, "print a message"},
    {NULL, NULL, 0, NULL}
};

//___________________________________________________________________________


//MODULE DEFINITION STRUCTURE................................................

static struct PyModuleDef hellomodule={
    PyModuleDef_HEAD_INIT,
    "hello"//name of module
    "print messages"//module doc
    -1//size of pre interpreter module state, -1=in global vars
    hello_methods//link to methods table
};

//___________________________________________________________________________




//MODULE INITIALIZER---------------------------------------------------------

PyMODINIT_FUNC PyInit_hello()
{
    PyModule_Create(&hellomodule);
}
//___________________________________________________________________________

The code for setup.py is

from distutils.core import setup, Extension

module1=Extension('hello', include_dirs=['/usr/local/include'], libraries=['pthread'], sources=['hello.c'])

setup(name='hello', version='1.0', description='debesh', url='http://www.debeshmohanty.com', ext_modules=[module1])

The error message I get when I use the command 'python setup.py build' is

running build
running build_ext
building 'hello' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/local/include -I/usr/include/python2.7 -c hello.c -o build/temp.linux-x86_64-2.7/hello.o
hello.c:40:15: error: variable ‘hellomodule’ has initializer but incomplete type
 static struct PyModuleDef hellomodule={
               ^
hello.c:41:2: error: ‘PyModuleDef_HEAD_INIT’ undeclared here (not in a function)
  PyModuleDef_HEAD_INIT,
  ^
hello.c:41:2: warning: excess elements in struct initializer
hello.c:41:2: warning: (near initialization for ‘hellomodule’)
hello.c:45:2: warning: excess elements in struct initializer
  hello_methods//link to methods table
  ^
hello.c:45:2: warning: (near initialization for ‘hellomodule’)
hello.c:45:2: error: expected ‘}’ before ‘hello_methods’
hello.c:55:16: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
 PyMODINIT_FUNC PyInit_hello()
            ^
hello.c: In function ‘PyInit_hello’:
hello.c:57:2: warning: implicit declaration of function ‘PyModule_Create’ [-Wimplicit-function-declaration]
  PyModule_Create(&hellomodule);
  ^
hello.c: At top level:
hello.c:28:20: warning: ‘hello_methods’ defined but not used [-Wunused-variable]
 static PyMethodDef hello_methods[]={
                ^
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
3
  • there might be something wrong with your Python.h Commented Aug 30, 2015 at 11:18
  • 4
    you are trying to build with using python2.7 and some of the definitions (PyModuleDef_HEAD_INIT) belongs to >= python3. And there may be a copy paste error on static struct PyModuleDef hellomodule definition. "," is missing after each parameter. Commented Aug 30, 2015 at 11:28
  • 1
    @seartun I'm also getting the similar error when I try to create an example for generator function in python c extension. Is it possible to write a python c extension for generator function in python 2.7? If possible can you please point to an example? Commented Oct 12, 2015 at 5:43

1 Answer 1

6

The compitable code for python2 and python3:

#include <Python.h>

#if PY_MAJOR_VERSION >= 3
#define PY3K
#endif

// module functions
static PyObject* message(PyObject *self, PyObject *args)
{
    char *fromPython, result[1024];
    if(!PyArg_ParseTuple(args, "s", &fromPython))
    {
        return NULL;
    }
    else
    {
        strcpy(result, "Hello, ");
        strcat(result, fromPython);
        return Py_BuildValue("s", result);
    }
}

// registration table
static PyMethodDef hello_methods[]={
    {"message", message, METH_VARARGS, "func doc"},
    {NULL, NULL, 0, NULL}
};


#ifdef PY3K
// module definition structure for python3
static struct PyModuleDef hellomodule = {
    PyModuleDef_HEAD_INIT,
    "hello",
    "mod doc",
    -1,
    hello_methods
};
// module initializer for python3
PyMODINIT_FUNC PyInit_hello()
{
    return PyModule_Create(&hellomodule);
}
#else
// module initializer for python2
PyMODINIT_FUNC inithello() {
    Py_InitModule3("hello", hello_methods, "mod doc");
}
#endif
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.