473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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()
==============================================
Sep 7 '05 #1
0 1263

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Maurice Ling | last post by:
Hi, I have read that this had been asked before but there's no satisfactory replies then. I have a module (pA) written in python, which originally is called by another python module (pB), and...
8
by: Hans Georg Krauthaeuser | last post by:
Dear all, I have a long running application (electromagnetic compatibility measurements in mode-stirred chambers over GPIB) that use pickle (cPickle) to autosave a class instance with all the...
2
by: Kirk Strauser | last post by:
I have a module that defines a Search class and a SearchResult class. I use these classes by writing other modules that subclass both of them as needed to interface with particular search engines....
9
by: Alex | last post by:
I have a serious problem and I hope there is some solution. It is easier to illustrate with a simple code: >>> class Parent(object): __slots__= def __init__(self, a, b): self.A=a; self.B=b...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.