How to save ctypes objects containing pointers

I use a third-party library that returns a ctypes object containing pointers after a lot of calculations.

How to save a ctypes object and what pointers point to for future use? I tried

  • scipy.io.savemat => TypeError: Failed to convert the object to an array
  • cPickle => ctypes objects containing pointers cannot be pickled
+4
source share
3 answers

Python is not able to do this automatically for you: you will need to create code to independently select all the data you need, placing them in a suitable Python data structure (or simply adding data to a unique byte string, where you will be knoe, where each element by its offset) - and they save this object to disk.

This is not a "Python" problem - this is exactly the problem that Python solves for you when you use Python objects and data. When encoding to C or lower, you are responsible for knowing not only your data, but also the length of each piece of data (and allocating memory for each chnk, as well as freeing it when this is done, etc. ) And this is what you need to do in this case.

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

+6
source

You can copy the data into the Python data structure and look up pointers along the way (using the contents attribute of the pointer).

0
source

To break up the ctypes object with pointers, you will need to define your own methods __getstate__ / __reduce__ for pickling and __setstate__ for __setstate__ . See the docs for the pickle module for more information.

0
source

Source: https://habr.com/ru/post/1402206/


All Articles