我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用__main__.PickleTestMemIO()。
def test_pickling(self): buf = self.buftype("1234567890") memio = self.ioclass(buf) memio.foo = 42 memio.seek(2) class PickleTestMemIO(self.ioclass): def __init__(me, initvalue, foo): self.ioclass.__init__(me, initvalue) me.foo = foo # __getnewargs__ is undefined on purpose. This checks that PEP 307 # is used to provide pickling support. # Pickle expects the class to be on the module level. Here we use a # little hack to allow the PickleTestMemIO class to derive from # self.ioclass without having to define all combinations explictly on # the module-level. import __main__ PickleTestMemIO.__module__ = '__main__' __main__.PickleTestMemIO = PickleTestMemIO submemio = PickleTestMemIO(buf, 80) submemio.seek(2) # We only support pickle protocol 2 and onward since we use extended # __reduce__ API of PEP 307 to provide pickling support. for proto in range(2, pickle.HIGHEST_PROTOCOL): for obj in (memio, submemio): obj2 = pickle.loads(pickle.dumps(obj, protocol=proto)) self.assertEqual(obj.getvalue(), obj2.getvalue()) self.assertEqual(obj.__class__, obj2.__class__) self.assertEqual(obj.foo, obj2.foo) self.assertEqual(obj.tell(), obj2.tell()) obj.close() self.assertRaises(ValueError, pickle.dumps, obj, proto) del __main__.PickleTestMemIO
def test_pickling(self): buf = self.buftype("1234567890") memio = self.ioclass(buf) memio.foo = 42 memio.seek(2) class PickleTestMemIO(self.ioclass): def __init__(me, initvalue, foo): self.ioclass.__init__(me, initvalue) me.foo = foo # __getnewargs__ is undefined on purpose. This checks that PEP 307 # is used to provide pickling support. # Pickle expects the class to be on the module level. Here we use a # little hack to allow the PickleTestMemIO class to derive from # self.ioclass without having to define all combinations explicitly on # the module-level. import __main__ PickleTestMemIO.__module__ = '__main__' PickleTestMemIO.__qualname__ = PickleTestMemIO.__name__ __main__.PickleTestMemIO = PickleTestMemIO submemio = PickleTestMemIO(buf, 80) submemio.seek(2) # We only support pickle protocol 2 and onward since we use extended # __reduce__ API of PEP 307 to provide pickling support. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): for obj in (memio, submemio): obj2 = pickle.loads(pickle.dumps(obj, protocol=proto)) self.assertEqual(obj.getvalue(), obj2.getvalue()) self.assertEqual(obj.__class__, obj2.__class__) self.assertEqual(obj.foo, obj2.foo) self.assertEqual(obj.tell(), obj2.tell()) obj.close() self.assertRaises(ValueError, pickle.dumps, obj, proto) del __main__.PickleTestMemIO
def test_pickling(self): buf = self.buftype("1234567890") memio = self.ioclass(buf) memio.foo = 42 memio.seek(2) class PickleTestMemIO(self.ioclass): def __init__(me, initvalue, foo): self.ioclass.__init__(me, initvalue) me.foo = foo # __getnewargs__ is undefined on purpose. This checks that PEP 307 # is used to provide pickling support. # Pickle expects the class to be on the module level. Here we use a # little hack to allow the PickleTestMemIO class to derive from # self.ioclass without having to define all combinations explictly on # the module-level. import __main__ PickleTestMemIO.__module__ = '__main__' __main__.PickleTestMemIO = PickleTestMemIO submemio = PickleTestMemIO(buf, 80) submemio.seek(2) # We only support pickle protocol 2 and onward since we use extended # __reduce__ API of PEP 307 to provide pickling support. for proto in range(2, pickle.HIGHEST_PROTOCOL): for obj in (memio, submemio): obj2 = pickle.loads(pickle.dumps(obj, protocol=proto)) self.assertEqual(obj.getvalue(), obj2.getvalue()) self.assertEqual(obj.__class__, obj2.__class__) self.assertEqual(obj.foo, obj2.foo) self.assertEqual(obj.tell(), obj2.tell()) obj2.close() self.assertRaises(ValueError, pickle.dumps, obj2, proto) del __main__.PickleTestMemIO
def test_pickling(self): buf = self.buftype("1234567890") memio = self.ioclass(buf) memio.foo = 42 memio.seek(2) class PickleTestMemIO(self.ioclass): def __init__(me, initvalue, foo): self.ioclass.__init__(me, initvalue) me.foo = foo # __getnewargs__ is undefined on purpose. This checks that PEP 307 # is used to provide pickling support. # Pickle expects the class to be on the module level. Here we use a # little hack to allow the PickleTestMemIO class to derive from # self.ioclass without having to define all combinations explicitly on # the module-level. import __main__ PickleTestMemIO.__module__ = '__main__' __main__.PickleTestMemIO = PickleTestMemIO submemio = PickleTestMemIO(buf, 80) submemio.seek(2) # We only support pickle protocol 2 and onward since we use extended # __reduce__ API of PEP 307 to provide pickling support. for proto in range(2, pickle.HIGHEST_PROTOCOL): for obj in (memio, submemio): obj2 = pickle.loads(pickle.dumps(obj, protocol=proto)) self.assertEqual(obj.getvalue(), obj2.getvalue()) self.assertEqual(obj.__class__, obj2.__class__) self.assertEqual(obj.foo, obj2.foo) self.assertEqual(obj.tell(), obj2.tell()) obj.close() self.assertRaises(ValueError, pickle.dumps, obj, proto) del __main__.PickleTestMemIO
def test_pickling(self): buf = self.buftype("1234567890") memio = self.ioclass(buf) memio.foo = 42 memio.seek(2) class PickleTestMemIO(self.ioclass): def __init__(me, initvalue, foo): self.ioclass.__init__(me, initvalue) me.foo = foo # __getnewargs__ is undefined on purpose. This checks that PEP 307 # is used to provide pickling support. # Pickle expects the class to be on the module level. Here we use a # little hack to allow the PickleTestMemIO class to derive from # self.ioclass without having to define all combinations explictly on # the module-level. import __main__ PickleTestMemIO.__module__ = '__main__' PickleTestMemIO.__qualname__ = PickleTestMemIO.__name__ __main__.PickleTestMemIO = PickleTestMemIO submemio = PickleTestMemIO(buf, 80) submemio.seek(2) # We only support pickle protocol 2 and onward since we use extended # __reduce__ API of PEP 307 to provide pickling support. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): for obj in (memio, submemio): obj2 = pickle.loads(pickle.dumps(obj, protocol=proto)) self.assertEqual(obj.getvalue(), obj2.getvalue()) self.assertEqual(obj.__class__, obj2.__class__) self.assertEqual(obj.foo, obj2.foo) self.assertEqual(obj.tell(), obj2.tell()) obj2.close() self.assertRaises(ValueError, pickle.dumps, obj2, proto) del __main__.PickleTestMemIO