如何在Python C API中定义class方法?
How to define class methods in Python C API?
我试图在 C++ 代码中创建 Python class 定义并在 Python 中访问它。然而,函数被调用但参数没有被正确接收。请帮助我正确地做到这一点。
#include <iostream>
#include <Python.h>
using namespace std;
#include <Python.h>
static PyObject* MyClass__Init(PyObject *self, PyObject *args)
{
cout << "MyClass__Init Called" << endl;
Py_INCREF(Py_None);
return Py_None;
};
static PyObject* MyModule__Start(PyObject *self, PyObject *args)
{
const char* zBuff;
if (PyArg_ParseTuple(args, "s", &zBuff))
cout << "MyModule Start Called with parameter " << zBuff << endl;
else
cout << "MyModule Start ERROR" << endl;
Py_INCREF(Py_None);
return Py_None;
};
static PyObject* MyClass__Start(PyObject *self, PyObject *args)
{
const char* zBuff;
if (PyArg_ParseTuple(args, "s", &zBuff))
cout << "MyClass Start Called with parameter" << zBuff << endl;
else
cout << "MyClass Start ERROR" << endl;
Py_INCREF(Py_None);
return Py_None;
};
static PyMethodDef pModuleMethods[] =
{
{"Start", MyModule__Start, METH_VARARGS, ""},
{NULL, NULL, 0, NULL}
};
static PyMethodDef pClassMethods[] =
{
{"__init__", MyClass__Init, METH_VARARGS, ""},
{"Start", MyClass__Start, METH_VARARGS, ""},
{NULL, NULL, 0, NULL}
};
void Start()
{
Py_Initialize();
/* create a new module and class */
PyObject *pClassDic = PyDict_New();
PyObject *pClassName = PyString_FromString("MyClass");
PyObject *pClass = PyClass_New(NULL, pClassDic, pClassName);
PyObject *pModule = Py_InitModule("MyModule", pModuleMethods);
PyObject *pModuleDic = PyModule_GetDict(pModule);
/* add methods to class */
for (PyMethodDef* pDef = pClassMethods; pDef->ml_name != NULL; pDef++)
{
PyObject *pFunc = PyCFunction_New(pDef, NULL);
PyObject *pMethod = PyMethod_New(pFunc, NULL, pClass);
PyDict_SetItemString(pClassDic, pDef->ml_name, pMethod);
}
PyDict_SetItemString(pModuleDic, "MyClass", pClass);
PyRun_SimpleString("import MyModule\n"
"MyModule.Start('Hello Module')\n"
"myObj = MyModule.MyClass()\n"
"myObj.Start('Hello Class')\n");
Py_Finalize();
};
int main()
{
Start();
};
输出是,
MyModule Start Called with parameter Hello Module
MyClass__Init Called
MyClass Start ERROR
调用 Module 函数没有任何问题,但调用 class 方法时没有正确的输入变量。
看来 self 的参数总是 NULL
而不是 - 对于 class 方法 - 在参数列表中传递对 self
的引用。
因此,要解析 class 方法的参数,您还需要解析对 self
的引用。
从 Python 2.6 开始,您可以向 PyArg_ParseTuple
提供格式说明符列表。格式说明符的数量必须适合传递给函数的参数数量(请参阅 (items) (tuple) [matching-items]
下的 https://docs.python.org/2/c-api/arg.html)。
通过修改 MyClass__Start
函数来解析附加参数,您可以解析并打印两个参数以检查它们。
对我来说,下面的代码
static PyObject* MyClass__Start(PyObject *self, PyObject *args)
{
PyObject* argListSelf;
const char* zBuff;
if (PyArg_ParseTuple(args, "Os", &argListSelf, &zBuff)) {
cout << "MyClass Start Called with parameters " <<
cout << PyString_AsString(PyObject_Str(argListSelf)) <<
cout << " and " << zBuff << endl;
cout << "self " << PyString_AsString(PyObject_Str(self)) << endl;
}
else {
if(PyErr_Occurred())
PyErr_Print();
cout << "MyClass Start ERROR" << endl;
}
Py_INCREF(Py_None);
return Py_None;
};
结果
MyModule Start Called with parameter Hello Module
MyClass__Init Called
MyClass Start Called with parameters \
0x602428<?.MyClass instance at 0x7f484a333200>0x602428 and HelloClass
self <NULL>
注意我打印的是self的指针值,是NULL
。
我还添加了
if(PyErr_Occurred())
PyErr_Print();
为了调试目的,我总是会添加它。
我试图在 C++ 代码中创建 Python class 定义并在 Python 中访问它。然而,函数被调用但参数没有被正确接收。请帮助我正确地做到这一点。
#include <iostream>
#include <Python.h>
using namespace std;
#include <Python.h>
static PyObject* MyClass__Init(PyObject *self, PyObject *args)
{
cout << "MyClass__Init Called" << endl;
Py_INCREF(Py_None);
return Py_None;
};
static PyObject* MyModule__Start(PyObject *self, PyObject *args)
{
const char* zBuff;
if (PyArg_ParseTuple(args, "s", &zBuff))
cout << "MyModule Start Called with parameter " << zBuff << endl;
else
cout << "MyModule Start ERROR" << endl;
Py_INCREF(Py_None);
return Py_None;
};
static PyObject* MyClass__Start(PyObject *self, PyObject *args)
{
const char* zBuff;
if (PyArg_ParseTuple(args, "s", &zBuff))
cout << "MyClass Start Called with parameter" << zBuff << endl;
else
cout << "MyClass Start ERROR" << endl;
Py_INCREF(Py_None);
return Py_None;
};
static PyMethodDef pModuleMethods[] =
{
{"Start", MyModule__Start, METH_VARARGS, ""},
{NULL, NULL, 0, NULL}
};
static PyMethodDef pClassMethods[] =
{
{"__init__", MyClass__Init, METH_VARARGS, ""},
{"Start", MyClass__Start, METH_VARARGS, ""},
{NULL, NULL, 0, NULL}
};
void Start()
{
Py_Initialize();
/* create a new module and class */
PyObject *pClassDic = PyDict_New();
PyObject *pClassName = PyString_FromString("MyClass");
PyObject *pClass = PyClass_New(NULL, pClassDic, pClassName);
PyObject *pModule = Py_InitModule("MyModule", pModuleMethods);
PyObject *pModuleDic = PyModule_GetDict(pModule);
/* add methods to class */
for (PyMethodDef* pDef = pClassMethods; pDef->ml_name != NULL; pDef++)
{
PyObject *pFunc = PyCFunction_New(pDef, NULL);
PyObject *pMethod = PyMethod_New(pFunc, NULL, pClass);
PyDict_SetItemString(pClassDic, pDef->ml_name, pMethod);
}
PyDict_SetItemString(pModuleDic, "MyClass", pClass);
PyRun_SimpleString("import MyModule\n"
"MyModule.Start('Hello Module')\n"
"myObj = MyModule.MyClass()\n"
"myObj.Start('Hello Class')\n");
Py_Finalize();
};
int main()
{
Start();
};
输出是,
MyModule Start Called with parameter Hello Module
MyClass__Init Called
MyClass Start ERROR
调用 Module 函数没有任何问题,但调用 class 方法时没有正确的输入变量。
看来 self 的参数总是 NULL
而不是 - 对于 class 方法 - 在参数列表中传递对 self
的引用。
因此,要解析 class 方法的参数,您还需要解析对 self
的引用。
从 Python 2.6 开始,您可以向 PyArg_ParseTuple
提供格式说明符列表。格式说明符的数量必须适合传递给函数的参数数量(请参阅 (items) (tuple) [matching-items]
下的 https://docs.python.org/2/c-api/arg.html)。
通过修改 MyClass__Start
函数来解析附加参数,您可以解析并打印两个参数以检查它们。
对我来说,下面的代码
static PyObject* MyClass__Start(PyObject *self, PyObject *args)
{
PyObject* argListSelf;
const char* zBuff;
if (PyArg_ParseTuple(args, "Os", &argListSelf, &zBuff)) {
cout << "MyClass Start Called with parameters " <<
cout << PyString_AsString(PyObject_Str(argListSelf)) <<
cout << " and " << zBuff << endl;
cout << "self " << PyString_AsString(PyObject_Str(self)) << endl;
}
else {
if(PyErr_Occurred())
PyErr_Print();
cout << "MyClass Start ERROR" << endl;
}
Py_INCREF(Py_None);
return Py_None;
};
结果
MyModule Start Called with parameter Hello Module
MyClass__Init Called
MyClass Start Called with parameters \
0x602428<?.MyClass instance at 0x7f484a333200>0x602428 and HelloClass
self <NULL>
注意我打印的是self的指针值,是NULL
。
我还添加了
if(PyErr_Occurred())
PyErr_Print();
为了调试目的,我总是会添加它。