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_float(): f1 = 15934.234349 s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1 == f2 print "Float 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_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_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_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_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_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_file(): for f in [sys.stdin, sys.stdout, sys.stderr]: f1 = f s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1 == f2 print "File pickling OK!"
def test_pickle_range(): r1 = xrange(1, 10, 2) s = pf.pickle_object(r1) r2 = pf.unpickle_object(s) assert repr(r1) == repr(r2) assert next(r1.__iter__()) == next(r2.__iter__()) print "Range pickling OK!"
def test_pickle_wrapper_descriptor(): d1 = object.__delattr__ assert repr(d1)[1:].startswith("slot wrapper") s = pf.pickle_object(d1) d2 = pf.unpickle_object(s) assert d1 == d2 print "Wrapper descriptor pickling OK!"
def test_pickle_operator_builtins(): f1 = operator.itemgetter(1) s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1([0,1,2]) == f2([0,1,2]) f1 = operator.methodcaller('lower') s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1('TEST') == f2('TEST') f1 = operator.attrgetter('__class__') s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1(1) == f2(1) print "operator module builtins pickling OK!"
def test_pickle_nullimporter(): n1 = imp.NullImporter("__test__") s = pf.pickle_object(n1) n2 = pf.unpickle_object(s) assert type(n1) == type(n2) assert n2.find_module() == None print "imp.NullImporter pickling OK!"
def test_pickle_cfunction(): f1 = locals s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1 == f2 f1 = globals s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1 == f2 f1 = pf.pickle_object s = pf.pickle_object(f1) f2 = pf.unpickle_object(s) assert f1 == f2 print "Built-in function 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!"
def test_pickle_buffer(): text = 'Hello World' b1 = buffer(text, 6, 5) s = pf.pickle_object(b1) b2 = pf.unpickle_object(s) assert str(b2) == 'World' print "buffer pickling OK!"
def test_pickle_enumerate(): e1 = enumerate([0,1,9], 0) assert e1.next() == (0, 0) s = pf.pickle_object(e1) e2 = pf.unpickle_object(s) assert e2.next() == (1, 1) assert e2.next() == (2, 9) print "enumerate pickling OK!"
def test_pickle_code(): def testfunc(a, b): return a + b c1 = testfunc.func_code s = pf.pickle_object(c1) c2 = pf.unpickle_object(s) assert c1 == c2 print "Code pickling OK!"
def test_pickle_builtin_method(): fs = frozenset([1, 2, 3, 4, 5]) m1 = fs.__contains__ s = pf.pickle_object(m1) m2 = pf.unpickle_object(s) assert 'built-in method __contains__' in repr(m2) assert m1(1) == m2(1) assert m1(9) == m2(9) print "Built-in method pickling OK!"
def test_pickle_member_descriptor(): m1 = exceptions.SyntaxError.text assert repr(m1)[1:].startswith('member') s = pf.pickle_object(m1) m2 = pf.unpickle_object(s) assert type(m1) == type(m2) assert m1.__name__ == m2.__name__ class Subclass(exceptions.SyntaxError): pass m1 = Subclass.text assert repr(m1)[1:].startswith('member') s = pf.pickle_object(m1) m2 = pf.unpickle_object(s) assert type(m1) == type(m2) assert m1.__name__ == m2.__name__ print "Member descriptor pickling OK!"
def test_pickle_classmethod(): class ClassMethodTestClass(object): @classmethod def test_cls_method(cls): return 'this is a %s class method' % str(cls) def test_instance(self): return 'this is a %s instance method' % str(self) c1 = classmethod(ClassMethodTestClass.test_cls_method) assert repr(c1)[1:].startswith("classmethod") s = pf.pickle_object(c1) c2 = pf.unpickle_object(s) assert c1.__func__.__name__ == c2.__func__.__name__ cls1 = ClassMethodTestClass s = pf.pickle_object(cls1) cls2 = pf.unpickle_object(s) assert cls1.test_cls_method() == cls2.test_cls_method() print "Classmethod pickling OK!"
def test_pickle_list(): l1 = [] s = pf.pickle_object(l1) l2 = pf.unpickle_object(s) assert l1 == l2 l1 = [1, 2, 3, "Hello", "World", (1, 2), [3], [[[9]]], "!", (1, [120])] s = pf.pickle_object(l1) l2 = pf.unpickle_object(s) assert l1 == l2 #self referencing l1 = [] l1.append(l1) s = pf.pickle_object(l1) l2 = pf.unpickle_object(s) assert l1.__repr__() == l2.__repr__() # Can't compare direcly due to recursive def assert id(l2) == id(l2[0]) print "List pickling OK!"
def test_pickle_zipimporter(): libdir = [d for d in sys.path if d.endswith('lib/python2.7')][0] z1 = zipimport.zipimporter(libdir + '/test/zipdir.zip') s = pf.pickle_object(z1) z2 = pf.unpickle_object(s) assert type(z1) == type(z2) assert z1.archive == z2.archive assert z1.prefix == z2.prefix assert z1._files == z2._files print "zipimport.zipimporter pickling OK!"
def test_pickle_dictproxy(): class TestClass(object): """ Test docstring """ pass d1 = TestClass.__dict__ assert repr(d1).startswith("dict_proxy") s = pf.pickle_object(d1) d2 = pf.unpickle_object(s) assert type(d1) == type(d2) assert d1['__doc__'] == d2['__doc__'] print "dictproxy pickling OK!"
def test_pickle_memoryview(): bytes = bytearray('\x00\x01\x02\x03\x04\x05\x06\x07') m1 = memoryview(bytes) s = pf.pickle_object(m1) m2 = pf.unpickle_object(s) assert len(m1) == len(m2) for i in range(len(m1)): assert m1[i] == m2[i] print "memoryview pickling OK!"
def test_pickle_stentry(): st1 = _symtable.symtable('def f(x): return x', 'test', 'exec') assert repr(st1)[1:].startswith('symtable entry') s = pf.pickle_object(st1) st2 = pf.unpickle_object(s) assert type(st1) == type(st2) assert st1.lineno == st2.lineno assert st1.id == st2.id assert st1.varnames == st2.varnames assert st1.name == st2.name print "Symbol Table Entry pickling OK!"
def test_pickle_cell(): def outer(): a = 5 def inner(num): return a + num return inner c1 = outer().__closure__ s = pf.pickle_object(c1) c2 = pf.unpickle_object(s) assert c1 == c2 print "Cell pickling OK!"
def test_pickle_method_wrapper(): class TestClass(object): pass inst = TestClass() d1 = inst.__setattr__ assert repr(d1)[1:].startswith("method-wrapper") s = pf.pickle_object(d1) d2 = pf.unpickle_object(s) assert d1.__name__ == d2.__name__ d2('test', 123) assert d2.__self__.test == 123 print "Method-wrapper (slot method) pickling OK!"