pickling objects in jython



Hi,
The following sample code is to pickle and unpickle an object. It works fine with CPython, but the unpickling fails in Jython and I receive an error stating that "A" is unsafe to unpickle (even though I believe I have the code to make "A" safe for unpickling). What do I do wrong and how can I fix it?
Thanks,


==============================================
import sys
import cPickle
import copy_reg

class A(object):
    __slots__ = ("x","y")
    __safe_for_unpickling__ = True
    def __init__(self, a, b):
        self.x = a
        self.y = b
    def __str__(self):
        return str(self.__getstate__())
    def __reduce__(self):
        return (self.__class__.__name__, self.__getstate__())
    def __new__(cls, a, b):
        return object.__new__(cls)
    def __getnewargs__(self):
        return self.__getstate__()
    def __getstate__(self):
        return (self.x, self.y)
    def __setstate__(self, state):
        (self.x, self.y) = state

copy_reg.constructor(A)
a = A(5,"abcd")

print "Before Pickling: %s"%str(a)
mfile = open("dumptest","wb")
cPickle.dump(a,mfile,-1)
mfile.close()

mfile = open("dumptest","rb")
m = cPickle.load(mfile)
print "After Pickling: %s"%str(m)
mfile.close()
==============================================
.



Relevant Pages

  • Re: pickle and infinity
    ... # unpickle can't handle nan/inf/ind floats, ... def myload_float: ... raise ValueError, "Don't know what to do with "+`s` ... # unpickle routine that overrides the float load method ...
    (comp.lang.python)
  • Pickling object inherited from dict
    ... If I pickle a class which inherits from dict, ... I don't get the same data again, if I unpickle ... def test_pickle: ...
    (comp.lang.python)
  • IE Automation/Recorder - Watir/Wet
    ... the the code below to capture HTML events by extending the sample code ... Sample code you will find relevant ... def navigate ... puts "You Navigated to the following URLs: ...
    (comp.lang.ruby)
  • Timeout error--newbie needs help, please
    ... I'm trying to interface with NewsGator's API, ... I'm using their sample code: ... def loadLocations ... but when I execute this (with the username, password, and ...
    (comp.lang.ruby)
  • Re: State Machines in Python
    ... Check out the following sample code and see if it gives you some ideas. ... def state1: ... return state1 ... state machine which is small enough to code by hand. ...
    (comp.lang.python)