1

我第一次尝试集成 C 和 Python 2.7.3。对于初学者,我只是想为 Python 编写一个可以进行基本加法的 C 模块。(它被称为 npfind 是因为一旦我弄清楚了,我想为 numpy 写一个 find 方法)

npfind.h:

#include <math.h>

extern int add(int a, int b);

npfind.c:

#include "npfind.h"

int add(int a, int b)
{
    return a + b;
}

pynpfind.c:

#include "Python.h"
#include "npfind.h"

static char* py_add_doc = "Adds two numbers.";
static PyObject* py_add(PyObject* self, PyObject* args)
{
    int a, b, r;

    if (!PyArg_ParseTuple(args, "ii", &a, &b))
    {
        return NULL;
    }

    r = a + b;

    return Py_BuildValue("i", r);
}

static PyMethodDef* _npfindmethods = {
    {"add", py_add, METH_VARARGS, py_add_doc},
    {NULL, NULL, 0, NULL}
};

void init_npfind(void)
{
    PyObject* mod;
    mod = Py_InitModule("_npfind", _npfindmethods);
}

npfind.py:

from _npfind import *

#Do stuff with the methods

npfindsetup.py

from distutils.core import setup, Extension

setup(name="npfind", version="1.0", py_modules = ['npfind.py'],
      ext_modules=[Extension("_npfind", ["pynpfind.c", "npfind.c"])])

毕竟,在 Windows 7 上,我输入

python npfindsetup.py build_ext --inplace --compiler=mingw32

这似乎有效。然后当我尝试查找 npfind.py 时,我收到此错误:

Traceback (most recent call last):
  File "npfind.py", line 1, in <module>
    from _npfind import *
ValueError: module functions cannot set METH_CLASS or METH_STATIC

我无法弄清楚它在说什么。什么是 METH_CLASS 和 METH_STATIC,我为什么要设置它们?

4

1 回答 1

3

您将 声明_npfindmethods为指针,并尝试将其初始化为数组。当我构建从您的代码段复制的代码时,我会收到很多警告,例如:

a.c:24:5: warning: braces around scalar initializer [enabled by default]
a.c:24:5: warning: (near initialization for '_npfindmethods') [enabled by default]
a.c:24:5: warning: initialization from incompatible pointer type [enabled by default]
a.c:24:5: warning: (near initialization for '_npfindmethods') [enabled by default]
(...)

该变量被初始化为不正确的值,因此 Python 会在其中找到随机数据。


您应该_npfindmethods改为声明为数组:

static PyMethodDef _npfindmethods[] = {
    {"add", py_add, METH_VARARGS, py_add_doc},
    {NULL, NULL, 0, NULL}
};

现在它将按照您的预期进行初始化。另外,因为现在py_add_doc需要有常量地址,所以你也必须把它变成一个数组:

static char py_add_doc[] = "Adds two numbers.";

所以,你的决赛pynpfind.c看起来像:

#include "Python.h"
#include "npfind.h"

static char py_add_doc[] = "Adds two numbers.";
static PyObject* py_add(PyObject* self, PyObject* args)
{
    int a, b, r;

    if (!PyArg_ParseTuple(args, "ii", &a, &b))
    {
        return NULL;
    }

    r = a + b;

    return Py_BuildValue("i", r);
}

static PyMethodDef _npfindmethods[] = {
    {"add", py_add, METH_VARARGS, py_add_doc},
    {NULL, NULL, 0, NULL}
};

void init_npfind(void)
{
    PyObject* mod;
    mod = Py_InitModule("_npfind", _npfindmethods);
}
于 2012-07-20T22:26:33.903 回答