Python C模块函数参数引用计数

时间:2012-03-27 15:27:10

标签: python c python-module python-c-api cpython

当我从PyObject *获得PyArg_ParseTuple时,在从函数返回之前,是否需要确保Py_DECREF

示例:

static PyObject * modulefunc(PyObject * self, PyObject * args) {
    PyObject * obj;
    if (!PyArg_ParseTuple(args, "O", &obj)) {
        return NULL;
    }

    if (!PyObject_TypeCheck(obj, expected_type_ptr)) {
        // Do I need to Py_DECREF(obj) here?
        PyErr_SetString(PyExc_TypeError, "First argument is not expected type.");
        return NULL;
    }

    // ... rest of function implementation.
}

1 个答案:

答案 0 :(得分:1)

没有。 PyArg_ParseTuple为您提供borrowed reference