Skip to content Skip to sidebar Skip to footer

How To Save Ctypes Objects Containing Pointers

I use a 3rd party library which returns after a lot of computation a ctypes object containing pointers. How can I save the ctypes object and what the pointers are pointing to for l

Solution 1:

Python has no way of doing that automatically for you:

You will have to build code to pick all the desired Data yourself, putting them in a suitable Python data structure (or just adding the data in a unique bytes-string where you will know where each element is by its offset) - and then save that object to disk.

This is not a "Python" problem - it is exactly a problem Python solves for you when you use Python objects and data. When coding in C or lower level, you are responsible to know not only where your data is, but also, the length of each chunk of data (and allocate memory for each chunk, and free it when done, and etc). And this is what you have to do in this case.

Your data structure should give you not only the pointers, but also the length of the data in each pointed location (in a way or the other - if the pointer is to another structure, "size_of" will work for you)

Solution 2:

To pickle a ctypes object that has pointers, you would have to define your own __getstate__/__reduce__ methods for pickling and __setstate__ for unpickling. More information in the docs for pickle module.

Solution 3:

You could copy the data into a Python data structure and dereference the pointers as you go (using the contents attribute of a pointer).

Post a Comment for "How To Save Ctypes Objects Containing Pointers"