Excuse me for the stupid question in advance.
Is it possible to copy a PyObject** objects
doing
memcpy(new_objects, objects, length * sizeof(PyObject))
instead of using the classical for
loop?
Excuse me for the stupid question in advance.
Is it possible to copy a PyObject** objects
doing
memcpy(new_objects, objects, length * sizeof(PyObject))
instead of using the classical for
loop?
Without additional context, no. At a minimum you need to consider refcounts.
This is no different than assigning a PyObject*
to a new pointer so if you make sure you’re upholding the normal invariants with respect to reference counting etc then you should be okay.
Some examples from the interpreter: listobject.c
does this when sorting and dicts when merging keys.
That assumes you meant
memcpy(new_objects, objects, length * sizeof(PyObject*))
(note the star in sizeof(PyObject*)
.
Definitively
Mh. Houston, I have a problem.
The original object was created using PyMem_NEW
. After memcpy
ed, I use PyMem_FREE
to remove the old object, but I get:
Debug memory block at address p=0x7efd20254400: API ‘m’
128 bytes originally requested
The 7 pad bytes at p-7 are FORBIDDENBYTE, as expected.
The 8 pad bytes at tail=0x7efd20254480 are not all FORBIDDENBYTE (0xfd):
at tail+0: 0xc0 *** OUCH
at tail+1: 0x96 *** OUCH
at tail+2: 0x18 *** OUCH
at tail+3: 0x22 *** OUCH
at tail+4: 0xfd
at tail+5: 0x7e *** OUCH
at tail+6: 0x00 *** OUCH
at tail+7: 0x00 *** OUCH
Data at p: c0 92 18 22 fd 7e 00 00 … 80 96 18 22 fd 7e 00 00Enable tracemalloc to get the memory block allocation traceback
Fatal Python error: _PyMem_DebugRawFree: bad trailing pad byte
Python runtime state: initialized
(By the way, the array in question is ma_values
of dict
).