"Simple __slots__ test" import gnosis.xml.pickle as xml_pickle import funcs funcs.set_parser() class foo(object): __slots__ = ('a', 'b') xml_pickle.setParanoia(0) f = foo() f.a = 1 f.b = 2 #print f.a, f.b s = xml_pickle.dumps(f) #print s g = xml_pickle.loads(s) #print g.a, g.b if g.__class__ != foo or g.a != f.a or g.b != f.b: raise "ERROR(1)" print "** OK **"
### we print type+value to make sure unpickling really worked ##def printfoo(obj): ## print type(obj.s1), obj.s1 ## print type(obj.s2), obj.s2 ## print type(obj.f), obj.f ## print type(obj.i), obj.i, type(obj.i2), obj.i2 ## print type(obj.li), obj.li ## print type(obj.j), obj.j ## print type(obj.n), obj.n ## print type(obj.d), obj.d['One'], obj.d['Two'], obj.d['Three'] ## print type(obj.l), obj.l[0], obj.l[1], obj.l[2] ## print type(obj.tup), obj.tup[0], obj.tup[1], obj.tup[2] xml_pickle.setParanoia(0) # allow it to use our namespace foo = foo_class() # try putting numeric content in body (doesn't matter which # numeric type) setInBody(ComplexType, 1) # test both code paths # path 1 - non-nested ('attr' nodes) foo.s1 = "this is a \" string with a ' in it" foo.s2 = 'this is a \' string with a " in it' foo.f = 123.456 foo.i = 789 foo.i2 = 0 # zero was a bug in 1.0.1
turned into more of a test of pickling self-referencing objects (toplevels are still tested, however). --fpm""" import gnosis.xml.pickle as xml_pickle import random, re, sys from gnosis.xml.pickle.ext import XMLP_Mutator, XMLP_Mutated import gnosis.xml.pickle.ext as mutate from UserList import UserList import funcs funcs.set_parser() class foo: pass # so we can unpickle foo xml_pickle.setParanoia(0) # test the obvious self-refs l = [1,2] l.append(l) #print l x = xml_pickle.dumps(l) #print x g = xml_pickle.loads(x) #print g # check values & ref if g[0] != l[0] or g[1] != l[1] or id(g[2]) != id(g): raise "ERROR(1)" d = {'a':1} d['b'] = d
# OTOH, we want all mutated objects that match our tag # ('userlist'), so do nothing for 'wants_mutated()' def mutate(self,obj): return XMLP_Mutated(obj.data, "%s.%s"%(util._module(obj),util._klass(obj))) def unmutate(self,mobj): p = mobj.extra.split('.') klass = util.get_class_from_name(p[1],p[0], xml_pickle.getParanoia()) return klass(mobj.obj) print "*** TEST 4 ***" xml_pickle.setParanoia(0) # let xml_pickle use our namespace my1 = mutate_userlist() mutate.add_mutator(my1) class mylist(UserList):pass class zlist(mylist): pass class foo:pass f = foo() f.u = UserList([1,2,3,4]) f.m = mylist([5,6,7,8]) f.z = zlist([9,10,11,12]) print f.u.__class__, f.u
raise "ERROR(1)" ### we print type+value to make sure unpickling really worked ##def printfoo(obj): ## print type(obj.s1), obj.s1 ## print type(obj.s2), obj.s2 ## print type(obj.f), obj.f ## print type(obj.i), obj.i, type(obj.i2), obj.i2 ## print type(obj.li), obj.li ## print type(obj.j), obj.j ## print type(obj.n), obj.n ## print type(obj.d), obj.d['One'], obj.d['Two'], obj.d['Three'] ## print type(obj.l), obj.l[0], obj.l[1], obj.l[2] ## print type(obj.tup), obj.tup[0], obj.tup[1], obj.tup[2] xml_pickle.setParanoia(0) # allow it to use our namespace foo = foo_class() # try putting numeric content in body (doesn't matter which # numeric type) setInBody(ComplexType,1) # test both code paths # path 1 - non-nested ('attr' nodes) foo.s1 = "this is a \" string with a ' in it" foo.s2 = 'this is a \' string with a " in it' foo.f = 123.456 foo.i = 789 foo.i2 = 0 # zero was a bug in 1.0.1