Pickling an extension type subclasses problems

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Feb 9 11:11:09 CET 2010

En Fri, 05 Feb 2010 11:25:02 -0300, Gaetan de Menten <gdementen at gmail.com>

> I am trying to write an (optional!) C extension for SQLAlchemy, and I
> am struggling to make an extension type picklable *and* using the same
> format as the pure Python version (so that computers with the C
> extension can unpickle pickles from computers without it and
> vice-versa). [...]
> My last hope is to define __reduce__ on the Python
> side too,

That seems reasonable.

> and make it use a module-level "reconstructor" function as
> follow:
> def mypythontype_reconstructor(cls, state):
>     obj = object.__new__(cls, state):
>     ....
>     return obj
> Will this work? Any other idea?

If you're going to call object.__new__(cls, state) anyway, then just
return the object's type as the first item in __reduce__; no need to
create such global function

Gabriel Genellina

More information about the Python-list mailing list