Py_BuildValue("y#", ...) 之后是否需要 PyBuffer_Release?

Is PyBuffer_Release required after Py_BuildValue("y#", ...)?

如果有所不同,我对有关 Python 3.

的答案很感兴趣

文档声明 (here and here) PyBuffer_Release() 应该在 PyArg_Parse*() 之后用 s*y*.

调用

没有关于 Py_BuildValue() 的文章。这是一个疏忽,还是在 Py_BuildValue() 的情况下 simple Py_DECREF() 就足够了?

这是我的具体案例:

uint8_t buf = (uint8_t *)malloc(bufSize);
PyObject *pyBuf = Py_BuildValue("y#", (char *)buf, bufSize);
free(buf);

// do something with pyBuf

// maybe a PyBuffer_Release(get_underlying_buffer(pyBuf)) here?
Py_DECREF(pyBuf);

我认为没有:

  1. PyArg_Parse*Py_BuildValue函数中,y#指的是字符串和长度,而不是缓冲区,因此没有底层缓冲区要释放的对象。

  2. Py_BuildValue 的文档说:

    When memory buffers are passed as parameters to supply data to build objects, as for the s and s# formats, the required data is copied. Buffers provided by the caller are never referenced by the objects created by Py_BuildValue().

    锁定 PyArg_Parse* 使用的缓冲区的目的是您已经 引用 某些数据 out 的 Python 到 C,并且您想在 C 中处理它,而不想让它被 Python 修改。在这种情况下,您已经将一些数据从 C 复制 到 Python 中,因此无需保护原始数据免遭修改。