我为
Python包装了一些C代码并且它可以工作. C模块创建一个句柄,我将其作为PyCapsule传递给Python.我想要的API可以用Python制作:
import wrapped
class Test(object):
def __init__(self,loc ):
self.handle = wrapped.new(loc)
def foo(self,data):
return wrapped.foo(self.handle,data)
所以问题更多是一个整容问题.我是否必须包装包装器,或者我可以将上面显示的代码移动到C代码中,即导出类而不是一堆函数?
解决方法
是的,您可以在C中创建自己的类类型.从C API开始,Python类型/类是PyTypeObject结构的一个实例,它适合您的类型填充.在下面的教程中很好地概述了执行此操作的整个过程:
https://docs.python.org/2/extending/newtypes.html
这将指导您定义初始核心类型,然后将数据和方法添加到类型/类.起初看起来似乎只是为了让一个用C语言实现的类很多,但是一旦你做了几次并且对它感到满意,那真的不是那么糟糕.
这是您在问题中定义的Test类的简单实现.
#include <Python.h>
#include "structmember.h"
typedef struct {
PyObject_HEAD
/* Your internal 'loc' data. */
int loc;
} Test;
static void
MyTest_dealloc(Test* self)
{
self->ob_type->tp_free((PyObject*)self);
}
static PyObject *
Test_new(PyTypeObject *type,PyObject *args,PyObject *kwds)
{
Test *self;
self = (Test *)type->tp_alloc(type,0);
self->loc = 0;
return (PyObject *)self;
}
static int
Test_init(Test *self,PyObject *kwds)
{
if (! PyArg_ParseTuple(args,"i",&self->loc))
return -1;
return 0;
}
static PyMemberDef Test_members[] = {
{"loc",T_INT,offsetof(Test,loc),"mytestobj loc"},{NULL} /* Sentinel */
};
static PyObject *
Test_foo(Test* self,PyObject *args)
{
int data;
PyObject *result;
if (! PyArg_ParseTuple(args,&data)) {
return NULL;
}
/* We'll just return data + loc as our result. */
result = Py_BuildValue("i",data + self->loc);
return result;
}
static PyMethodDef Test_methods[] = {
{"foo",(PyCFunction)Test_foo,METH_VaraRGS,"Return input parameter added to 'loc' argument from init.",},{NULL} /* Sentinel */
};
static PyTypeObject mytest_MyTestType = {
PyObject_HEAD_INIT(NULL)
0,/*ob_size*/
"mytest.MyTest",/*tp_name*/
sizeof(Test),/*tp_basicsize*/
0,/*tp_itemsize*/
(destructor)MyTest_dealloc,/*tp_dealloc*/
0,/*tp_print*/
0,/*tp_getattr*/
0,/*tp_setattr*/
0,/*tp_compare*/
0,/*tp_repr*/
0,/*tp_as_number*/
0,/*tp_as_sequence*/
0,/*tp_as_mapping*/
0,/*tp_hash */
0,/*tp_call*/
0,/*tp_str*/
0,/*tp_getattro*/
0,/*tp_setattro*/
0,/*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,/*tp_flags*/
"MyTest objects",/* tp_doc */
0,/* tp_traverse */
0,/* tp_clear */
0,/* tp_richcompare */
0,/* tp_weaklistoffset */
0,/* tp_iter */
0,/* tp_iternext */
Test_methods,/* tp_methods */
Test_members,/* tp_members */
0,/* tp_getset */
0,/* tp_base */
0,/* tp_dict */
0,/* tp_descr_get */
0,/* tp_descr_set */
0,/* tp_dictoffset */
(initproc)Test_init,/* tp_init */
0,/* tp_alloc */
Test_new,/* tp_new */
};
static PyMethodDef mytest_methods[] = {
{NULL} /* Sentinel */
};
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC
initmytest(void)
{
PyObject* m;
if (PyType_Ready(&mytest_MyTestType) < 0)
return;
m = Py_InitModule3("mytest",mytest_methods,"Example module that creates an extension type.");
Py_INCREF(&mytest_MyTestType);
PyModule_Addobject(m,"Test",(PyObject *)&mytest_MyTestType);
}
它在Python解释器中的用法:
>>> from mytest import Test >>> t = Test(5) >>> t.foo(10) 15