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