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 later use? I tried
scipy.io.savemat
=> TypeError: Could not convert object to arraycPickle
=> ctypes objects containing pointers cannot be pickledPython 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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With