def doit3(): class foo: pass f = foo() f.a = 1 f.b = f.a f.c = "abc" f.d = f.c f.r = re.compile('aaa\s+[0-9]*') f.r2 = f.r l = [1,2,3] f.u = UserList([l,l]) x = xml_pickle.dumps(f) print(x) g = thing_from_sax2(None,x) print(f.u, f.r.pattern, f.r2.pattern)
def doit3(): class foo: pass f = foo() f.a = 1 f.b = f.a f.c = "abc" f.d = f.c f.r = re.compile('aaa\s+[0-9]*') f.r2 = f.r l = [1,2,3] f.u = UserList([l,l]) x = xml_pickle.dumps(f) print x g = thing_from_sax2(None,x) print f.u, f.r.pattern, f.r2.pattern
def convert(self, output): self.logger.debug('Running conversion to %s' % output) outfile = '%s.%s' % (self.input.split('.')[0], output) data = None with open(self.input, 'r') as infile: if output == 'xml': data = dumps(load(infile.read())) elif output == 'json': data = load(infile) if not data: self.logger.debug('Error with parsing %s data' % output) sys.exit(1) with open(outfile, "w") as out: if output == 'xml': out.write(data) elif output == 'json': out.write(json.dumps(data, sort_keys=True, indent=4))
return mobj.obj # test1 -- use our custom handler to pickle & unpickle # (here we fold two types to a single tagname) print "*** TEST 1 ***" my1 = mystring(StringType,"MyString",in_body=1) my2 = mystring(UnicodeType,"MyString",in_body=1) mutate.add_mutator(my1) mutate.add_mutator(my2) u = UserList(['aaa','bbb','ccc']) print u x = xml_pickle.dumps(u) print x del u z = xml_pickle.loads(x) print z # remove custom mutators mutate.remove_mutator(my1) mutate.remove_mutator(my2) # # test 2 -- custom pickler, but builtin unpickler # # use same tagname as builtin so builtin can unpickle it
funcs.set_parser() #-- Create some unicode and python strings (and an object that contains them) 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 o1.__class__ != o2.__class__ or \ 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)
import gnosis.xml.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 **"
def __init__(cls, name, bases, dict): from cPickle import dumps super(MetaPyPickler, cls).__init__(name, bases, dict) setattr(cls, 'dumps', lambda self: dumps(self))
funcs.set_parser() a = (1,2,3) b = [4,5,6] 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
self.a = False self.b = True 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.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
import gnosis.xml.objectify as xo import gnosis.xml.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.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 **"
o = C() from os import sep fname = sep.join(xml_pickle.__file__.split(sep)[:-1]) + sep + '_pickle.py' o.lst = open(fname).readlines() o.tup = tuple(o.lst) o.dct = {} for i in range(500): o.dct[i] = i print("Size of standard cPickle: ", len(pickle.dumps(o))) print("Size of binary cPickle: ", len(pickle.dumps(o, 1))) print("gzip'd standard cPickle: ", len(compress(pickle.dumps(o)))) print("gzip'd binary cPickle: ", len(compress(pickle.dumps(o, 1)))) print("Size of standard xml_pickle: ", len(xml_pickle.dumps(o))) 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), end=' ') print("--------------- Compressed xml_pickle: -----------------") print(repr(xml_pickle.dumps(o, 1))) #-----------------------------------------------------------------------
o = C() from os import sep fname = sep.join(xml_pickle.__file__.split(sep)[:-1]) + sep + '_pickle.py' o.lst = open(fname).readlines() o.tup = tuple(o.lst) o.dct = {} for i in range(500): o.dct[i] = i print "Size of standard cPickle: ", len(cPickle.dumps(o)) print "Size of binary cPickle: ", len(cPickle.dumps(o, 1)) print "gzip'd standard cPickle: ", len(compress(cPickle.dumps(o))) print "gzip'd binary cPickle: ", len(compress(cPickle.dumps(o, 1))) print "Size of standard xml_pickle: ", len(xml_pickle.dumps(o)) 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) ` #-----------------------------------------------------------------------
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> ''' print "===================== DOM VERSION ========================" fh = StringIO(xml) o = xo.XML_Objectify(fh, 'DOM').make_instance() 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) print "==================== EXPAT VERSION =======================" fh = StringIO(xml) o = xo.XML_Objectify(fh, 'EXPAT').make_instance() 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)
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 d = {'a':1} d['b'] = d print d x = xml_pickle.dumps(d) #print x g = xml_pickle.loads(x) print g # pickle builtins as toplevel objects (the self-ref is actually # inside the wrapper for these cases)
from gnosis.xml.pickle.util import setParanoia import funcs funcs.set_parser() setParanoia(0) def checkit(o1,o2): for attr in ['first','last','addr','city','state']: if getattr(o1,attr) != getattr(o2,attr): raise "ERROR(1)" c = contact('Joe','Jones','1744 Elk Road','Manchester','NH') #c.hi() x = xml_pickle.dumps(c) #print x #del c d = xml_pickle.loads(x) #d.hi() checkit(c,d) # just to show this is a legal pickleable object ... import pickle s = pickle.dumps(d) #del d q = pickle.loads(s) #q.hi()
foo.l = [] foo.l.append( date.DateTime(2005,6,7,8,9,10.11) ) 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
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) # print it so we can see the modname #print "Fullname = "+str(p) if str(p.__class__) != '__main__.myfoo': raise "ERROR(3)" # dump it and make sure modname doesn't stick s = xml_pickle.dumps(p)
foo.l = [] foo.l.append(date.DateTime(2005, 6, 7, 8, 9, 10.11)) foo.l.append(date.DateTime(2006, 7, 8, 9, 10, 11.12)) foo.ul = collections.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
def check_combo(o1,o2): if o1.__class__ != o2.__class__ or \ 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)
o = C() from os import sep fname = sep.join(xml_pickle.__file__.split(sep)[:-1])+sep+'_pickle.py' o.lst = open(fname).readlines() o.tup = tuple(o.lst) o.dct = {} for i in range(500): o.dct[i] = i print "Size of standard cPickle: ", len(cPickle.dumps(o)) print "Size of binary cPickle: ", len(cPickle.dumps(o,1)) print "gzip'd standard cPickle: ", len(compress(cPickle.dumps(o))) print "gzip'd binary cPickle: ", len(compress(cPickle.dumps(o,1))) print "Size of standard xml_pickle: ", len(xml_pickle.dumps(o)) 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)` #-----------------------------------------------------------------------
import gzip, cStringIO import gnosis.xml.pickle as xml_pickle # --- Make an object to play with --- class C: pass o = C() o.lst, o.dct = [1, 2], {'spam': 'eggs'} s = xml_pickle.gzdumps(o) print '------------- Progressively compressed pickle -------------' print ` s ` print '------------- Uncompressed gzstream -----------------------' print xml_pickle.ungz_string(s) print '------------- Restore gzstream to object ------------------' o2 = xml_pickle.gzloads(s) print xml_pickle.dumps(o2)
def doit2(): u = UserList([1,2,[(3,4,5),(6,7,8)],3,UserList([0,1,2]),4]) x = xml_pickle.dumps(u) print x g = thing_from_sax2(None,x) print g
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, str): raise Exception("AAGH! Didn't get str") if not isinstance(o2.pstring, str): raise Exception("AAGH! Didn't get StringType for pstring") if not isinstance(o2.estring, StringType): raise Exception("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)
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 #print d x = xml_pickle.dumps(d) #print x g = xml_pickle.loads(x) #print g # check values & ref
<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) # print it so we can see the modname #print "Fullname = "+str(p) if str(p.__class__) != '__main__.myfoo': raise "ERROR(3)" # dump it and make sure modname doesn't stick s = xml_pickle.dumps(p)
def __init__(cls, name, bases, dict): from pickle import dumps super(MetaPyPickler, cls).__init__(name, bases, dict) setattr(cls, 'dumps', lambda self: dumps(self))
funcs.set_parser() a = (1,2,3) b = [4,5,6] 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 x = xml_pickle.dumps(u) print x del u g = xml_pickle.loads(x) print g # next, using DEEPCOPY print "------ DEEP COPY ------" setDeepCopy(1) x = xml_pickle.dumps(g) print x del g print xml_pickle.loads(x)
funcs.set_parser() setParanoia(0) def checkit(o1, o2): for attr in ['first', 'last', 'addr', 'city', 'state']: if getattr(o1, attr) != getattr(o2, attr): print("ERROR(1)") raise c = contact('Joe', 'Jones', '1744 Elk Road', 'Manchester', 'NH') #c.hi() x = xml_pickle.dumps(c) #print x #del c d = xml_pickle.loads(x) #d.hi() checkit(c, d) # just to show this is a legal pickleable object ... import pickle s = pickle.dumps(d) #del d q = pickle.loads(s) #q.hi()
import gnosis.xml.pickle.ext as mutate from collections import UserList from . 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
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.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 **"
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 **"
# being pickled correctly. # let it load my classes xml_pickle.setParanoia(0) if pyconfig.Have_ObjectClass(): # need new-style classes class PickleMeOld: def __init__(self): pass class PickleMeNew(object): def __init__(self): pass class Container(object): def __init__(self, klass): self.classRef = klass x = xml_pickle.dumps(Container(PickleMeOld)) o = xml_pickle.loads(x) if o.classRef != PickleMeOld: print("ERROR(1)") raise x = xml_pickle.dumps(Container(PickleMeNew)) o = xml_pickle.loads(x) if o.classRef != PickleMeNew: print("ERROR(2)") raise print("** OK **")
# this is from a bug report from Even Westvang <*****@*****.**>. # the problem is that new-style classes used as attributes weren't # being pickled correctly. # let it load my classes xml_pickle.setParanoia(0) if pyconfig.Have_ObjectClass(): # need new-style classes class PickleMeOld: def __init__(self): pass class PickleMeNew(object): def __init__(self): pass class Container(object): def __init__(self, klass): self.classRef = klass x = xml_pickle.dumps(Container(PickleMeOld)) o = xml_pickle.loads(x) if o.classRef != PickleMeOld: raise "ERROR(1)" x = xml_pickle.dumps(Container(PickleMeNew)) o = xml_pickle.loads(x) if o.classRef != PickleMeNew: raise "ERROR(2)" print "** OK **"
def doit2(): u = UserList([1,2,[(3,4,5),(6,7,8)],3,UserList([0,1,2]),4]) x = xml_pickle.dumps(u) print(x) g = thing_from_sax2(None,x) print(g)
self.a = False self.b = True 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.dumps(f) print s x = xmp.loads(s) print "Expect False, True, None, func, class: ", x.a, x.b, x.c, x.f, x.k # dump builtin obj containing bools s = xmp.dumps((True, False)) print s x = xmp.loads(s) print "Expect True, False: ", x[0], x[1] # dump bool itself as toplevel obj s = xmp.dumps(True) print s
# 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 **"
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(str): def __init__(self,s): str.__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('OK') x = xml_pickle.dumps(f) o = xml_pickle.loads(x) print(o.s, type(o.s)) f = Foo(WeirdUni('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)
# test binary pickling import gnosis.xml.pickle as xml_pickle import gzip from io 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': print("ERROR(1)") raise # reload object o2 = xml_pickle.loads(x) # check it if o.lst != o2.lst or o.dct != o2.dct: print("ERROR(2)") raise print("** OK **")
"exercise all 4 list-writing methods --fpm" import gnosis.xml.pickle as xml_pickle import sys import funcs funcs.set_parser() 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)
o2.c != o1.__getinitargs__()[2] or \ 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.dumps(x) x3 = xml_pickle.loads(px) # 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)" # do a multilevel object to test all three cases # (toplevel, attr, item)