class foo: pass 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)
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.make_instance(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 **"
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 **"
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.dumps(u) #print x #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 gnosis.xml.pickle as xml_pickle from gnosis.xml.pickle.util import setInBody from types import * class Foo: def __init__(self,s): self.s = s class WeirdUni(unicode): 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.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 "* 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"
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 #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)"
# test binary pickling import gnosis.xml.pickle as xml_pickle import gzip from StringIO import StringIO # --- Make an object to play with --- class C: pass o = C() o.lst, o.dct = [1,2], {'spam':'eggs'} x = xml_pickle.dumps(o,1) # make sure xml_pickle really gzipped it sio = StringIO(x) gz = gzip.GzipFile('dummy','rb',9,sio) if gz.read(5) != '<?xml': raise "ERROR(1)" # reload object o2 = xml_pickle.loads(x) # check it if o.lst != o2.lst or o.dct != o2.dct: raise "ERROR(2)" print "** OK **"
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) __disable_extensions() x1 = xml_pickle.dumps(foo) #print x1 #print "---POST-PICKLE---" bar = xml_pickle.loads(x1) #printfoo(bar) #bar.hi() 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
funcs.set_parser() # handcoded object to load Foo (i.e. without the pickler having # seen it before) ud_xml = """<?xml version="1.0"?> <!DOCTYPE PyObject SYSTEM "PyObjects.dtd"> <PyObject module="__main__" class="Foo"> </PyObject> """ class myfoo: pass class Foo: pass # print "On-the-fly -- SHOULD *NOT* SEE MODULE NAME IN XML" p = xml_pickle.loads(ud_xml) # print it so we can see the modname #print "Fullname = "+str(p) if str(p.__class__) != 'gnosis.xml.pickle.util._util.Foo': raise "ERROR(1)" # dump and make sure modname doesn't stick s = xml_pickle.dumps(p) #print s if re.search(s,'module'): raise "ERROR(2)" #print "From (old) xml_pickle namespace -- SHOULD *NOT* SEE MODULE NAME IN XML" # put Foo into xml_pickle namespace xml_pickle.Foo = myfoo p = xml_pickle.loads(ud_xml)
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 **"
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.dumps(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.dumps((True, False)) if SHOW_XML: print s x = xmp.loads(s) # print "Expect True, False: ",x[0],x[1]
self.a = False 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:
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:
print "Size of gzip'd xml_pickle: ", len(xml_pickle.dumps(o,1)) #----------------------------------------------------------------------- # Second: look at actual compressed and uncompressed (small) pickles #----------------------------------------------------------------------- o = C() o.lst = [1,2] o.dct = {'spam':'eggs'} print "--------------- Uncompressed xml_pickle: ---------------" print xml_pickle.dumps(o), print "--------------- Compressed xml_pickle: -----------------" print `xml_pickle.dumps(o,1)` #----------------------------------------------------------------------- # Third: make sure compressed pickle makes it through restore cycle #----------------------------------------------------------------------- print "------------------------------------------------------" try: xml_pickle.loads(xml_pickle.dumps(o)) 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!"
# s = pickle.dumps(f) x = xml_pickle.dumps(f) g = pickle.loads(s) testfoo(f, g) # have to do this here, otherwise "m" gets recreated # with an on-the-fly class that breaks regular pickle # (as it should --- pickle shouldn't know about xml_pickle.foo) # we could just not reuse m, but this is kind of a neat # way to show what's going on, and how PARANOIA works xml_pickle.setParanoia(0) m = xml_pickle.loads(x) testfoo(f, m) # now test dump/load to/from a file fh = open('aaa', 'w') pickle.dump(m, fh) fh.close() fh = open('bbb', 'w') xml_pickle.dump(m, fh) fh.close() fh = open('aaa', 'r') g = pickle.load(fh) fh.close()