mirror of
https://github.com/valitydev/thrift.git
synced 2024-11-07 02:45:22 +00:00
THRIFT-695. python: [de]serialize convenience functions
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@916508 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
dee6d4260c
commit
6acc269d18
15
lib/py/src/TSerialization.py
Normal file
15
lib/py/src/TSerialization.py
Normal file
@ -0,0 +1,15 @@
|
||||
from protocol import TBinaryProtocol
|
||||
from transport import TTransport
|
||||
|
||||
def serialize(thrift_object, protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()):
|
||||
transport = TTransport.TMemoryBuffer()
|
||||
protocol = protocol_factory.getProtocol(transport)
|
||||
thrift_object.write(protocol)
|
||||
return transport.getvalue()
|
||||
|
||||
def deserialize(base, buf, protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()):
|
||||
transport = TTransport.TMemoryBuffer(buf)
|
||||
protocol = protocol_factory.getProtocol(transport)
|
||||
base.read(protocol)
|
||||
return base
|
||||
|
@ -27,6 +27,7 @@ from ThriftTest.ttypes import *
|
||||
from thrift.transport import TTransport
|
||||
from thrift.transport import TSocket
|
||||
from thrift.protocol import TBinaryProtocol
|
||||
from thrift.TSerialization import serialize, deserialize
|
||||
import unittest
|
||||
import time
|
||||
|
||||
@ -118,6 +119,23 @@ class AcceleratedFramedTest(unittest.TestCase):
|
||||
self.assertEqual(prot.readString(), bigstring)
|
||||
self.assertEqual(prot.readI16(), 24)
|
||||
|
||||
class SerializersTest(unittest.TestCase):
|
||||
|
||||
def testSerializeThenDeserialize(self):
|
||||
obj = Xtruct2(i32_thing=1,
|
||||
struct_thing=Xtruct(string_thing="foo"))
|
||||
|
||||
s1 = serialize(obj)
|
||||
for i in range(10):
|
||||
self.assertEquals(s1, serialize(obj))
|
||||
objcopy = Xtruct2()
|
||||
deserialize(objcopy, serialize(obj))
|
||||
self.assertEquals(obj, objcopy)
|
||||
|
||||
obj = Xtruct(string_thing="bar")
|
||||
objcopy = Xtruct()
|
||||
deserialize(objcopy, serialize(obj))
|
||||
self.assertEquals(obj, objcopy)
|
||||
|
||||
|
||||
def suite():
|
||||
@ -127,6 +145,7 @@ def suite():
|
||||
suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
|
||||
suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
|
||||
suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest))
|
||||
suite.addTest(loader.loadTestsFromTestCase(SerializersTest))
|
||||
return suite
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
Reference in New Issue
Block a user