Pickling C objects

Ovidiu Predescu ovidiu at cup.hp.com
Wed Jul 14 14:14:28 EDT 1999


Stuart Reynolds wrote:

> I'm writing some new Python classes with a C implementation. How do you
> make a C object pickleable?
> 
> Is it possible to use the __repr__ method? It would be easy for me to
> output some Python code as a string which generated the object:
> 
>  'MyClass( MyComponent1(1,2,3), MyOtherComponent2('a','b','c') )'

You need to register your new C type extension with the pickle module.
Take a look in the copy_reg module in the Python distribution to see how
this works.

As an example I have written a new C type entension, WeakReference that
keeps a reference to a Python object without incrementing the reference
count (this is useful for avoiding cycles). Here is the initialization
function of the module:

void initWeakReference ()
{
  char pickling[] = "\
from copy_reg import *\n\
from WeakReference import *\n\
def pickleWeakReference (ref):\n\
    return WeakReference, (ref.__object__,)\n\
w = WeakReference (1)\n\
pickle (type (w), pickleWeakReference, WeakReference)\n";

  Py_InitModule ("WeakReference", WeakReferenceType_methods);
  if (PyErr_Occurred())
    Py_FatalError ("can't initialize module WeakReference");

  if (PyRun_SimpleString (pickling) == -1)
    Py_FatalError ("can't initialize module WeakReference for
pickling");
}

As I mentioned, the only member that I need to pickle is the object,
which is internally kept in a WeakReference as __object__. The
pickleWeakReference function returns a tuple with one element, the
object. If your C type has more members just add them to the tuple.

Hope this helps.

Greetings,
-- 
Ovidiu Predescu <ovidiu at cup.hp.com>
http://andromeda.cup.hp.com/  (inside HP's firewall only)
http://www.geocities.com/SiliconValley/Monitor/7464/




More information about the Python-list mailing list