#include "Python.h"
#include <iostream>
class MyClass {
public:
MyClass() {};
static MyClass& getInstance() {
static MyClass instance;
return instance;
}
void tester() {
std::cout << "testing..." << std::endl;
}
static void runTesterFunc(PyObject*, PyObject*) {
static MyClass& myObj = MyClass::getInstance();
myObj.tester();
}
static PyObject* getModule() {
Py_SetPythonHome(L"C:\\Python36");
Py_SetProgramName(L"my_program");
PyMethodDef pyModuleMethods[] = {
{ "runTester", (PyCFunction)runTesterFunc, METH_VARARGS | METH_KEYWORDS, "Foo Boo." },
{ NULL, NULL, 0, NULL }
};
static PyModuleDef MyModule = {
PyModuleDef_HEAD_INIT,
"my_module",
"Foo Boo.",
-1,
pyModuleMethods,
NULL
};
return PyModule_Create(&MyModule);
}
void initializePython() {
PyImport_AppendInittab("my_module", getModule);
Py_Initialize();
}
};
int main() {
static MyClass& myObj = MyClass::getInstance();
myObj.initializePython();
PyRun_SimpleString(
"import my_module \n"
"my_module.runTester() \n"
);
return 0;
}
问题是当我在PyRun_SimpleString
中运行my_module.runTester()
时。我收到以下错误消息:
SystemError: Bad call flags in PyCFunction_Call. METH_OLDARGS is no longer supported!
我没有使用METH_OLDARGS
。如您所见,我使用的是METH_VARARGS | METH_KEYWORDS
。我可能会错过什么?你知道吗
我使用的是python3.6和windows7x64。你知道吗
以下修改解决了问题:
相关问题 更多 >
编程相关推荐