def testDict () : rec = { }; rec["recursive"] = rec results = [ ( {}, 'P\x00\x00\x00\x00\x01\x01 t\x00\x00\x00\x00'), ( {'a':1, 'b':2.0, 'c':'three'},'P\x00\x00\x00\x00\x01\x01 t\x03\x00\x00\x00a\x01\x00\x00\x00aS\x01a\x01\x00\x00\x00ca\x05\x00\x00\x00threea\x01\x00\x00\x00bd\x00\x00\x00\x00\x00\x00\x00@'), ( {'a':1, 'b':{}, 'c': {'a':1, 'b':3.0 }}, 'P\x00\x00\x00\x00\x01\x01 t\x03\x00\x00\x00a\x01\x00\x00\x00aS\x01a\x01\x00\x00\x00cP\x01\x00\x00\x00\x01\x01 t\x02\x00\x00\x00a\x01\x00\x00\x00aS\x01a\x01\x00\x00\x00bd\x00\x00\x00\x00\x00\x00\x08@a\x01\x00\x00\x00bP\x02\x00\x00\x00\x01\x01 t\x00\x00\x00\x00'), # (rec, 'P\x00\x00\x00\x00\x01\x01 t\x01\x00\x00\x00a\t\x00\x00\x00recursiveP\x00\x00\x00\x00'), ] engine(results, "testDict") if not checking_roundtrip : return d = { } dd = {'a':d, 'b':d, 'c':d } ser = ocdumps(dd) res = ocloads(ser) if res==dd and res['a'] is res['b'] and res['b'] is res['c']: print '.. preserved structure for', dd, " and ", res else : print "***ERROR! Didn't preserve structure for", dd, " and ", res ser = ocdumps(rec) res = ocloads(ser) if len(rec)==1 and len(res)==1 and type(rec)==type(res) and res["recursive"] is res : print '.. preserved recursive structure for', dd, " and ", res else : print "***ERROR! Didn't preserve recursive structure for", dd, " and ", res
def testTuple () : results = [ ( (1,2.0, "three"),'P\x00\x00\x00\x00\x01\x01 uZ\x03\x00\x00\x00S\x01d\x00\x00\x00\x00\x00\x00\x00@a\x05\x00\x00\x00three'), ( (), "P\x00\x00\x00\x00\x01\x01 uZ\x00\x00\x00\x00") ] engine(results, "testTuple") if not checking_roundtrip : return d = () dd = (d, d, d) ser = ocdumps(dd) res = ocloads(ser) if res==dd and res[0] is res[1] and res[1] is res[2]: print '.. preserved structure for', dd, " and ", res else : print "***ERROR! Didn't preserve structure for", dd, " and ", res di = { } rec = (di,) di["recursive"] = rec ser = ocdumps(rec) res = ocloads(ser) if len(rec)==1 and len(res)==1 and type(rec)==type(res) and res[0]["recursive"] is res : print '.. preserved recursive structure for', rec, " and ", res else : print "***ERROR! Didn't preserve recursive structure for", rec, " and ", res
def testArr() : l = [1,2,3,4] results = [ ([1, 2.0, "three"], "P\x00\x00\x00\x00\x01\x01 nZ\x03\x00\x00\x00S\x01d\x00\x00\x00\x00\x00\x00\x00@a\x05\x00\x00\x00three"), ([], 'P\x00\x00\x00\x00\x01\x01 nZ\x00\x00\x00\x00'), ([[],1],'P\x00\x00\x00\x00\x01\x01 nZ\x02\x00\x00\x00P\x01\x00\x00\x00\x01\x01 nZ\x00\x00\x00\x00S\x01' ), ([l,l,l], 'P\x00\x00\x00\x00\x01\x01 nZ\x03\x00\x00\x00P\x01\x00\x00\x00\x01\x01 nZ\x04\x00\x00\x00S\x01S\x02S\x03S\x04P\x01\x00\x00\x00P\x01\x00\x00\x00'), ] engine(results, "testArr") # checking shape kept ll = [l,l, l] ser = ocdumps(ll) res = ocloads(ser) if res==ll and res[0] is res[0] and res[1] is res[2]: print '.. preserved structure for', ll, " and ", res else : print "***ERROR! Didn't preserve structure for", ll, " and ", res if not checking_roundtrip : return crazy = [] crazy.append(crazy) ser = ocdumps(crazy) print '*******************', repr(ser) res = ocloads(ser) if len(crazy)==1 and len(res)==1 and type(res)==type(crazy) and crazy[0] is crazy : print '.. preserved recursive structure for', crazy, " and ", res else : print "***ERROR! Didn't preserve recursive structure for", crazy, " and ", res
def testBigString () : a = "1"*(2**32) if len(a) != 2**32 : print "***ERROR! Expected a very long string!" else : print '.. ok len for long string' ser = ocdumps(a) expected_hdr = "A\x00\x00\x00\x00\x01\x00\x00\x00" if ser[:9] != expected_hdr : print "***ERROR! Header on long string is wrong! Saw:", repr(a[:9]), " but expected:", repr(expected_hdr) else : print '.. ok header on long string' if ser[9:] != a : print '***ERROR! Serialized big string wrong' sys.exit(1) else : print '.. ok string serialized correctly' if checking_roundtrip : # NOW check that we can deserialize and the same back! deser = ocloads(ser) if deser != a : print "***ERROR! didn't get back big string" else : print '.. ok for big string'
def packageData_(self, raw_data) : """Helper function to pack data (Pickled, raw, etc.). and return the unpacked data""" if self.serialization == SERIALIZE_NONE : data = str(raw_data) elif self.serialization == SERIALIZE_OC : data = ocdumps(raw_data) else : data = cPickle.dumps(raw_data, abs(self.serialization)) return data
def packageData_(self, raw_data): """Helper function to pack data (Pickled, raw, etc.). and return the unpacked data""" if self.serialization == SERIALIZE_NONE: data = str(raw_data) elif self.serialization == SERIALIZE_OC: data = ocdumps(raw_data) else: data = cPickle.dumps(raw_data, abs(self.serialization)) return data
def testVERYLARGENumPyArray () : if numpy is None : print " .. can't import numpy .. " return dd = numpy_test.big_arr() print '.. ok dd is big:', len(dd) ser = ocdumps(dd) print '.. ok Was able to serialize a very big array: len(ser)=', len(ser) res = ocloads(ser) print '.. ok Was able to deserialize very big array: len(res)=', len(res) if len(dd)==len(res) and (dd[0]==res[0]).all() : print '.. worked for big array' else : print "***ERROR! Didn't work for big array"
def engine (results, mesg, equals=eq) : print "---------------", mesg for value, result in results : trial = ocdumps(value) if trial != result : print '***ERROR! Expected', value, ' to dumps as\n', repr(result),' not\n', repr(trial) else : if checking_roundtrip : # NOW check that we can deserialize and the same back! # print type(trial), trial deser = ocloads(trial, 0, check_what_we_can_import()) if not equals(deser, value) : print '***ERROR! value started as ',value,' and came back as ', deser, " type(original value)=", type(value), " type(return value)=",type(deser) else : print '.. ok for', repr(value), ":", repr(deser) else : print '.. ok for', value, ":", repr(result)
def testNumPyArray () : if numpy is None : print " .. can't import numpy .. " return # Defer evaluation in case your version of Python can't handle numpy results = numpy_test.results engine(results, "testNumPyAreray", numpyeq) if not checking_roundtrip : return d= numpy.array([1,2,3], 'i') dd = (d, d, d) ser = ocdumps(dd) res = ocloads(ser) if len(dd)==len(res) and (dd[0]==res[0]).all() and res[0] is res[1] and res[1] is res[2]: print '.. preserved structure for', dd, " and ", res else : print "***ERROR! Didn't preserve structure for", dd, " and ", res
# Time host fast protocol 0 vs. 2 is proto = int(arg[-1]) dumped = pickle.dumps(t, proto) for x in xrange(0,times): loader = pickle.loads(dumped) # if loader != t : print("not same??") elif arg in ["pickle0", "pickle1", "pickle2"] : proto = int(arg[-1]) for x in xrange(0,times) : dumped = pickle.dumps(t, proto) elif arg in ["ocdumps", "ocloads"]: if pyocser is None : print 'ocdumps and ocloads not available: make sure you build pyocser' sys.exit(1) if arg == "ocdumps" : for x in xrange(0, times) : dumped = pyocser.ocdumps(t) elif arg == "ocloads": dumped = pyocser.ocdumps(t) for x in xrange(0, times) : loader = pyocser.ocloads(dumped) else : print 'Unknown?'
elif arg in ["unpickle0", "unpickle1", "unpickle2"]: # Time host fast protocol 0 vs. 2 is proto = int(arg[-1]) dumped = pickle.dumps(t, proto) for x in xrange(0, times): loader = pickle.loads(dumped) # if loader != t : print("not same??") elif arg in ["pickle0", "pickle1", "pickle2"]: proto = int(arg[-1]) for x in xrange(0, times): dumped = pickle.dumps(t, proto) elif arg in ["ocdumps", "ocloads"]: if pyocser is None: print 'ocdumps and ocloads not available: make sure you build pyocser' sys.exit(1) if arg == "ocdumps": for x in xrange(0, times): dumped = pyocser.ocdumps(t) elif arg == "ocloads": dumped = pyocser.ocdumps(t) for x in xrange(0, times): loader = pyocser.ocloads(dumped) else: print 'Unknown?'