【问题标题】:Can't call a method from a module built in Python C API无法从 Python C API 中内置的模块调用方法
【发布时间】:2019-04-08 13:50:46
【问题描述】:

我正在尝试使用 Python C API 在 C++ 中构建一个 python 模块,代码如下:

#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:PyCFunction_Call 中的错误调用标志。 METH_OLDARGS 为否 支持时间更长!

我没有使用METH_OLDARGS。如您所见,我使用的是METH_VARARGS | METH_KEYWORDS。我在这里可能缺少什么?

我使用的是 Python 3.6 和 Windows 7 x64。

【问题讨论】:

    标签: python c++ python-3.x


    【解决方案1】:

    以下修改解决了这个问题:

    static PyMethodDef pyModuleMethods[] = {
        { "runTester", (PyCFunction)runTesterFunc, METH_VARARGS | METH_KEYWORDS, "Foo Boo." },
        { NULL, NULL, 0, NULL }
    };
    
    static struct PyModuleDef MyModule = {
        PyModuleDef_HEAD_INIT,
        "my_module",
        "Foo Boo.",
        -1,
        pyModuleMethods,
        NULL
    };
    

    【讨论】:

      猜你喜欢
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 2015-11-25
      • 2015-12-19
      • 1970-01-01
      • 2015-07-11
      • 1970-01-01
      相关资源
      最近更新 更多