f = foo() f.a = (1, 2, 3) # method 1 -- StreamWriter is an uncompressed StringIO x = xml_pickle.dumps(f) # check header (to ensure correct method used) + contents #if x[0:5] == '<?xml': #print "OK" #else: #print "ERROR" #sys.exit(1) g = xml_pickle.loads(x) if g.a == (1, 2, 3): print "OK" else: print "ERROR" sys.exit(1) # method 2 -- StreamWriter is a compressed StringIO #x = xml_pickle.dumps(f,1) ## check header + contents #if x[0:2] == '\037\213': #print "OK" #else: #print "ERROR" #sys.exit(1)
foo.l.append(re.compile('[qrs]+[1-9]?')) foo.ul = UserList.UserList() foo.ul.append(re.compile('[+\-][0-9]+')) foo.ul.append(re.compile('(bored yet)?')) foo.tup = (re.compile('this is [not]? '), re.compile('a [tuple|list]')) #print "---PRE-PICKLE---" #printfoo(foo) x1 = xml_pickle.dumps(foo) #print x1 #print "---POST-PICKLE---" bar = xml_pickle.loads(x1) #print str(dir(bar)) #printfoo(bar) checkfoo(foo, bar) # same thing on copy x2 = xml_pickle.dumps(bar) baz = xml_pickle.loads(x2) checkfoo(bar, baz) #print "---XML from original---" #print x1 #print "---XML from copy---"
self.f = a_test_function self.k = a_test_class # always show the family tag so I can make sure it's right #setVerbose(1) #setParanoia(0) f = foo() # dump an object containing bools s = xmp.dumpsp(f) if SHOW_XML: print s x = xmp.loads(s) #print "Expect False, True, None, func, class: ",x.a,x.b,x.c,x.f,x.k # check it for attr in ['a', 'b', 'c', 'f', 'k']: if getattr(f, attr) != getattr(x, attr): raise "ERROR(1)" # dump builtin obj containing bools s = xmp.dumpsp((True, False)) if SHOW_XML: print s x = xmp.loads(s) #print "Expect True, False: ",x[0],x[1]
self.b = True self.c = None self.f = a_test_function self.k = a_test_class # If this Python doesn't have True/False, then the unpickler # will return 1/0 instead #if not pyconfig.Have_TrueFalse(): # True = 1 # False = 0 # the tests are portable versions of those in test_bools.py # bools inside an object x = xmp.loads(x1) # check it if x.a != False or x.b != True or x.c != None or \ x.f != a_test_function or x.k != a_test_class: #print x.__dict__ raise "ERROR(1)" # bools inside a toplevel bltin x = xmp.loads(x2) if x[0] != True or x[1] != False: raise "ERROR(2)" # bool as toplevel x = xmp.loads(x3) if x != True:
foo.l.append(date.DateTime(2006, 7, 8, 9, 10, 11.12)) foo.ul = UserList.UserList() foo.ul.append(date.DateTime(2007, 8, 9, 10, 11, 12.13)) foo.ul.append(date.DateTime(2008, 9, 10, 11, 12, 13.14)) foo.tup = (date.DateTime(2009, 10, 11, 12, 13, 14.15), date.DateTime(2010, 11, 12, 13, 14, 15.16)) #print "---PRE-PICKLE---" #printfoo(foo) x1 = xml_pickle.dumps(foo) #print x1 #print "---POST-PICKLE---" bar = xml_pickle.loads(x1) #printfoo(bar) testfoo(foo, bar) x2 = xml_pickle.dumps(bar) #print "---XML from original---" #print x1 #print "---XML from copy---" #print x2 print "** OK **"
import funcs #funcs.set_parser() class foo: pass #xml_pickle.setParanoia(0) f = foo() f.b = test_ftypes_i.gimme_bfunc() f.p = test_ftypes_i.gimme_pfunc() f.f = foo print f.b, f.p, f.f x = xml_pickle.dumps(f) print x g = xml_pickle.loads(x) print g.b, g.p, g.f # check it for attr in ['b','p','f']: if getattr(f,attr) != getattr(g,attr): raise "ERROR(1)" print "** OK **"
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 #print d x = xml_pickle.dumps(d) #print x g = xml_pickle.loads(x) #print g # check values & ref if g['a'] != 1 or id(g['b']) != id(g): raise "ERROR(2)"
import zif.sedna.persistence.pickle as xml_pickle class Test: pass o1 = Test() o1.s = "o1" o2 = Test() o2.s = "o2" o1.obj1 = o2 o1.obj2 = o2 o2.obj3 = o1 o2.obj4 = o1 xml = xml_pickle.dumps(o1) #print xml z = xml_pickle.loads(xml) # check it if z.s != o1.s or z.obj1.s != o1.obj1.s or \ z.obj2.s != o1.obj2.s or z.obj1.obj3.s != o1.obj1.obj3.s or \ z.obj2.obj4.s != o1.obj2.obj4.s: raise "ERROR(1)" print "** OK **"
raise "ERROR(%d)" % NR x = Foo(1, 'abc', [1, 2, 3], ('a', 'b', 'c')) # python-pickle p = pickle.dumps(x) x2 = pickle.loads(p) # test: .a and .b should match x; .c and .d should match # values from __getinitargs__ check_foo(x, x2, 1) # xml-pickle px = xml_pickle.dumpsp(x) x3 = xml_pickle.loads(px) print px print x3.a, x3.b, x3.c, x3.d print x2.a, x2.b, x2.c, x2.d # test: .a and .b should match x; .c and .d should match # values from __getinitargs__ check_foo(x, x3, 2) # make sure all call counts are correct # 1 __init__ per loads(), plus original object = 3 # 1 __getinitargs__ per dumps(), plus 2 extra calls per test = 6 if COUNT_INIT != 3 or COUNT_GETARGS != 6: raise "ERROR(3)"
import lxml.objectify as xo import zif.sedna.persistence.pickle as xp from StringIO import StringIO import funcs #funcs.set_parser() xml = '''<?xml version="1.0"?> <!DOCTYPE Spam SYSTEM "spam.dtd" > <Spam> <Eggs>Some text about eggs.</Eggs> <MoreSpam>Ode to Spam</MoreSpam> </Spam> ''' fh = StringIO(xml) o = xo.fromstring(xml) s = xp.dumps(o) #print "---------- xml_objectify'd object, xml_pickle'd ----------" #print s o2 = xp.loads(s) #print "---------- object after the pickle/unpickle cycle --------" #print xp.dumps(o2) if o.Eggs.PCDATA != o2.Eggs.PCDATA or \ o.MoreSpam.PCDATA != o2.MoreSpam.PCDATA: raise "ERROR(1)" print "** OK **"
#----------------------------------------------------------------------- o = C() o.lst = [1, 2] o.dct = {'spam': 'eggs'} s = xml_pickle.dumps(o) print "--------------- Uncompressed xml_pickle: (%s bytes)---------------" % len( s) print s s = xml_pickle.dumps(o, 1) print "--------------- Compressed xml_pickle: (%s bytes)-----------------" % len( s) print ` s ` #----------------------------------------------------------------------- # Third: make sure compressed pickle makes it through restore cycle #----------------------------------------------------------------------- print "------------------------------------------------------" try: xml_pickle.loads(xml_pickle.dumps(o, 1)) print "Pickle/restore cycle with compression: OK" except: print "Pickle/restore cycle with compression: FAILED!" try: xml_pickle.loads('CORRUPT' + xml_pickle.dumps(o)) print "Pickle/restore corrupt data with compression: OK" except: print "Pickle/restore corrupt data with compression: FAILED!"
#funcs.set_parser() xml = '''<?xml version="1.0"?> <!DOCTYPE Spam SYSTEM "spam.dtd" > <Spam> <Eggs>Some text about eggs.</Eggs> <MoreSpam>Ode to Spam</MoreSpam> </Spam> ''' fh = StringIO(xml) o = xo.fromstring(xml) s = xp.dumps(o) #print "---------- xml_objectify'd object, xml_pickle'd ----------" #print s o2 = xp.loads(s) #print "---------- object after the pickle/unpickle cycle --------" #print xp.dumps(o2) if o.Eggs.PCDATA != o2.Eggs.PCDATA or \ o.MoreSpam.PCDATA != o2.MoreSpam.PCDATA: raise "ERROR(1)" print "** OK **"
#raise "ERROR(5)" ## dump & make sure modname doesn't stick #s = xml_pickle.dumps(p) ##print s #if re.search('module',s): #raise "ERROR(6)" ## remove so it won't be found again below #xml_pickle.remove_class_from_store('Foo') ## now, make sure we haven't broken modnames showing up when they SHOULD :-) ##print "My namespace (__main__) -- SHOULD SEE MODULE NAME IN XML" ## allow it to load my Foo #xml_pickle.setParanoia(0) p = xml_pickle.loads(ud_xml) # print it so we can see the modname #print "Fullname = "+str(p) if str(p.__class__) != '__main__.Foo': raise "ERROR(7)" # dump & make sure module name is written s = xml_pickle.dumps(p) print s if not re.search('module="__main__"', s): raise "ERROR(8)" if not re.search('class="Foo"', s): raise "ERROR(9)" print "** OK **"
ustring = u"Alef: %s, Omega: %s" % (unichr(1488), unichr(969)) pstring = "Only US-ASCII characters" estring = "Only US-ASCII with line breaks\n\tthat was a tab" class C: def __init__(self, ustring, pstring, estring): self.ustring = ustring self.pstring = pstring self.estring = estring o = C(ustring, pstring, estring) #-- Try standard pickling cycle (default setInBody() settings) #print '\n------------* Pickle with Python and Unicode strings *------------------' xml = dumps(o) #print xml, #print '------------* Restored attributes from different strings *--------------' o2 = loads(xml) # check types explicitly, since comparison will coerce types if not isinstance(o2.ustring,UnicodeType): raise "AAGH! Didn't get UnicodeType" if not isinstance(o2.pstring,StringType): raise "AAGH! Didn't get StringType for pstring" if not isinstance(o2.estring,StringType): raise "AAGH! Didn't get StringType for estring" #print "UNICODE:", `o2.ustring`, type(o2.ustring) #print "PLAIN: ", o2.pstring, type(o2.pstring) #print "ESCAPED:", o2.estring, type(o2.estring) if o.ustring != o2.ustring or \ o.pstring != o2.pstring or \ o.estring != o2.estring:
# show we do the same thing sp = pickle.dumps(f) sx = xml_pickle.dumpsp(f) #del f f=None #import time #time.sleep(1) #j = open('setstate_xml.xml','w') #j.write(sx) #j.close() print sx g = xml_pickle.loads(sx) h = pickle.loads(sp) #print sx print t1, t2, t3, t4 p1 = g.r.random() p2 = g.l1[1].random() p3 = g.l2[0].random() p4 = g.d['Two'].random() print p1,p2,p3,p4 if t1!=p1 or t2!=p2 or t3!=p3 or t4!=p4: raise "ERROR(1)" #print t1, t2, t3, t4
#from gnosis.xml.pickle.util import setInBody from types import * class Foo: def __init__(self,s): self.s = s class WeirdUni(unicode): pass # def __init__(self,s): # unicode.__init__(self,s) # show that I didn't screw up normal strings f = Foo('OK') x = xml_pickle.dumps(f) o = xml_pickle.loads(x) print o.s, type(o.s) f = Foo(u'OK') x = xml_pickle.dumps(f) o = xml_pickle.loads(x) print o.s, type(o.s) f = Foo(WeirdUni(u'OK')) x = xml_pickle.dumps(f) o = xml_pickle.loads(x) print o.s, type(o.s) # pickler should catch all these unpickleable cases. # (to be fixed in gnosis 1.2.x)
o1.a != o2.a or o1.a.a != o2.a.a or \ o1.a.b != o2.a.b or \ o1.a.zz != o2.a.zz: raise "ERROR(1)" # # test all coredata+attr classes # #print "* LCOMBO" x = top() x.a = lcombo([4,5,6],1,2) x.a.zz = 10 #print x.a, x.a.a, x.a.b, x.a.zz s = xml_pickle.dumpsp(x) print s g = xml_pickle.loads(s) #print g.a, g.a.a, g.a.b, g.a.zz check_combo(x,g) #print "* DCOMBO" x = top() x.a = dcombo({'a':1,'b':2,'c':3},1,2) x.a.zz = 10 #print x.a, x.a.a, x.a.b, x.a.zz s = xml_pickle.dumps(x) #print s g = xml_pickle.loads(s) #print g.a, g.a.a, g.a.b, g.a.zz check_combo(x,g) #print "* TCOMBO"
def unpickling(): obj = xml_pickle.loads(thepickle)
foo.ul = UserList.UserList() foo.ul.append( date.DateTime(2007,8,9,10,11,12.13) ) foo.ul.append( date.DateTime(2008,9,10,11,12,13.14) ) foo.tup = (date.DateTime(2009,10,11,12,13,14.15), date.DateTime(2010,11,12,13,14,15.16)) #print "---PRE-PICKLE---" #printfoo(foo) x1 = xml_pickle.dumps(foo) #print x1 #print "---POST-PICKLE---" bar = xml_pickle.loads(x1) #printfoo(bar) testfoo(foo,bar) x2 = xml_pickle.dumps(bar) #print "---XML from original---" #print x1 #print "---XML from copy---" #print x2 print "** OK **"
class C: def __init__(self, ustring, pstring, estring): self.ustring = ustring self.pstring = pstring self.estring = estring o = C(ustring, pstring, estring) #-- Try standard pickling cycle (default setInBody() settings) #print '\n------------* Pickle with Python and Unicode strings *------------------' xml = dumps(o) #print xml, #print '------------* Restored attributes from different strings *--------------' o2 = loads(xml) # check types explicitly, since comparison will coerce types if not isinstance(o2.ustring, UnicodeType): raise "AAGH! Didn't get UnicodeType" if not isinstance(o2.pstring, StringType): raise "AAGH! Didn't get StringType for pstring" if not isinstance(o2.estring, StringType): raise "AAGH! Didn't get StringType for estring" #print "UNICODE:", `o2.ustring`, type(o2.ustring) #print "PLAIN: ", o2.pstring, type(o2.pstring) #print "ESCAPED:", o2.estring, type(o2.estring) if o.ustring != o2.ustring or \ o.pstring != o2.pstring or \ o.estring != o2.estring:
def unpickling(): obj = xml_pickle.loads(thepickle)
o2.d != o1.__getinitargs__()[3]: raise "ERROR(%d)" % NR x = Foo(1,'abc',[1,2,3],('a','b','c')) # python-pickle p = pickle.dumps(x) x2 = pickle.loads(p) # test: .a and .b should match x; .c and .d should match # values from __getinitargs__ check_foo(x, x2, 1) # xml-pickle px = xml_pickle.dumpsp(x) x3 = xml_pickle.loads(px) print px print x3.a, x3.b, x3.c, x3.d print x2.a, x2.b, x2.c, x2.d # test: .a and .b should match x; .c and .d should match # values from __getinitargs__ check_foo(x, x3, 2) # make sure all call counts are correct # 1 __init__ per loads(), plus original object = 3 # 1 __getinitargs__ per dumps(), plus 2 extra calls per test = 6 if COUNT_INIT != 3 or COUNT_GETARGS != 6: raise "ERROR(3)"
c = {'a': 1, 'b': 2, 'c': 3, 'd': [100, 200, 300]} dd = c['d'] # make sure subitems get refchecked uu = UserList([10, 11, 12]) u = UserList([[uu, c, b, a], [a, b, c, uu], [c, a, b, uu], dd]) #print u # allow xml_pickle to read our namespace #setParanoia(0) # by default, with references x1 = xml_pickle.dumpsp(u) print x1 #del u g = xml_pickle.loads(x1) #print g if u != g: raise "ERROR(1)" # next, using DEEPCOPY #print "------ DEEP COPY ------" #setDeepCopy(1) x2 = xml_pickle.dumps(g) #print x #del g z = xml_pickle.loads(x2) # deepcopy version should be significantly larger
self.c = None self.f = a_test_function self.k = a_test_class # always show the family tag so I can make sure it's right #setVerbose(1) #setParanoia(0) f = foo() # dump an object containing bools s = xmp.dumpsp(f) if SHOW_XML: print s x = xmp.loads(s) #print "Expect False, True, None, func, class: ",x.a,x.b,x.c,x.f,x.k # check it for attr in ['a','b','c','f','k']: if getattr(f,attr) != getattr(x,attr): raise "ERROR(1)" # dump builtin obj containing bools s = xmp.dumpsp( (True,False) ) if SHOW_XML: print s x = xmp.loads( s ) #print "Expect True, False: ",x[0],x[1]
# Second: look at actual compressed and uncompressed (small) pickles #----------------------------------------------------------------------- o = C() o.lst = [1,2] o.dct = {'spam':'eggs'} s = xml_pickle.dumps(o) print "--------------- Uncompressed xml_pickle: (%s bytes)---------------" % len(s) print s s = xml_pickle.dumps(o,1) print "--------------- Compressed xml_pickle: (%s bytes)-----------------" % len(s) print `s` #----------------------------------------------------------------------- # Third: make sure compressed pickle makes it through restore cycle #----------------------------------------------------------------------- print "------------------------------------------------------" try: xml_pickle.loads(xml_pickle.dumps(o,1)) print "Pickle/restore cycle with compression: OK" except: print "Pickle/restore cycle with compression: FAILED!" try: xml_pickle.loads('CORRUPT'+xml_pickle.dumps(o)) print "Pickle/restore corrupt data with compression: OK" except: print "Pickle/restore corrupt data with compression: FAILED!"
foo.ul.append( re.compile('[+\-][0-9]+') ) foo.ul.append( re.compile('(bored yet)?') ) foo.tup = ( re.compile('this is [not]? '), re.compile('a [tuple|list]') ) #print "---PRE-PICKLE---" #printfoo(foo) # turn off extensions so that SREs will be saved as rawpickles #__disable_extensions() x1 = xml_pickle.dumps(foo) #print x1 #print "---POST-PICKLE---" bar = xml_pickle.loads(x1) #printfoo(bar) checkfoo(foo,bar) # same thing on copy x2 = xml_pickle.dumps(bar) baz = xml_pickle.loads(x2) checkfoo(bar,baz) #print "---XML from original---" #print x1 #print "---XML from copy---" #print x2
c = {'a':1,'b':2,'c':3,'d':[100,200,300]} dd = c['d'] # make sure subitems get refchecked uu = UserList([10,11,12]) u = UserList([[uu,c,b,a],[a,b,c,uu],[c,a,b,uu],dd]) #print u # allow xml_pickle to read our namespace #setParanoia(0) # by default, with references x1 = xml_pickle.dumpsp(u) print x1 #del u g = xml_pickle.loads(x1) #print g if u != g: raise "ERROR(1)" # next, using DEEPCOPY #print "------ DEEP COPY ------" #setDeepCopy(1) x2 = xml_pickle.dumps(g) #print x #del g z = xml_pickle.loads(x2) # deepcopy version should be significantly larger
import zif.sedna.persistence.pickle as xml_pickle class Test: pass o1 = Test() o1.s = "o1" o2 = Test() o2.s = "o2" o1.obj1 = o2 o1.obj2 = o2 o2.obj3 = o1 o2.obj4 = o1 xml = xml_pickle.dumps(o1) #print xml z = xml_pickle.loads(xml) # check it if z.s != o1.s or z.obj1.s != o1.obj1.s or \ z.obj2.s != o1.obj2.s or z.obj1.obj3.s != o1.obj1.obj3.s or \ z.obj2.obj4.s != o1.obj2.obj4.s: raise "ERROR(1)" print "** OK **"
o2.tup2 = o2.tup o2.num = 2 + 2j o2.dct = {"this": "that", "spam": "eggs", 3.14: "about PI"} o2.dct2 = o2.dct o.obj = o2 #print '------* Print python-defined pickled object *-----' # pickle it s = xml_pickle.dumpsp(o) #print '------* Load it and print it again *-----' #print '------ should look approximately the same ---' print s # unpickle it #for attr in ['num','str','lst','lst2','dtm','date']: #print attr, getattr(o,attr) t = xml_pickle.loads(s) # sanity, can't possibly happen if id(o) == id(t) or id(o.obj) == id(t.obj): raise "ERROR(0)" # check that it is the same for attr in [ 'num', 'str', 'lst', 'lst2', 'dtm', 'children', 'married', 'divorced', 'c_names', 'pet_names', 'date', 'dtm' ]: if getattr(o, attr) != getattr(t, attr): #print getattr(o,attr), getattr(t,attr) raise "ERROR(1)" for attr in ['tup', 'tup2', 'num', 'dct', 'dct2']:
o2.tup2 = o2.tup o2.num = 2+2j o2.dct = { "this": "that", "spam": "eggs", 3.14: "about PI" } o2.dct2 = o2.dct o.obj = o2 #print '------* Print python-defined pickled object *-----' # pickle it s = xml_pickle.dumpsp(o) #print '------* Load it and print it again *-----' #print '------ should look approximately the same ---' print s # unpickle it #for attr in ['num','str','lst','lst2','dtm','date']: #print attr, getattr(o,attr) t = xml_pickle.loads(s) # sanity, can't possibly happen if id(o) == id(t) or id(o.obj) == id(t.obj): raise "ERROR(0)" # check that it is the same for attr in ['num','str','lst','lst2','dtm','children','married','divorced', 'c_names','pet_names','date','dtm']: if getattr(o,attr) != getattr(t,attr): #print getattr(o,attr), getattr(t,attr) raise "ERROR(1)" for attr in ['tup','tup2','num','dct','dct2']: getattr(t.obj,attr) if getattr(o.obj,attr) != getattr(t.obj,attr):
#raise "ERROR(5)" ## dump & make sure modname doesn't stick #s = xml_pickle.dumps(p) ##print s #if re.search('module',s): #raise "ERROR(6)" ## remove so it won't be found again below #xml_pickle.remove_class_from_store('Foo') ## now, make sure we haven't broken modnames showing up when they SHOULD :-) ##print "My namespace (__main__) -- SHOULD SEE MODULE NAME IN XML" ## allow it to load my Foo #xml_pickle.setParanoia(0) p = xml_pickle.loads(ud_xml) # print it so we can see the modname #print "Fullname = "+str(p) if str(p.__class__) != '__main__.Foo': raise "ERROR(7)" # dump & make sure module name is written s = xml_pickle.dumps(p) print s if not re.search('module="__main__"',s): raise "ERROR(8)" if not re.search('class="Foo"',s): raise "ERROR(9)" print "** OK **"