pickle vs cPickle: error is not raised / windows vs linux?

Irmen de Jong irmen at -NOSPAM-REMOVETHIS-xs4all.nl
Sun Jun 1 09:09:15 CEST 2003


I'm experiencing  a problem with pickle vs cPickle.
The code below prints:
***pickle***
Creating t...
Thing.__init__ name= myname
Pickle t...
Restore t...
FAIL!!!  maximum recursion depth exceeded
***cPickle***
Creating t...
Thing.__init__ name= myname
Pickle t...
Restore t...
SUCCESS!!! name= myname

running with python 2.2/2.3 under windows and linux.

When I enable the __getinitargs__, I also get strange results:
- python 2.2.2 under windows tells me with pickle: FAIL!!!  __main__.Thing is 
not safe for unpickling.  (cPickle works)
- python 2.3b under windows tells me everything is fine
- python 2.2.2 *and* 2.3b under linux tell me everything is fine

(When I enable __setstate__, all cases work.)

My questions:
1) with the unmodified code, why doesn't cPickle raise an error when the 
unpickling gets in an endless loop ?
2) in the second case with __getinitargs__ enabled, why does windows tell me 
the object isn't safe to unpickle, while linux says it's all right?


Thanks for any answers!!

--Irmen


CODE:
------------------------------------
import pickle, cPickle

class Thing:
	def __init__(self,aname):
		print "Thing.__init__ name=",aname
		self.name=aname
	def __getattr__(self, attrname):
		self.name.trim()   # any method on the name attribute..
		raise AttributeError()

# enable the setstate *AND/OR* getinitargs method to make it work for pickle:
#	def __setstate__(self,args):
#		print "SETSTATE! args=",args
#		self.__dict__=args
#	def __getinitargs__(self):
#		print "GETINITARGS!"
#		return ["initname"]

def test(pickle):
	print "Creating t..."
	t=Thing("myname")
	try:
		print "Pickle t..."
		s=pickle.dumps(t)
		print "Restore t..."
		t2=pickle.loads(s)
		print "SUCCESS!!! name=",t2.name
	except Exception,x:
		print "FAIL!!! ",x

print "***pickle***"
test(pickle)

print "***cPickle***"
test(cPickle)





More information about the Python-list mailing list