How can I get a pointer to the data inside a generic PyObject*?

  boost-python, c++, python

I’m trying to get the memory address of data held within a PyObject* (from Python.h 3.8.2 specifically) so I can do a memcpy to a buffer. I’ve only been able to figure out how to copy the data out of the object but nothing on just getting the pointer. Say I have this object data

PyObject* data = PyLong_FromLong(100L);

As of now it seems my only option to get this data over to the buffer is to copy it out and then do a memcpy using the address of the temporary variable …

long temp = PyLong_AsLong(data);
memcpy(buffer, &temp, 8);

This is being done thousands and thousands of times so I would assume it’d be faster if I’m able to get the memory address of the data and directly copy that over to my buffer like …

memcpy(buffer, data->address_to_data(), 8)

instead of having that extra copy to the temporary variable.

Does anyone know if / how I can get the memory address of the long value from the PyObject* wrapper?

Appreciate the help!

Source: Windows Questions C++

LEAVE A COMMENT