python: pickling c objects

独自空忆成欢 提交于 2019-12-10 12:37:18

问题


First off I'm not expecting a solution, just hoping for some pointers on how to start.

I've got a C program with an embedded Python interpreter. The Python scripts the program uses as input obviously refer to the C-defined objects and functions. I'd now like to make some of these objects pickleable.

The pickle docs describe how extension types can be made picklable using __reduce__. But this is a Python method - how would I define this in the underlying PyObject?

Fairly sure I'm mis-understanding something...


回答1:


The pickle module comes in both a python-only and a C variant (called cPickle). As such, the __reduce__ method needs to be callable from Python code.

Thus, you need to provide a __reduce__ entry in your C object PyMethodDef struct with a suitable implementation.

Alternatively, you can also register a pickling function with the copy_reg module. This module's original usecase was to support extension modules better; the source code for the module states:

This is only useful to add pickle support for extension types defined in C, not for instances of user-defined classes.



来源:https://stackoverflow.com/questions/12530333/python-pickling-c-objects

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