THRIFT-1857 Python 3 Support
Client: Python
Patch: Thomas Bartelmess, Eevee (Alex Munroe), helgridly, Christian Verkerk, Jeroen Vlek, Nobuaki Sukegawa
This closes #213 and closes #680
diff --git a/lib/py/src/protocol/TCompactProtocol.py b/lib/py/src/protocol/TCompactProtocol.py
index 7054ab0..b8d171e 100644
--- a/lib/py/src/protocol/TCompactProtocol.py
+++ b/lib/py/src/protocol/TCompactProtocol.py
@@ -17,9 +17,11 @@
# under the License.
#
-from TProtocol import *
+from .TProtocol import TType, TProtocolBase, TProtocolException, checkIntegerLimits
from struct import pack, unpack
+from ..compat import binary_to_str, str_to_binary
+
__all__ = ['TCompactProtocol', 'TCompactProtocolFactory']
CLEAR = 0
@@ -62,7 +64,7 @@
else:
out.append((n & 0xff) | 0x80)
n = n >> 7
- trans.write(''.join(map(chr, out)))
+ trans.write(bytearray(out))
def readVarint(trans):
@@ -141,7 +143,7 @@
self.__writeUByte(self.PROTOCOL_ID)
self.__writeUByte(self.VERSION | (type << self.TYPE_SHIFT_AMOUNT))
self.__writeVarint(seqid)
- self.__writeString(name)
+ self.__writeBinary(str_to_binary(name))
self.state = VALUE_WRITE
def writeMessageEnd(self):
@@ -254,10 +256,10 @@
def writeDouble(self, dub):
self.trans.write(pack('<d', dub))
- def __writeString(self, s):
+ def __writeBinary(self, s):
self.__writeSize(len(s))
self.trans.write(s)
- writeString = writer(__writeString)
+ writeBinary = writer(__writeBinary)
def readFieldBegin(self):
assert self.state == FIELD_READ, self.state
@@ -302,7 +304,7 @@
def __readSize(self):
result = self.__readVarint()
if result < 0:
- raise TException("Length < 0")
+ raise TProtocolException("Length < 0")
return result
def readMessageBegin(self):
@@ -310,15 +312,15 @@
proto_id = self.__readUByte()
if proto_id != self.PROTOCOL_ID:
raise TProtocolException(TProtocolException.BAD_VERSION,
- 'Bad protocol id in the message: %d' % proto_id)
+ 'Bad protocol id in the message: %d' % proto_id)
ver_type = self.__readUByte()
type = (ver_type >> self.TYPE_SHIFT_AMOUNT) & self.TYPE_BITS
version = ver_type & self.VERSION_MASK
if version != self.VERSION:
raise TProtocolException(TProtocolException.BAD_VERSION,
- 'Bad version: %d (expect %d)' % (version, self.VERSION))
+ 'Bad version: %d (expect %d)' % (version, self.VERSION))
seqid = self.__readVarint()
- name = self.__readString()
+ name = binary_to_str(self.__readBinary())
return (name, type, seqid)
def readMessageEnd(self):
@@ -388,10 +390,10 @@
val, = unpack('<d', buff)
return val
- def __readString(self):
+ def __readBinary(self):
len = self.__readSize()
return self.trans.readAll(len)
- readString = reader(__readString)
+ readBinary = reader(__readBinary)
def __getTType(self, byte):
return TTYPES[byte & 0x0f]