Reputation: 2391
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...
Upvotes: 11
Views: 2023
Reputation: 1122332
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.
Upvotes: 5