def test_pickle_frame(): # get the current stack frame (the one we are in) # storing a reference to the frame in the frame's own locals creates a cyclic reference f1 = sys._getframe() s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1.f_code.co_code == f2.f_code.co_code def func(): loc1 = 2 * 2 loc2 = 'Hello' + ' ' + 'World' s = pf.pickle_object(sys._getframe()) loc3 = 'Should not be in frame snapshot' loc4 = 'Should not be in frame snapshot' return s s = func() f2 = pf.unpickle_object(s) assert f2.f_code.co_code == func.func_code.co_code assert len(f2.f_locals) == 2 assert f2.f_locals['loc1'] == 4 assert f2.f_locals['loc2'] == 'Hello World' print "Frame pickling OK!"
def test_pickle_staticmethod(): class TestClass(object): def __new__(cls): ret = object.__new__(cls) ret.myattr = 'Hello World' return ret @staticmethod def test_static_method(a): return "staticmethod with arg %s" % repr(a) s1 = staticmethod(TestClass.test_static_method) assert repr(s1)[1:].startswith('staticmethod') s = pf.pickle_object(s1) s2 = pf.unpickle_object(s) assert repr(s2)[1:].startswith('staticmethod') c1 = TestClass s = pf.pickle_object(c1) c2 = pf.unpickle_object(s) assert c1.test_static_method(123) == c2.test_static_method(123) inst = c2() assert inst.myattr == 'Hello World' print "staticmethod pickling OK!"
def test_pickle_dict(): d1 = {} s = pf.pickle_object(d1) d2 = pf.unpickle_object(s) assert d1 == d2 a = "same", d1 = { "Hello" : "World", "Apple" : "Orange", a : a, 99 : 12356, 100 : (1, 2, 3), 101 : {0 : {}, 1 : {}} } s = pf.pickle_object(d1) d2 = pf.unpickle_object(s) assert d1 == d2 #self referencing d1 = {} d1["key"] = d1 s = pf.pickle_object(d1) d2 = pf.unpickle_object(s) assert d1.__repr__() == d2.__repr__() assert id(d2) == id(d2["key"]) print "Dict pickling OK!"
def test_pickle_weakref(): class TestWeakrefClass(object): def __init__(self, a): self.a = a a = TestWeakrefClass(456123) w1 = weakref.ref(a) s = pf.pickle_object(w1) w2 = pf.unpickle_object(s) # w2 should be 'None' when dereferenced. This makes sense # since when the root of the picked heirarchy is a weak # reference, it alone will not retain the objects it is # referencing. They will be pickled and unpickled, but # will have their reference counting reach 0 during # unpickling assert w2() == None # But if something else retains the referent, the weakref # relationship should be preserved l1 = [a, w1] s = pf.pickle_object(l1) l2 = pf.unpickle_object(s) assert type(l2[1]) == type(w1) assert l2[0] == l2[1]() print "Weakref pickling OK!"
def test_pickle_weak_proxy(): def func(): return 'Hello World' w1 = weakref.proxy(func) assert 'weakcallableproxy' in repr(type(w1)) s = pf.pickle_object(w1) w2 = pf.unpickle_object(s) try: print w2() except ReferenceError: pass else: raise Exception("Unexpected dereferencing possible!") class Wrapper(object): pass obj = Wrapper() wo = weakref.proxy(obj) assert 'weakproxy' in repr(type(wo)) l1 = [func, w1, obj, wo] s = pf.pickle_object(l1) l2 = pf.unpickle_object(s) assert l2[0]() == l2[1]() assert l2[2].__repr__.__self__ == l2[3].__repr__.__self__ print "Weak Proxy pickling OK!"
def test_pickle_exceptions(): e1 = Exception('test123') s = pf.pickle_object(e1) e2 = pf.unpickle_object(s) assert type(e1) == type(e2) assert e1.message == e2.message e1 = StopIteration(123) s = pf.pickle_object(e1) e2 = pf.unpickle_object(s) assert type(e1) == type(e2) assert e1.message == e2.message e1 = BytesWarning(123) s = pf.pickle_object(e1) e2 = pf.unpickle_object(s) assert type(e1) == type(e2) assert e1.message == e2.message e1 = KeyboardInterrupt(123) s = pf.pickle_object(e1) e2 = pf.unpickle_object(s) assert type(e1) == type(e2) assert e1.message == e2.message print "Exception pickling OK!"
def test_pickle_tuple(): t1 = () s = pf.pickle_object(t1) t2 = pf.unpickle_object(s) assert t1 == t2 t1 = (0, 1, 2) s = pf.pickle_object(t1) t2 = pf.unpickle_object(s) assert t1 == t2 t1 = (0, 1, ("Hello", "World", "!", (2, 3, ()))) s = pf.pickle_object(t1) t2 = pf.unpickle_object(s) assert t1 == t2 #Self referencing l = [] t1 = (1, 2, l) l.append(t1) s = pf.pickle_object(t1) t2 = pf.unpickle_object(s) assert t1.__repr__() == t2.__repr__() # Can't compare direcly due to recursive def assert id(t2) == id(t2[2][0]) a = 1 t1 = (a, a, a) s = pf.pickle_object(t1) t2 = pf.unpickle_object(s) assert t1 == t2 assert id(t2[0]) == id(t2[1]) assert id(t2[1]) == id(t2[2]) print "Tuple pickling OK!"
def test_pickle_super(): class Root(object): def foo(self): return "Root.foo" class Left(Root): def foo(self): return "Left.foo-" + super(Left, self).foo() class Right(Root): def foo(self): return "Right.foo-" + super(Right, self).foo() class Final(Left, Right): def foo(self): return "Final.foo-" + super(Final, self).foo() f = Final() s1 = super(Final, f) s = pf.pickle_object(s1) s2 = pf.unpickle_object(s) assert s1.foo() == s2.foo() s1 = super(Left, f) s = pf.pickle_object(s1) s2 = pf.unpickle_object(s) assert s1.foo() == s2.foo() s1 = super(Right, f) s = pf.pickle_object(s1) s2 = pf.unpickle_object(s) assert s1.foo() == s2.foo() print "Super pickling OK!"
def test_pickle_dictviews(): d = { 'a' : 1, 'b' : 2, 'c' : 3, } d1 = d.viewkeys() s = pf.pickle_object(d1) d2 = pf.unpickle_object(s) assert len(d1) == len(d2) for k in d2: assert k in d1 d1 = d.viewvalues() s = pf.pickle_object(d1) d2 = pf.unpickle_object(s) assert len(d1) == len(d2) for k in d2: assert k in d1 d1 = d.viewitems() s = pf.pickle_object(d1) d2 = pf.unpickle_object(s) assert len(d1) == len(d2) for k in d2: assert k in d1 print "dict view pickling OK!"
def test_pickle_generator(): def whole_nums_gen(): num = 0 while True: yield num num += 1 g1 = whole_nums_gen() assert repr(g1)[1:].startswith('generator') assert g1.next() == 0 s = pf.pickle_object(g1) g2 = pf.unpickle_object(s) assert g2.next() == 1 assert g2.next() == 2 def mult_inputs(): # the iterators for the 2 lists will be on the # generator frame's valuestack for i in [1,2]: for j in [9,4]: x = yield yield x * (i + j) g1 = mult_inputs() g1.send(None) assert g1.send(4) == 4*(1+9) g1.next() assert g1.send(2) == 2*(1+4) s = pf.pickle_object(g1) g2 = pf.unpickle_object(s) g2.next() assert g2.send(8) == 8*(2+9) g2.next() assert g2.send(1) == 1*(2+4) try: g2.send(0) except StopIteration: pass else: raise Exception("Unpickled generator yielded unexpected value") s = pf.pickle_object(g2) g2 = pf.unpickle_object(s) try: g2.send(0) except StopIteration: pass else: raise Exception("Unpickled generator yielded unexpected value") print "Generator pickling OK!"
def test_pickle_sys_singleton_namedtuples(): o1 = sys.version_info s = pf.pickle_object(o1) o2 = pf.unpickle_object(s) assert o1 == o2 o1 = sys.flags s = pf.pickle_object(o1) o2 = pf.unpickle_object(s) assert o1 == o2 print "sys singleton named tuple pickling OK!"
def test_pickle_int(): i1 = -259 s = pf.pickle_object(i1) i2 = pf.unpickle_object(s) assert i1 == i2 i1 = 0x556789 s = pf.pickle_object(i1) i2 = pf.unpickle_object(s) assert i1 == i2 print "Int picking OK!"
def test_pickle_bool(): b1 = True s = pf.pickle_object(b1) b2 = pf.unpickle_object(s) assert b1 == b2 b1 = False s = pf.pickle_object(b1) b2 = pf.unpickle_object(s) assert b1 == b2 print "Bool pickling OK!"
def test_pickle_function(): def testfunc(a, b): return a + b f1 = testfunc s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1(1, 3) == f2(1, 3) #test closure def outer(): a = 5 def inner(num): return a + num return inner f1 = outer() s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1(15) == f2(15) #test function decorator def test_decorator(func): def triple(num): return 3 * func(num) return triple @test_decorator def double(num): return 2 * num f1 = double s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1(9) == f2(9) #test pickling default args def testfunc2(num=5, word='test'): return num * word f1 = testfunc2 s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1() == f2() assert f1(num=3) == f2(num=3) print "Function pickling OK!"
def test_pickle_property(): class PropertyClass(object): def __init__(self): self.__dict__['prop'] = 'default' @property def prop(self): return 'GET.' + self.__dict__['prop'] @prop.setter def prop(self, val): self.__dict__['prop'] = 'SET.' + val @prop.deleter def prop(self): self.__dict__['prop'] = 'DEL.' + self.__dict__['prop'] p1 = PropertyClass.prop assert repr(p1)[1:].startswith('property') s = pf.pickle_object(p1) p2 = pf.unpickle_object(s) obj = PropertyClass() val = p2.fget(obj) assert val == 'GET.default' p2.fset(obj, 'permafrost engine') val = p2.fget(obj) assert val == 'GET.SET.permafrost engine' print "property pickling OK!"
def test_pickle_descr_side_effects(): class UserDescr(object): def __get__(self, obj, objtype): raise RuntimeError('getting illegal attr') def __set__(self, obj, name): raise RuntimeError('setting illegal attr') class TestClass(object): desc = UserDescr() inst1 = TestClass() s = pf.pickle_object(inst1) inst2 = pf.unpickle_object(s) try: inst2.desc except RuntimeError as e: # accessing the descriptor gives the # expected exception assert str(e) == 'getting illegal attr' else: raise RuntimeError print "Descriptor pickling OK!"
def test_pickle_slice(): s1 = slice(2, 6, 2) s = pf.pickle_object(s1) s2 = pf.unpickle_object(s) assert repr(s1) == repr(s2) l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] assert l[s2] == [2, 4] s1 = slice('Hello World', slice(1, 5), 123) s = pf.pickle_object(s1) s2 = pf.unpickle_object(s) assert repr(s1) == repr(s2) print "Slice pickling OK!"
def test_pickle_notimplemented(): n1 = NotImplemented s = pf.pickle_object(n1) n2 = pf.unpickle_object(s) print "NotImplemented pickling OK!"
def test_pickle_ellipsis(): e1 = Ellipsis s = pf.pickle_object(e1) e2 = pf.unpickle_object(s) print "Ellipsis pickling OK!"
def test_pickle_sysfloatinfo(): l1 = sys.float_info s = pf.pickle_object(l1) l2 = pf.unpickle_object(s) assert l1 == l2 print "sys.float_info pickling OK!"
def test_pickle_set(): s1 = set({1,2,3,4,5,"Hello World"}) s = pf.pickle_object(s1) s2 = pf.unpickle_object(s) assert s1 == s2 print "Set pickling OK!"
def test_pickle_frozenset(): f1 = frozenset({1,2,3,4,5,"Hello World"}) s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1 == f2 print "Frozenset pickling OK!"
def test_pickle_bytearray(): b1 = bytearray('\x00\x01\x02\x03') s = pf.pickle_object(b1) b2 = pf.unpickle_object(s) assert b1 == b2 print "Bytearray pickling OK!"
def test_pickle_complex(): c1 = complex(1.23223, 0.09833) s = pf.pickle_object(c1) c2 = pf.unpickle_object(s) assert c1 == c2 print "Complex pickling OK!"
def test_pickle_float(): f1 = 15934.234349 s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1 == f2 print "Float pickling OK!"
def test_pickle_baseobject(): o1 = object() s = pf.pickle_object(o1) o2 = pf.unpickle_object(s) assert type(o1) == type(o2) print "Base object pickling OK!"
def test_pickle_long(): l1 = long(1 << 32) s = pf.pickle_object(l1) l2 = pf.unpickle_object(s) assert l1 == l2 print "Long picking OK!"
def test_pickle_syslonginfo(): l1 = sys.long_info s = pf.pickle_object(l1) l2 = pf.unpickle_object(s) assert l1 == l2 print "sys.long_info pickling OK!"
def test_pickle_unicode(): u1 = u'abcdefg' s = pf.pickle_object(u1) u2 = pf.unpickle_object(s) assert u1 == u2 u1 = u'\u043F\u0440\u0438\u0432\u0435\u0442' s = pf.pickle_object(u1) u2 = pf.unpickle_object(s) assert u1 == u2 u1 = u'\n\n' s = pf.pickle_object(u1) u2 = pf.unpickle_object(s) assert u1 == u2 print "Unicode pickling OK!"
def test_pickle_string(): s1 = "Hello World!" s = pf.pickle_object(s1) s2 = pf.unpickle_object(s) assert s1 == s2 s1 = "" s = pf.pickle_object(s1) s2 = pf.unpickle_object(s) assert s1 == s2 s1 = "\"This is a weird string\"\n" s = pf.pickle_object(s1) s2 = pf.unpickle_object(s) assert s1 == s2 print "String pickling OK!"