Question

I am reading binary data from a file in python and trying to send that data to a c module. In python the data is read like so

file = open("data", "rb")
data = file.read()

I want the data as a pointer to a buffer and a length in c if possible. I am using PyArg_ParseTuple to get the parameters in the c module. I noticed in python 3+ there is a y/y*/y# format specifier for binary data but I need the equivalent way of doing it in python 2.7.

Thanks

Était-ce utile?

La solution

You should investigate the Buffer Api.

From the docs:

These functions can be used by an object to expose its data in a raw, byte-oriented format. Clients of the object can use the buffer interface to access the object data directly, without needing to copy it first.

Two examples of objects that support the buffer interface are strings and arrays. The string object exposes the character contents in the buffer interface’s byte-oriented form. An array can also expose its contents, but it should be noted that array elements may be multi-byte values.

For example (in C++):

void* ExtractBuffer(PyObject* bufferInterfaceObject, Py_buffer& bufferStruct)
{
    if (PyObject_GetBuffer(bufferInterfaceObject, &bufferStruct, PyBUF_SIMPLE) == -1)
        return 0;

    return (void*)bufferStruct.buf;
}

Do not forget to release the bufferStruct when you no longer need it:

PyBuffer_Release(&bufferStruct);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top