PyObject_CallMethod返回Null,提供简单示例

时间:2018-04-09 04:05:54

标签: python c++

我是一名初学Python程序员,致力于将一些Python代码嵌入到C ++ DLL中。无法使PyObject_CallMethod工作,每次调用Python 3.6类方法时它返回Null。创建了一个小例子来说明问题,请参阅代码中的注释。会对我做错了什么的指导表示赞赏。

    #include "stdafx.h"

using namespace std;

int TestBasicStats()
{
    _putenv_s("PYTHONPATH", ".");

    Py_Initialize();

    PyObject* module = PyImport_ImportModule("BasicStats");
    assert(module != NULL); // Returned non-null object

    PyObject* MyPyClass = PyObject_GetAttrString(module, "BasicStats");
    assert(MyPyClass != NULL);// Returned non-null object

    PyObject* myClassInstance = PyInstanceMethod_New(MyPyClass);
    assert(myClassInstance != NULL);// Returned non-null object

    PyObject* result = PyObject_CallMethod(myClassInstance, "AddItem", "(i)", 1);
    assert(result != NULL); // Failed - returned NULL

    result = PyObject_CallMethod(myClassInstance, "AddItem", "(i)", 2);
    assert(result != NULL); // Failed - returned NULL

    result = PyObject_CallMethod(myClassInstance, "get_Max", NULL);
    assert(result != NULL); // Failed - returned NULL

    printf("Min = %ld\n", PyLong_AsLong(result));
    printf("Min = %f\n", PyFloat_AsDouble(result));

    Py_Finalize();

    return 0;
}


int main()
{
    TestBasicStats();

    return 0;
}

调用的Python类如下:

class BasicStats:
def __init__(self):
    self._m_min = self._m_max = self._m_sum = self._m_sumSqr = 0
    self._m_count = 0
    self.Reset()

def Reset(self):
    self._m_min = float("inf")
    self._m_max = float("-inf")
    self._m_sum = self._m_sumSqr = 0
    self._m_count = 0

def AddItem(self, value):
    self._m_count += 1
    if self._m_max < value:
        self._m_max = value

def get_Max(self):
    return self._m_max

Max = property(fget=get_Max)

2 个答案:

答案 0 :(得分:1)

这里我不明白的是PyInstanceMethod_New()电话。我真的不确定它在做什么,因为我从来没有在我的任何代码中使用过它。从PyObject调用获得的PyObject_GetAttrString()应该是类型对象,因此是可调用的,可以调用它来创建该类型的新对象。因此,要创建一个BasicStats对象,我将把类型对象称为函数:

PyObject* myClassInstance = PyObject_CallFunctionObjArgs(myPyClass, NULL);

答案 1 :(得分:0)

我相信

PyObject* myClassInstance = PyInstanceMethod_New(MyPyClass);

应该是

// should call __init__. assumes __init__() has arity of 1
// otherwise you will have to marshall correct arguments
PyObject* myClassInstance = PyObject_CallObject(MyPyClass, 0);