Eric, please sorry for the confussion, I just had a bug in __reduce__ . Look at the new files attached, your last use case is included in the test script.
__reduce__ returns a 3-tuple: the fist item is the type object, with
pickle sees as a callable, the second is a tuple argument for that
callable, and the third is the state
In general, if you implement __reduce__ like this
def __reduce__(self):
return function, args, state
the at un-pickle time, Python (2.5) does
function, args, state = unpickle(stream)
newobj = function(*args)
newobj.__setstate__(state)
and then 'newobj' is the final recoverd object.
I hope I was clear enough. If you still want to need more complicated
stuff, please just keep coming. All this discussion will surelly help
in the future at the time Cython tries to implement something to
automatically manage all this beast.
On 5/27/08, Eric Jonas <[EMAIL PROTECTED]> wrote:
> To start with, this is awesome. Thank you so much!
>
> Next question: What if I want MyPair to have a zero-argument
> constructor? (Well, besides self, that is). If I do:
>
> class MyPair(pair):
> def __init__(self):
> pass
>
> then I get "TypeError: ('__init__() takes exactly 1 argument (3 given)".
> Does this mean that derived types must always have constructors with the
> same number (or more) than their base type?
>
> Thanks,
>
> ...Eric
>
>
>
>
> On Tue, 2008-05-27 at 15:47 -0300, Lisandro Dalcin wrote:
> > Well, I admit that the Python docs are a bit hard to follow, but let's
> > solve your issue. Look at the attached files,
> >
> > * mod.pyx: a simple 'cdef' class pair, like the C++ one
> > * test.py: a full test, all pickle protocols, includes subclassing
> > * cy2py: my lovely distutils based script for the process *.pyx->*.so
> > (no need of makefiles, improvements welcome!)
> > run like $ python cy2py mod.pyx . This way, you get mod.so.
> >
> > If you find this code useful, you can donate some dollars ... Just a
> > joke! But if you can make it a really good example, It would be great
> > to have this included in Cython docs, in order to help other doing
> > this, I just do not have the time.
> >
> > Enjoy!
> >
> >
> > On 5/27/08, Eric Jonas <[EMAIL PROTECTED]> wrote:
> > >
> > > > But there is definitelly better ways. You sould look at the docs of
> > > > pickle module, and particularly to the __getinitargs__/__getnewargs__
> > > > and the __getstate__/__setstate__ stuff. Iff I were to implement
> > > > pickle protocol, I would explicitely use that.
> > >
> > >
> > >
> > > The python pickle docs, which I've been struggling to understand, seem
> > > to suggest that __reduce__ is necessary for all extensions, or to use
> > > the copy_reg module. But it seems that __getstate__/__setstate__ are not
> > > applicable for c-extensions. Is this correct?
> > >
> > > Thanks,
> > >
> > > ...Eric
> > >
> > >
> > >
> > > _______________________________________________
> > > Cython-dev mailing list
> > > [email protected]
> > > http://codespeak.net/mailman/listinfo/cython-dev
> > >
> >
> >
> > _______________________________________________
> > Cython-dev mailing list
> > [email protected]
> > http://codespeak.net/mailman/listinfo/cython-dev
>
> _______________________________________________
> Cython-dev mailing list
> [email protected]
> http://codespeak.net/mailman/listinfo/cython-dev
>
--
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594
mod.pyx
Description: Binary data
from mod import pair
import cPickle as pkl
p1 = pair()
s1p0 = pkl.dumps(p1, 0)
s1p1 = pkl.dumps(p1, 1)
s1p2 = pkl.dumps(p1, 2)
print pkl.loads(s1p0)
print pkl.loads(s1p1)
print pkl.loads(s1p2)
p2 = pair( "abc", [1,2,3])
s2p0 = pkl.dumps(p2, 0)
s2p1 = pkl.dumps(p2, 1)
s2p2 = pkl.dumps(p2, 2)
print pkl.loads(s2p0)
print pkl.loads(s2p1)
print pkl.loads(s2p2)
class MyPair(pair):
def __init__(self, f=0, s=0):
self.first = f
self.second = s
class MyPairZero(MyPair):
def __init__(self):
super(MyPairZero, self).__init__()
p3 = MyPair()
s3p0 = pkl.dumps(p3, 0)
s3p1 = pkl.dumps(p3, 1)
s3p2 = pkl.dumps(p3, 2)
print pkl.loads(s3p0)
print pkl.loads(s3p1)
print pkl.loads(s3p2)
p4 = MyPair(5,7)
s4p0 = pkl.dumps(p4, 0)
s4p1 = pkl.dumps(p4, 1)
s4p2 = pkl.dumps(p4, 2)
print pkl.loads(s4p0)
print pkl.loads(s4p1)
print pkl.loads(s4p2)
p5 = MyPairZero()
s5p0 = pkl.dumps(p5, 0)
s5p1 = pkl.dumps(p5, 1)
s5p2 = pkl.dumps(p5, 2)
print pkl.loads(s5p0)
print pkl.loads(s5p1)
print pkl.loads(s5p2)
_______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
