How to save ctypes objects containing pointers

匆匆过客 提交于 2019-12-21 17:52:13

问题


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

回答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)




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/9768218/how-to-save-ctypes-objects-containing-pointers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!