Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/py/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ py3-test: py3-build
$(PYTHON3) test/thrift_TZlibTransport.py
$(PYTHON3) test/thrift_TCompactProtocol.py
$(PYTHON3) test/thrift_TNonblockingServer.py
$(PYTHON3) test/thrift_TSerializer.py
else
py3-build:
py3-test:
Expand All @@ -55,6 +56,7 @@ check-local: all py3-test
$(PYTHON) test/thrift_TZlibTransport.py
$(PYTHON) test/thrift_TCompactProtocol.py
$(PYTHON) test/thrift_TNonblockingServer.py
$(PYTHON) test/thrift_TSerializer.py


clean-local:
Expand Down
1 change: 1 addition & 0 deletions lib/py/src/ext/binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

#define PY_SSIZE_T_CLEAN
#include "ext/binary.h"
namespace apache {
namespace thrift {
Expand Down
1 change: 1 addition & 0 deletions lib/py/src/ext/compact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

#define PY_SSIZE_T_CLEAN
#include "ext/compact.h"

namespace apache {
Expand Down
6 changes: 5 additions & 1 deletion lib/py/test/test_thrift_file/TestServer.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
# specific language governing permissions and limitations
# under the License.
#


struct Message {
1: optional string body,
2: optional i64 num,
}

service TestServer{
string add_and_get_msg(1:string msg)
Expand Down
84 changes: 84 additions & 0 deletions lib/py/test/thrift_TSerializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

import unittest
import os
import sys

gen_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "gen-py"
)
sys.path.append(gen_path)

import _import_local_thrift # noqa
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
from thrift.protocol.TBinaryProtocol import TBinaryProtocolAcceleratedFactory
from thrift.protocol.TCompactProtocol import TCompactProtocolFactory
from thrift.protocol.TCompactProtocol import TCompactProtocolAcceleratedFactory
from thrift.transport import TTransport
from thrift.TSerialization import serialize, deserialize
from TestServer.ttypes import Message


class TestSerializer(unittest.TestCase):
def setUp(self):
self.message = Message("hello thrift", 42)
self.binary_serialized = b"\x0b\x00\x01\x00\x00\x00\x0chello thrift\n\x00\x02\x00\x00\x00\x00\x00\x00\x00*\x00"
self.compact_serialized = b'\x18\x0chello thrift\x16T\x00'

def verify(self, serialized, factory):
self.assertEqual(serialized, serialize(self.message, factory))

self.assertEqual(
"hello thrift",
deserialize(Message(), serialized, factory).body,
)
self.assertEqual(
42, deserialize(Message(), serialized, factory).num
)

self.assertRaises(EOFError, deserialize, Message(), None, factory)
self.assertRaises(EOFError, deserialize, Message(), b'', factory)
self.assertRaises(TypeError, deserialize, Message(), "test", factory)


def test_TBinaryProtocol(self):
buf = TTransport.TMemoryBuffer()
transport = TTransport.TBufferedTransportFactory().getTransport(buf)
factory = TBinaryProtocolFactory(transport)
self.verify(self.binary_serialized, factory)


def test_TBinaryProtocolAccelerated(self):
buf = TTransport.TMemoryBuffer()
transport = TTransport.TBufferedTransportFactory().getTransport(buf)
factory = TBinaryProtocolAcceleratedFactory(transport)
self.verify(self.binary_serialized, factory)

def test_TCompactProtocol(self):
factory = TCompactProtocolFactory()
self.verify(self.compact_serialized, factory)

def test_TCompactProtocolAccelerated(self):
factory = TCompactProtocolAcceleratedFactory()
self.verify(self.compact_serialized, factory)


if __name__ == "__main__":
unittest.main()