0

I am trying to write a simple python extention in C. However, it keeps throwing these errors about levels of indirection when I try to compile the setup.py. I am quite a noob at C, but after error googling for some time I think there is something wrong with the pointers. Only I have no idea which pointers! Can someone please help me.

This is the C:

#include <Python.h>

static PyObject *exmod_devide(PyObject *self, PyObject *args){
    double a, b;

    // parse the data
    if(!PyArg_ParseTuple(args, 'dd', &a, &b)){  // this is line 7, which throws the first two errors.
        return NULL;
    }
    else{
        return PyFloat_FromDouble(a/b);
    }
}

static PyMethodDef module_functions[] = {
    { "devide", exmod_devide, METH_VARARGS, "Method for deviding two values" },
    { NULL, NULL, 0, NULL }
};

PyMODINIT_FUNC init_exmod(void){
    return PyModule_Create(&module_functions);  // this is line 21, which throws the next two errors
}

For good measure I included the setup.py

from distutils.core import Extension, setup

module1 = Extension('exmod', sources=['exmodmodule.c'])

setup(name='exmod', ext_modules=[module1,])

This is the error:

exmodmodule.c(7): warning C4047: 'function': 'const char *' differs in levels of indirection from 'int'
exmodmodule.c(7): warning C4024: 'PyArg_ParseTuple': different types for formal and actual parameter 2
exmodmodule.c(21): warning C4047: 'function': 'PyModuleDef *' differs in levels of indirection from 'PyMethodDef (*)[2]'
exmodmodule.c(21): warning C4024: 'PyModule_Create2': different types for formal and actual parameter 1

1 Answers1

1

exmodmodule.c(7): warning C4024: 'PyArg_ParseTuple': different types for formal and actual parameter 2

That tells you all you need to know. Let's look at parameter 2:

PyArg_ParseTuple(args, 'dd', &a, &b)

The 2nd parameter is 'dd', and you've contracted language dyslexia dysphoria. In C, unlike Python, strings are always in double quotes. You want

PyArg_ParseTuple(args, "dd", &a, &b)

As a technical matter, 'dd' is, in C, an integer constant formed by the ASCII values of 'd'. Since a lowercase 'd' is 0x64, naturally that integer is 0x6464. Some compilers sometimes warn about multi-character integer constants, but they're still part of the language.

James K. Lowden
  • 7,574
  • 1
  • 16
  • 31