Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 later use? I tried

  • scipy.io.savemat => TypeError: Could not convert object to array
  • cPickle => ctypes objects containing pointers cannot be pickled
like image 902
Framester Avatar asked Mar 19 '12 10:03

Framester


2 Answers

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)

like image 168
jsbueno Avatar answered Nov 04 '22 15:11

jsbueno


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.

like image 1
lonetwin Avatar answered Nov 04 '22 16:11

lonetwin