【问题标题】:Python C module function argument reference countingPython C 模块函数参数引用计数
【发布时间】:2012-04-11 04:00:41
【问题描述】:

当我有一个从PyArg_ParseTuple 获得的PyObject * 时,我是否需要在从函数返回之前确保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.
}

【问题讨论】:

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


    【解决方案1】:

    没有。 PyArg_ParseTuple 给你一个borrowed reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 2022-11-21
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      相关资源
      最近更新 更多