Robert Bradshaw | 2 Feb 2011 02:55
Favicon

Re: Cython + Pickle: Can't pickle <type ...> it's not found as ...

On Tue, Feb 1, 2011 at 12:01 PM, Carl Vondrick <cvondrick <at> gmail.com> wrote:
> I'm trying to pickle one of my Cython classes. It's defined like:
>
> cdef class Box(object):
>   def __init__(self, a):
>      self.a = a
>
>   def __reduce__(self):
>       return (Box, (self.a,))
>
> But, when I pickle this object, I get the error:
>
> a = Box(1)
> pickle.dump(a, open("foo", "w"))
>
> I get the error: Can't pickle <type 'module.Box'>: it's not found as
> module.Box
>
> For what it's worth, the Box class is defined in an external module
> installed via setuptools.
>
> Any idea what's going on? As far as I can tell, the __reduce__ method
> isn't even being called.

No idea, that works fine for me (well, inserting "cdef object a" into
Box's definition first).

- Robert

-------------------------------------------------------------------

import pickle

cdef class Box(object):
  cdef a
  def __init__(self, a):
     self.a = a

  def __reduce__(self):
      return (Box, (self.a,))

  def __repr__(self):
      return "Box(%s)" % self.a

a = Box(1)
print "a", a
s = pickle.dumps(a)
print "s", s
print pickle.loads(s)


Gmane