Search code examples
pythonpython-c-apipybuffer

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


If it makes a difference, I am interested in an answer regarding Python 3.

The docs state (here and here) that PyBuffer_Release() should be called after PyArg_Parse*() with s*, y*.

Nothing of the sort is written about Py_BuildValue(). Is it an oversight, or in case of Py_BuildValue() a simple Py_DECREF() is enough?

Here is my specific case:

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);

Solution

  • I think no:

    1. In both the PyArg_Parse* and Py_BuildValue functions, y# refers to a string and length, rather than a buffer, and therefore there is no underlying buffer object to be released.

    2. The documentation for Py_BuildValue says:

      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().

      The purpose of holding a lock on the buffer used by PyArg_Parse* is that you have got a reference to some data out of Python to C, and you want to process it in C without any chance of it being modified by Python. In this case you have copied some data from C into Python and so there's no need to protect the original data from modification.