By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,944 Members | 1,819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,944 IT Pros & Developers. It's quick & easy.

pickling objects in jython

P: n/a
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?

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

a = A(5,"abcd")

print "Before Pickling: %s"%str(a)
mfile = open("dumptest","wb")

mfile = open("dumptest","rb")
m = cPickle.load(mfile)
print "After Pickling: %s"%str(m)
Sep 7 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.