如何将蟒蛇2 C扩展模块转换到Python 3

问题描述 投票:0回答:1

我试图编译的扩展模块,我定义自定义类型。不过,我得到的用于此目的的一些Python 2代码保持,但我不能为我的生命将其转换成一个Python 3模块。

到目前为止,我已经为了尝试编译它蟒蛇3,通过合并https://docs.python.org/3/extending/newtypes_tutorial.html,but我仍然一事无成一些微调位原代码,我在书“的Python概括地说”找到。我知道这本书的第三版有蟒蛇3版本我试图编译代码,但我没有获得这本书的那个版本。

所以,这里是模块的源代码test.c的:

#include "Python.h"
#include "structmember.h"

/* per-instance data structure */
typedef struct {
    PyObject_HEAD
    int first, second;
} intpair;

static int
intpair_init(PyObject *self, PyObject *args, PyObject *kwds)
{
 static char* nams[] = {"first","second",NULL};
 int first, second;
 if(!PyArg_ParseTupleAndKeywords(args, kwds, "ii", nams, &first, &second))
 return -1;
 ((intpair*)self)->first = first;
 ((intpair*)self)->second = second;
 return 0;
}

static void
intpair_dealloc(PyObject *self)
{
 self->ob_type->tp_free(self);
}


static PyObject* nothing_method(PyObject* self, PyObject* args)
{
    printf("This does literally nothing!\n");

    Py_RETURN_NONE;
}

static PyMemberDef intpair_members[] = {
 {"first", T_INT, offsetof(intpair, first), 0, "first item" },
 {"second", T_INT, offsetof(intpair, second), 0, "second item" },
 {NULL}
};

static PyTypeObject t_intpair = {
 PyObject_HEAD_INIT(0) /* tp_head */
 0, /* tp_internal */
 "test.intpair", /* tp_name */
 sizeof(intpair), /* tp_basicsize */
 0, /* tp_itemsize */
 intpair_dealloc, /* tp_dealloc */
 0, /* tp_print */
 0, /* tp_getattr */
 0, /* tp_setattr */
 0, /* tp_compare */
 0, /* tp_as_number */
 0, /* tp_as_sequence */
 0, /* tp_as_mapping */
 0, /* tp_hash */
 0, /* tp_call */
 0, /* tp_str */
 PyObject_GenericGetAttr, /* tp_getattro */
 PyObject_GenericSetAttr, /* tp_setattro */
 0, /* tp_as_buffer */
 Py_TPFLAGS_DEFAULT,
 "two ints (first,second)",
 0, /* tp_traverse */
 0, /* tp_clear */
 0, /* tp_richcompare */
 0, /* tp_weaklistoffset */
 0, /* tp_iter */
 0, /* tp_iternext */
 0, /* tp_methods */
 intpair_members, /* tp_members */
 0, /* tp_getset */
  0, /* tp_base */
 0, /* tp_dict */
 0, /* tp_descr_get */
 0, /* tp_descr_set */
 0, /* tp_dictoffset */
 intpair_init, /* tp_init */
 PyType_GenericAlloc, /* tp_alloc */
 PyType_GenericNew, /* tp_new */
 PyObject_Del, /* tp_free */
};


static PyMemberDef Noddy_members[] = {
    {"PI", T_INT, 3, 0, "noddy number"},
    {NULL, NULL, 0, NULL}
};

static PyMethodDef CosMethods[] =
{
     {"does_nothing", nothing_method, METH_VARARGS, "This really does nothing"},
     {NULL, NULL, 0, NULL}
};

static PyModuleDef testmodule = {
    PyModuleDef_HEAD_INIT,
    .m_name = "test",
    .m_doc = "Example module that creates an extension type.",
    .m_size = -1,
    CosMethods,
};

PyMODINIT_FUNC
PyInit_test(void)
{
    PyObject *m;
    if (PyType_Ready(&t_intpair) < 0)
        return NULL;

    m = PyModule_Create(&testmodule);
    if (m == NULL)
        return NULL;

    Py_INCREF(&t_intpair);
    PyModule_AddObject(m, "Custom", (PyObject *) &t_intpair);
    return m;
}

和文件setup.py:

from distutils.core import setup, Extension
setup(name="test", version="1.0",
      ext_modules=[
         Extension("test", ["test.c"]),
         ])

我可以使用python3 setup.py build_ext --inplace成功编译此模块,但是,当我导入此模块在Python 3我得到以下错误:

Python 3.7.2 (default, Jan 10 2019, 23:51:51) 
[GCC 8.2.1 20181127] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: Type does not define the tp_name field.

我不明白此错误消息,因为有一个tp_name场“test.intpair”在PyTypeObject定义。我缺少的是在这里吗?

此外,什么是必要的,这个代码更改,使其编译蟒蛇3?

谢谢!

python-c-api
1个回答
2
投票

我敢肯定的缺陷是线0, /* tp_internal */在:

 PyObject_HEAD_INIT(0) /* tp_head */
 0, /* tp_internal */
 "test.intpair", /* tp_name */

如果你搜索这条线将其在互联网上的唯一引用这个问题,你用的书 - 这是完全不标准,我不知道从哪里它从何而来。我半怀疑的Python 2代码使用PyVarObject,然后使用tp_internal填补了尺寸,而不是PyVarObject_HEAD_INIT,但我不能看到这本书的相关页面...

The current documentation recommends using C99 initialization

static PyTypeObject t_intpair = {
    PyObject_HEAD_INIT(0)
    .tp_name = "test.intpair",
    // etc
 };

以便获得属性权不再是问题的顺序(没有指定什么是初始化为0)。


建立你的代码提供了有关类型不匹配的一些非常明确的警告:

warning: initialization of ‘long int’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion] “test_mod.intpair”/ * * tp_name /`

你应该注意这些

© www.soinside.com 2019 - 2024. All rights reserved.