简体   繁体   中英

C extension for Python not working

The C extension installs fine (no warning and errors) but when I try to import it I get the following error:

File "", line 1, in ValueError: module functions cannot set METH_CLASS or METH_STATIC

Here is the code, what could be the problem/what can I do to fix and avoid this error? Thank you very much in advance. EDIT: For some reason the compiler didnt show me all the warnings. I fixed the warnings and updated the code, but I still get the same error

#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Python.h>


PyObject *makelist(unsigned int array[], size_t size) {
    PyObject *l = PyList_New(size);
    for (size_t i = 0; i != size; ++i) {
        PyList_SET_ITEM(l, i, Py_BuildValue("i", array[i]));
    }
    return l;
}

static PyObject *module_func(PyObject *self, PyObject *args) {
    unsigned int n;

    if (!PyArg_ParseTuple(args, "i", &n)) {
        return NULL;
    }

    const unsigned int size = n >> 3 + (n & 15 ? 1 : 0);
    const unsigned int arraySize = n >> 1;

    char* bitArray = (char*)malloc(size);

    for (unsigned int i = 0; i < size; i++) {
        *(bitArray + i) = 0b11111111;
    }

    int num;
    for (unsigned int i = 1; i <= ((unsigned int)sqrt(n)) >> 1; i++) {
        int ni = i - 1;
        if ((*(bitArray + (ni >> 3)) >> (ni & 7)) & 1) {
            num = (i << 1) | 1;
            for (unsigned int j = num * num; j <= n; j += num << 1) {
                *(bitArray + ((j - 3) >> 4)) &= ~(1 << (((j - 3) >> 1) & 7));
                // *(bitArray + ((((j - 1) >> 1) - 1) >> 3)) &= ~(1 << (((((j - 1) >> 1) - 1) >> 3) & 7));
            }
        }
    }

    unsigned int* primes = (unsigned int*)malloc((arraySize * sizeof(int)));
    *primes = 2;
    int counter = 1;
    for (unsigned int index = 1; index < arraySize; index++) {
        if ((*(bitArray + ((index - 1) >> 3)) >> ((index - 1) & 7)) & 1) {
            *(primes + counter) = (index << 1) | 1;
            counter++;
        }
    }
    *(primes + counter) = 0;
    return Py_BuildValue("O", makelist(primes, sizeof(primes)));
}

static PyMethodDef module_methods[] = {
    { "func", (PyCFunction)module_func, METH_VARARGS, NULL }
};

static struct PyModuleDef func =
{
    PyModuleDef_HEAD_INIT,
    "module",
    "Null",
    -1,
    module_methods
};

PyMODINIT_FUNC PyInit_func(void) {
    return PyModule_Create(&func);
}

You forgot to null-terminate your module_methods :

static PyMethodDef module_methods[] = {
    { "func", (PyCFunction)module_func, METH_VARARGS, NULL },
    {NULL}
};

Python looks for a null entry to determine the end of a PyMethodDef array. Without the null terminator, Python doesn't know where the array ends, and it walks off the end of the array looking for more methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM