THRIFT-3596 Better conformance to PEP8
This closes #832
diff --git a/lib/py/src/protocol/TBase.py b/lib/py/src/protocol/TBase.py
index d106f4e..87caf0d 100644
--- a/lib/py/src/protocol/TBase.py
+++ b/lib/py/src/protocol/TBase.py
@@ -21,78 +21,79 @@
from thrift.transport import TTransport
try:
- from thrift.protocol import fastbinary
+ from thrift.protocol import fastbinary
except:
- fastbinary = None
+ fastbinary = None
class TBase(object):
- __slots__ = ()
+ __slots__ = ()
- def __repr__(self):
- L = ['%s=%r' % (key, getattr(self, key)) for key in self.__slots__]
- return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
+ def __repr__(self):
+ L = ['%s=%r' % (key, getattr(self, key)) for key in self.__slots__]
+ return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
- def __eq__(self, other):
- if not isinstance(other, self.__class__):
- return False
- for attr in self.__slots__:
- my_val = getattr(self, attr)
- other_val = getattr(other, attr)
- if my_val != other_val:
- return False
- return True
+ def __eq__(self, other):
+ if not isinstance(other, self.__class__):
+ return False
+ for attr in self.__slots__:
+ my_val = getattr(self, attr)
+ other_val = getattr(other, attr)
+ if my_val != other_val:
+ return False
+ return True
- def __ne__(self, other):
- return not (self == other)
+ def __ne__(self, other):
+ return not (self == other)
- def read(self, iprot):
- if (iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
- isinstance(iprot.trans, TTransport.CReadableTransport) and
- self.thrift_spec is not None and
- fastbinary is not None):
- fastbinary.decode_binary(self,
- iprot.trans,
- (self.__class__, self.thrift_spec),
- iprot.string_length_limit,
- iprot.container_length_limit)
- return
- iprot.readStruct(self, self.thrift_spec)
+ def read(self, iprot):
+ if (iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
+ isinstance(iprot.trans, TTransport.CReadableTransport) and
+ self.thrift_spec is not None and
+ fastbinary is not None):
+ fastbinary.decode_binary(self,
+ iprot.trans,
+ (self.__class__, self.thrift_spec),
+ iprot.string_length_limit,
+ iprot.container_length_limit)
+ return
+ iprot.readStruct(self, self.thrift_spec)
- def write(self, oprot):
- if (oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
- self.thrift_spec is not None and
- fastbinary is not None):
- oprot.trans.write(
- fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
- return
- oprot.writeStruct(self, self.thrift_spec)
+ def write(self, oprot):
+ if (oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
+ self.thrift_spec is not None and
+ fastbinary is not None):
+ oprot.trans.write(
+ fastbinary.encode_binary(
+ self, (self.__class__, self.thrift_spec)))
+ return
+ oprot.writeStruct(self, self.thrift_spec)
class TExceptionBase(TBase, Exception):
- pass
+ pass
class TFrozenBase(TBase):
- def __setitem__(self, *args):
- raise TypeError("Can't modify frozen struct")
+ def __setitem__(self, *args):
+ raise TypeError("Can't modify frozen struct")
- def __delitem__(self, *args):
- raise TypeError("Can't modify frozen struct")
+ def __delitem__(self, *args):
+ raise TypeError("Can't modify frozen struct")
- def __hash__(self, *args):
- return hash(self.__class__) ^ hash(self.__slots__)
+ def __hash__(self, *args):
+ return hash(self.__class__) ^ hash(self.__slots__)
- @classmethod
- def read(cls, iprot):
- if (iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
- isinstance(iprot.trans, TTransport.CReadableTransport) and
- cls.thrift_spec is not None and
- fastbinary is not None):
- self = cls()
- return fastbinary.decode_binary(None,
- iprot.trans,
- (self.__class__, self.thrift_spec),
- iprot.string_length_limit,
- iprot.container_length_limit)
- return iprot.readStruct(cls, cls.thrift_spec, True)
+ @classmethod
+ def read(cls, iprot):
+ if (iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and
+ isinstance(iprot.trans, TTransport.CReadableTransport) and
+ cls.thrift_spec is not None and
+ fastbinary is not None):
+ self = cls()
+ return fastbinary.decode_binary(None,
+ iprot.trans,
+ (self.__class__, self.thrift_spec),
+ iprot.string_length_limit,
+ iprot.container_length_limit)
+ return iprot.readStruct(cls, cls.thrift_spec, True)
diff --git a/lib/py/src/protocol/TBinaryProtocol.py b/lib/py/src/protocol/TBinaryProtocol.py
index db4ea31..7fce12f 100644
--- a/lib/py/src/protocol/TBinaryProtocol.py
+++ b/lib/py/src/protocol/TBinaryProtocol.py
@@ -22,264 +22,264 @@
class TBinaryProtocol(TProtocolBase):
- """Binary implementation of the Thrift protocol driver."""
+ """Binary implementation of the Thrift protocol driver."""
- # NastyHaxx. Python 2.4+ on 32-bit machines forces hex constants to be
- # positive, converting this into a long. If we hardcode the int value
- # instead it'll stay in 32 bit-land.
+ # NastyHaxx. Python 2.4+ on 32-bit machines forces hex constants to be
+ # positive, converting this into a long. If we hardcode the int value
+ # instead it'll stay in 32 bit-land.
- # VERSION_MASK = 0xffff0000
- VERSION_MASK = -65536
+ # VERSION_MASK = 0xffff0000
+ VERSION_MASK = -65536
- # VERSION_1 = 0x80010000
- VERSION_1 = -2147418112
+ # VERSION_1 = 0x80010000
+ VERSION_1 = -2147418112
- TYPE_MASK = 0x000000ff
+ TYPE_MASK = 0x000000ff
- def __init__(self, trans, strictRead=False, strictWrite=True, **kwargs):
- TProtocolBase.__init__(self, trans)
- self.strictRead = strictRead
- self.strictWrite = strictWrite
- self.string_length_limit = kwargs.get('string_length_limit', None)
- self.container_length_limit = kwargs.get('container_length_limit', None)
+ def __init__(self, trans, strictRead=False, strictWrite=True, **kwargs):
+ TProtocolBase.__init__(self, trans)
+ self.strictRead = strictRead
+ self.strictWrite = strictWrite
+ self.string_length_limit = kwargs.get('string_length_limit', None)
+ self.container_length_limit = kwargs.get('container_length_limit', None)
- def _check_string_length(self, length):
- self._check_length(self.string_length_limit, length)
+ def _check_string_length(self, length):
+ self._check_length(self.string_length_limit, length)
- def _check_container_length(self, length):
- self._check_length(self.container_length_limit, length)
+ def _check_container_length(self, length):
+ self._check_length(self.container_length_limit, length)
- def writeMessageBegin(self, name, type, seqid):
- if self.strictWrite:
- self.writeI32(TBinaryProtocol.VERSION_1 | type)
- self.writeString(name)
- self.writeI32(seqid)
- else:
- self.writeString(name)
- self.writeByte(type)
- self.writeI32(seqid)
+ def writeMessageBegin(self, name, type, seqid):
+ if self.strictWrite:
+ self.writeI32(TBinaryProtocol.VERSION_1 | type)
+ self.writeString(name)
+ self.writeI32(seqid)
+ else:
+ self.writeString(name)
+ self.writeByte(type)
+ self.writeI32(seqid)
- def writeMessageEnd(self):
- pass
+ def writeMessageEnd(self):
+ pass
- def writeStructBegin(self, name):
- pass
+ def writeStructBegin(self, name):
+ pass
- def writeStructEnd(self):
- pass
+ def writeStructEnd(self):
+ pass
- def writeFieldBegin(self, name, type, id):
- self.writeByte(type)
- self.writeI16(id)
+ def writeFieldBegin(self, name, type, id):
+ self.writeByte(type)
+ self.writeI16(id)
- def writeFieldEnd(self):
- pass
+ def writeFieldEnd(self):
+ pass
- def writeFieldStop(self):
- self.writeByte(TType.STOP)
+ def writeFieldStop(self):
+ self.writeByte(TType.STOP)
- def writeMapBegin(self, ktype, vtype, size):
- self.writeByte(ktype)
- self.writeByte(vtype)
- self.writeI32(size)
+ def writeMapBegin(self, ktype, vtype, size):
+ self.writeByte(ktype)
+ self.writeByte(vtype)
+ self.writeI32(size)
- def writeMapEnd(self):
- pass
+ def writeMapEnd(self):
+ pass
- def writeListBegin(self, etype, size):
- self.writeByte(etype)
- self.writeI32(size)
+ def writeListBegin(self, etype, size):
+ self.writeByte(etype)
+ self.writeI32(size)
- def writeListEnd(self):
- pass
+ def writeListEnd(self):
+ pass
- def writeSetBegin(self, etype, size):
- self.writeByte(etype)
- self.writeI32(size)
+ def writeSetBegin(self, etype, size):
+ self.writeByte(etype)
+ self.writeI32(size)
- def writeSetEnd(self):
- pass
+ def writeSetEnd(self):
+ pass
- def writeBool(self, bool):
- if bool:
- self.writeByte(1)
- else:
- self.writeByte(0)
+ def writeBool(self, bool):
+ if bool:
+ self.writeByte(1)
+ else:
+ self.writeByte(0)
- def writeByte(self, byte):
- buff = pack("!b", byte)
- self.trans.write(buff)
+ def writeByte(self, byte):
+ buff = pack("!b", byte)
+ self.trans.write(buff)
- def writeI16(self, i16):
- buff = pack("!h", i16)
- self.trans.write(buff)
+ def writeI16(self, i16):
+ buff = pack("!h", i16)
+ self.trans.write(buff)
- def writeI32(self, i32):
- buff = pack("!i", i32)
- self.trans.write(buff)
+ def writeI32(self, i32):
+ buff = pack("!i", i32)
+ self.trans.write(buff)
- def writeI64(self, i64):
- buff = pack("!q", i64)
- self.trans.write(buff)
+ def writeI64(self, i64):
+ buff = pack("!q", i64)
+ self.trans.write(buff)
- def writeDouble(self, dub):
- buff = pack("!d", dub)
- self.trans.write(buff)
+ def writeDouble(self, dub):
+ buff = pack("!d", dub)
+ self.trans.write(buff)
- def writeBinary(self, str):
- self.writeI32(len(str))
- self.trans.write(str)
+ def writeBinary(self, str):
+ self.writeI32(len(str))
+ self.trans.write(str)
- def readMessageBegin(self):
- sz = self.readI32()
- if sz < 0:
- version = sz & TBinaryProtocol.VERSION_MASK
- if version != TBinaryProtocol.VERSION_1:
- raise TProtocolException(
- type=TProtocolException.BAD_VERSION,
- message='Bad version in readMessageBegin: %d' % (sz))
- type = sz & TBinaryProtocol.TYPE_MASK
- name = self.readString()
- seqid = self.readI32()
- else:
- if self.strictRead:
- raise TProtocolException(type=TProtocolException.BAD_VERSION,
- message='No protocol version header')
- name = self.trans.readAll(sz)
- type = self.readByte()
- seqid = self.readI32()
- return (name, type, seqid)
+ def readMessageBegin(self):
+ sz = self.readI32()
+ if sz < 0:
+ version = sz & TBinaryProtocol.VERSION_MASK
+ if version != TBinaryProtocol.VERSION_1:
+ raise TProtocolException(
+ type=TProtocolException.BAD_VERSION,
+ message='Bad version in readMessageBegin: %d' % (sz))
+ type = sz & TBinaryProtocol.TYPE_MASK
+ name = self.readString()
+ seqid = self.readI32()
+ else:
+ if self.strictRead:
+ raise TProtocolException(type=TProtocolException.BAD_VERSION,
+ message='No protocol version header')
+ name = self.trans.readAll(sz)
+ type = self.readByte()
+ seqid = self.readI32()
+ return (name, type, seqid)
- def readMessageEnd(self):
- pass
+ def readMessageEnd(self):
+ pass
- def readStructBegin(self):
- pass
+ def readStructBegin(self):
+ pass
- def readStructEnd(self):
- pass
+ def readStructEnd(self):
+ pass
- def readFieldBegin(self):
- type = self.readByte()
- if type == TType.STOP:
- return (None, type, 0)
- id = self.readI16()
- return (None, type, id)
+ def readFieldBegin(self):
+ type = self.readByte()
+ if type == TType.STOP:
+ return (None, type, 0)
+ id = self.readI16()
+ return (None, type, id)
- def readFieldEnd(self):
- pass
+ def readFieldEnd(self):
+ pass
- def readMapBegin(self):
- ktype = self.readByte()
- vtype = self.readByte()
- size = self.readI32()
- self._check_container_length(size)
- return (ktype, vtype, size)
+ def readMapBegin(self):
+ ktype = self.readByte()
+ vtype = self.readByte()
+ size = self.readI32()
+ self._check_container_length(size)
+ return (ktype, vtype, size)
- def readMapEnd(self):
- pass
+ def readMapEnd(self):
+ pass
- def readListBegin(self):
- etype = self.readByte()
- size = self.readI32()
- self._check_container_length(size)
- return (etype, size)
+ def readListBegin(self):
+ etype = self.readByte()
+ size = self.readI32()
+ self._check_container_length(size)
+ return (etype, size)
- def readListEnd(self):
- pass
+ def readListEnd(self):
+ pass
- def readSetBegin(self):
- etype = self.readByte()
- size = self.readI32()
- self._check_container_length(size)
- return (etype, size)
+ def readSetBegin(self):
+ etype = self.readByte()
+ size = self.readI32()
+ self._check_container_length(size)
+ return (etype, size)
- def readSetEnd(self):
- pass
+ def readSetEnd(self):
+ pass
- def readBool(self):
- byte = self.readByte()
- if byte == 0:
- return False
- return True
+ def readBool(self):
+ byte = self.readByte()
+ if byte == 0:
+ return False
+ return True
- def readByte(self):
- buff = self.trans.readAll(1)
- val, = unpack('!b', buff)
- return val
+ def readByte(self):
+ buff = self.trans.readAll(1)
+ val, = unpack('!b', buff)
+ return val
- def readI16(self):
- buff = self.trans.readAll(2)
- val, = unpack('!h', buff)
- return val
+ def readI16(self):
+ buff = self.trans.readAll(2)
+ val, = unpack('!h', buff)
+ return val
- def readI32(self):
- buff = self.trans.readAll(4)
- val, = unpack('!i', buff)
- return val
+ def readI32(self):
+ buff = self.trans.readAll(4)
+ val, = unpack('!i', buff)
+ return val
- def readI64(self):
- buff = self.trans.readAll(8)
- val, = unpack('!q', buff)
- return val
+ def readI64(self):
+ buff = self.trans.readAll(8)
+ val, = unpack('!q', buff)
+ return val
- def readDouble(self):
- buff = self.trans.readAll(8)
- val, = unpack('!d', buff)
- return val
+ def readDouble(self):
+ buff = self.trans.readAll(8)
+ val, = unpack('!d', buff)
+ return val
- def readBinary(self):
- size = self.readI32()
- self._check_string_length(size)
- s = self.trans.readAll(size)
- return s
+ def readBinary(self):
+ size = self.readI32()
+ self._check_string_length(size)
+ s = self.trans.readAll(size)
+ return s
class TBinaryProtocolFactory(object):
- def __init__(self, strictRead=False, strictWrite=True, **kwargs):
- self.strictRead = strictRead
- self.strictWrite = strictWrite
- self.string_length_limit = kwargs.get('string_length_limit', None)
- self.container_length_limit = kwargs.get('container_length_limit', None)
+ def __init__(self, strictRead=False, strictWrite=True, **kwargs):
+ self.strictRead = strictRead
+ self.strictWrite = strictWrite
+ self.string_length_limit = kwargs.get('string_length_limit', None)
+ self.container_length_limit = kwargs.get('container_length_limit', None)
- def getProtocol(self, trans):
- prot = TBinaryProtocol(trans, self.strictRead, self.strictWrite,
- string_length_limit=self.string_length_limit,
- container_length_limit=self.container_length_limit)
- return prot
+ def getProtocol(self, trans):
+ prot = TBinaryProtocol(trans, self.strictRead, self.strictWrite,
+ string_length_limit=self.string_length_limit,
+ container_length_limit=self.container_length_limit)
+ return prot
class TBinaryProtocolAccelerated(TBinaryProtocol):
- """C-Accelerated version of TBinaryProtocol.
+ """C-Accelerated version of TBinaryProtocol.
- This class does not override any of TBinaryProtocol's methods,
- but the generated code recognizes it directly and will call into
- our C module to do the encoding, bypassing this object entirely.
- We inherit from TBinaryProtocol so that the normal TBinaryProtocol
- encoding can happen if the fastbinary module doesn't work for some
- reason. (TODO(dreiss): Make this happen sanely in more cases.)
+ This class does not override any of TBinaryProtocol's methods,
+ but the generated code recognizes it directly and will call into
+ our C module to do the encoding, bypassing this object entirely.
+ We inherit from TBinaryProtocol so that the normal TBinaryProtocol
+ encoding can happen if the fastbinary module doesn't work for some
+ reason. (TODO(dreiss): Make this happen sanely in more cases.)
- In order to take advantage of the C module, just use
- TBinaryProtocolAccelerated instead of TBinaryProtocol.
+ In order to take advantage of the C module, just use
+ TBinaryProtocolAccelerated instead of TBinaryProtocol.
- NOTE: This code was contributed by an external developer.
- The internal Thrift team has reviewed and tested it,
- but we cannot guarantee that it is production-ready.
- Please feel free to report bugs and/or success stories
- to the public mailing list.
- """
- pass
+ NOTE: This code was contributed by an external developer.
+ The internal Thrift team has reviewed and tested it,
+ but we cannot guarantee that it is production-ready.
+ Please feel free to report bugs and/or success stories
+ to the public mailing list.
+ """
+ pass
class TBinaryProtocolAcceleratedFactory(object):
- def __init__(self,
- string_length_limit=None,
- container_length_limit=None):
- self.string_length_limit = string_length_limit
- self.container_length_limit = container_length_limit
+ def __init__(self,
+ string_length_limit=None,
+ container_length_limit=None):
+ self.string_length_limit = string_length_limit
+ self.container_length_limit = container_length_limit
- def getProtocol(self, trans):
- return TBinaryProtocolAccelerated(
- trans,
- string_length_limit=self.string_length_limit,
- container_length_limit=self.container_length_limit)
+ def getProtocol(self, trans):
+ return TBinaryProtocolAccelerated(
+ trans,
+ string_length_limit=self.string_length_limit,
+ container_length_limit=self.container_length_limit)
diff --git a/lib/py/src/protocol/TCompactProtocol.py b/lib/py/src/protocol/TCompactProtocol.py
index 3d9c0e6..8d3db1a 100644
--- a/lib/py/src/protocol/TCompactProtocol.py
+++ b/lib/py/src/protocol/TCompactProtocol.py
@@ -36,390 +36,391 @@
def make_helper(v_from, container):
- def helper(func):
- def nested(self, *args, **kwargs):
- assert self.state in (v_from, container), (self.state, v_from, container)
- return func(self, *args, **kwargs)
- return nested
- return helper
+ def helper(func):
+ def nested(self, *args, **kwargs):
+ assert self.state in (v_from, container), (self.state, v_from, container)
+ return func(self, *args, **kwargs)
+ return nested
+ return helper
writer = make_helper(VALUE_WRITE, CONTAINER_WRITE)
reader = make_helper(VALUE_READ, CONTAINER_READ)
def makeZigZag(n, bits):
- checkIntegerLimits(n, bits)
- return (n << 1) ^ (n >> (bits - 1))
+ checkIntegerLimits(n, bits)
+ return (n << 1) ^ (n >> (bits - 1))
def fromZigZag(n):
- return (n >> 1) ^ -(n & 1)
+ return (n >> 1) ^ -(n & 1)
def writeVarint(trans, n):
- out = bytearray()
- while True:
- if n & ~0x7f == 0:
- out.append(n)
- break
- else:
- out.append((n & 0xff) | 0x80)
- n = n >> 7
- trans.write(bytes(out))
+ out = bytearray()
+ while True:
+ if n & ~0x7f == 0:
+ out.append(n)
+ break
+ else:
+ out.append((n & 0xff) | 0x80)
+ n = n >> 7
+ trans.write(bytes(out))
def readVarint(trans):
- result = 0
- shift = 0
- while True:
- x = trans.readAll(1)
- byte = ord(x)
- result |= (byte & 0x7f) << shift
- if byte >> 7 == 0:
- return result
- shift += 7
+ result = 0
+ shift = 0
+ while True:
+ x = trans.readAll(1)
+ byte = ord(x)
+ result |= (byte & 0x7f) << shift
+ if byte >> 7 == 0:
+ return result
+ shift += 7
class CompactType(object):
- STOP = 0x00
- TRUE = 0x01
- FALSE = 0x02
- BYTE = 0x03
- I16 = 0x04
- I32 = 0x05
- I64 = 0x06
- DOUBLE = 0x07
- BINARY = 0x08
- LIST = 0x09
- SET = 0x0A
- MAP = 0x0B
- STRUCT = 0x0C
+ STOP = 0x00
+ TRUE = 0x01
+ FALSE = 0x02
+ BYTE = 0x03
+ I16 = 0x04
+ I32 = 0x05
+ I64 = 0x06
+ DOUBLE = 0x07
+ BINARY = 0x08
+ LIST = 0x09
+ SET = 0x0A
+ MAP = 0x0B
+ STRUCT = 0x0C
-CTYPES = {TType.STOP: CompactType.STOP,
- TType.BOOL: CompactType.TRUE, # used for collection
- TType.BYTE: CompactType.BYTE,
- TType.I16: CompactType.I16,
- TType.I32: CompactType.I32,
- TType.I64: CompactType.I64,
- TType.DOUBLE: CompactType.DOUBLE,
- TType.STRING: CompactType.BINARY,
- TType.STRUCT: CompactType.STRUCT,
- TType.LIST: CompactType.LIST,
- TType.SET: CompactType.SET,
- TType.MAP: CompactType.MAP
- }
+CTYPES = {
+ TType.STOP: CompactType.STOP,
+ TType.BOOL: CompactType.TRUE, # used for collection
+ TType.BYTE: CompactType.BYTE,
+ TType.I16: CompactType.I16,
+ TType.I32: CompactType.I32,
+ TType.I64: CompactType.I64,
+ TType.DOUBLE: CompactType.DOUBLE,
+ TType.STRING: CompactType.BINARY,
+ TType.STRUCT: CompactType.STRUCT,
+ TType.LIST: CompactType.LIST,
+ TType.SET: CompactType.SET,
+ TType.MAP: CompactType.MAP,
+}
TTYPES = {}
for k, v in CTYPES.items():
- TTYPES[v] = k
+ TTYPES[v] = k
TTYPES[CompactType.FALSE] = TType.BOOL
del k
del v
class TCompactProtocol(TProtocolBase):
- """Compact implementation of the Thrift protocol driver."""
+ """Compact implementation of the Thrift protocol driver."""
- PROTOCOL_ID = 0x82
- VERSION = 1
- VERSION_MASK = 0x1f
- TYPE_MASK = 0xe0
- TYPE_BITS = 0x07
- TYPE_SHIFT_AMOUNT = 5
+ PROTOCOL_ID = 0x82
+ VERSION = 1
+ VERSION_MASK = 0x1f
+ TYPE_MASK = 0xe0
+ TYPE_BITS = 0x07
+ TYPE_SHIFT_AMOUNT = 5
- def __init__(self, trans,
- string_length_limit=None,
- container_length_limit=None):
- TProtocolBase.__init__(self, trans)
- self.state = CLEAR
- self.__last_fid = 0
- self.__bool_fid = None
- self.__bool_value = None
- self.__structs = []
- self.__containers = []
- self.string_length_limit = string_length_limit
- self.container_length_limit = container_length_limit
+ def __init__(self, trans,
+ string_length_limit=None,
+ container_length_limit=None):
+ TProtocolBase.__init__(self, trans)
+ self.state = CLEAR
+ self.__last_fid = 0
+ self.__bool_fid = None
+ self.__bool_value = None
+ self.__structs = []
+ self.__containers = []
+ self.string_length_limit = string_length_limit
+ self.container_length_limit = container_length_limit
- def _check_string_length(self, length):
- self._check_length(self.string_length_limit, length)
+ def _check_string_length(self, length):
+ self._check_length(self.string_length_limit, length)
- def _check_container_length(self, length):
- self._check_length(self.container_length_limit, length)
+ def _check_container_length(self, length):
+ self._check_length(self.container_length_limit, length)
- def __writeVarint(self, n):
- writeVarint(self.trans, n)
+ def __writeVarint(self, n):
+ writeVarint(self.trans, n)
- def writeMessageBegin(self, name, type, seqid):
- assert self.state == CLEAR
- self.__writeUByte(self.PROTOCOL_ID)
- self.__writeUByte(self.VERSION | (type << self.TYPE_SHIFT_AMOUNT))
- self.__writeVarint(seqid)
- self.__writeBinary(str_to_binary(name))
- self.state = VALUE_WRITE
+ def writeMessageBegin(self, name, type, seqid):
+ assert self.state == CLEAR
+ self.__writeUByte(self.PROTOCOL_ID)
+ self.__writeUByte(self.VERSION | (type << self.TYPE_SHIFT_AMOUNT))
+ self.__writeVarint(seqid)
+ self.__writeBinary(str_to_binary(name))
+ self.state = VALUE_WRITE
- def writeMessageEnd(self):
- assert self.state == VALUE_WRITE
- self.state = CLEAR
+ def writeMessageEnd(self):
+ assert self.state == VALUE_WRITE
+ self.state = CLEAR
- def writeStructBegin(self, name):
- assert self.state in (CLEAR, CONTAINER_WRITE, VALUE_WRITE), self.state
- self.__structs.append((self.state, self.__last_fid))
- self.state = FIELD_WRITE
- self.__last_fid = 0
+ def writeStructBegin(self, name):
+ assert self.state in (CLEAR, CONTAINER_WRITE, VALUE_WRITE), self.state
+ self.__structs.append((self.state, self.__last_fid))
+ self.state = FIELD_WRITE
+ self.__last_fid = 0
- def writeStructEnd(self):
- assert self.state == FIELD_WRITE
- self.state, self.__last_fid = self.__structs.pop()
+ def writeStructEnd(self):
+ assert self.state == FIELD_WRITE
+ self.state, self.__last_fid = self.__structs.pop()
- def writeFieldStop(self):
- self.__writeByte(0)
+ def writeFieldStop(self):
+ self.__writeByte(0)
- def __writeFieldHeader(self, type, fid):
- delta = fid - self.__last_fid
- if 0 < delta <= 15:
- self.__writeUByte(delta << 4 | type)
- else:
- self.__writeByte(type)
- self.__writeI16(fid)
- self.__last_fid = fid
+ def __writeFieldHeader(self, type, fid):
+ delta = fid - self.__last_fid
+ if 0 < delta <= 15:
+ self.__writeUByte(delta << 4 | type)
+ else:
+ self.__writeByte(type)
+ self.__writeI16(fid)
+ self.__last_fid = fid
- def writeFieldBegin(self, name, type, fid):
- assert self.state == FIELD_WRITE, self.state
- if type == TType.BOOL:
- self.state = BOOL_WRITE
- self.__bool_fid = fid
- else:
- self.state = VALUE_WRITE
- self.__writeFieldHeader(CTYPES[type], fid)
+ def writeFieldBegin(self, name, type, fid):
+ assert self.state == FIELD_WRITE, self.state
+ if type == TType.BOOL:
+ self.state = BOOL_WRITE
+ self.__bool_fid = fid
+ else:
+ self.state = VALUE_WRITE
+ self.__writeFieldHeader(CTYPES[type], fid)
- def writeFieldEnd(self):
- assert self.state in (VALUE_WRITE, BOOL_WRITE), self.state
- self.state = FIELD_WRITE
+ def writeFieldEnd(self):
+ assert self.state in (VALUE_WRITE, BOOL_WRITE), self.state
+ self.state = FIELD_WRITE
- def __writeUByte(self, byte):
- self.trans.write(pack('!B', byte))
+ def __writeUByte(self, byte):
+ self.trans.write(pack('!B', byte))
- def __writeByte(self, byte):
- self.trans.write(pack('!b', byte))
+ def __writeByte(self, byte):
+ self.trans.write(pack('!b', byte))
- def __writeI16(self, i16):
- self.__writeVarint(makeZigZag(i16, 16))
+ def __writeI16(self, i16):
+ self.__writeVarint(makeZigZag(i16, 16))
- def __writeSize(self, i32):
- self.__writeVarint(i32)
+ def __writeSize(self, i32):
+ self.__writeVarint(i32)
- def writeCollectionBegin(self, etype, size):
- assert self.state in (VALUE_WRITE, CONTAINER_WRITE), self.state
- if size <= 14:
- self.__writeUByte(size << 4 | CTYPES[etype])
- else:
- self.__writeUByte(0xf0 | CTYPES[etype])
- self.__writeSize(size)
- self.__containers.append(self.state)
- self.state = CONTAINER_WRITE
- writeSetBegin = writeCollectionBegin
- writeListBegin = writeCollectionBegin
+ def writeCollectionBegin(self, etype, size):
+ assert self.state in (VALUE_WRITE, CONTAINER_WRITE), self.state
+ if size <= 14:
+ self.__writeUByte(size << 4 | CTYPES[etype])
+ else:
+ self.__writeUByte(0xf0 | CTYPES[etype])
+ self.__writeSize(size)
+ self.__containers.append(self.state)
+ self.state = CONTAINER_WRITE
+ writeSetBegin = writeCollectionBegin
+ writeListBegin = writeCollectionBegin
- def writeMapBegin(self, ktype, vtype, size):
- assert self.state in (VALUE_WRITE, CONTAINER_WRITE), self.state
- if size == 0:
- self.__writeByte(0)
- else:
- self.__writeSize(size)
- self.__writeUByte(CTYPES[ktype] << 4 | CTYPES[vtype])
- self.__containers.append(self.state)
- self.state = CONTAINER_WRITE
+ def writeMapBegin(self, ktype, vtype, size):
+ assert self.state in (VALUE_WRITE, CONTAINER_WRITE), self.state
+ if size == 0:
+ self.__writeByte(0)
+ else:
+ self.__writeSize(size)
+ self.__writeUByte(CTYPES[ktype] << 4 | CTYPES[vtype])
+ self.__containers.append(self.state)
+ self.state = CONTAINER_WRITE
- def writeCollectionEnd(self):
- assert self.state == CONTAINER_WRITE, self.state
- self.state = self.__containers.pop()
- writeMapEnd = writeCollectionEnd
- writeSetEnd = writeCollectionEnd
- writeListEnd = writeCollectionEnd
+ def writeCollectionEnd(self):
+ assert self.state == CONTAINER_WRITE, self.state
+ self.state = self.__containers.pop()
+ writeMapEnd = writeCollectionEnd
+ writeSetEnd = writeCollectionEnd
+ writeListEnd = writeCollectionEnd
- def writeBool(self, bool):
- if self.state == BOOL_WRITE:
- if bool:
- ctype = CompactType.TRUE
- else:
- ctype = CompactType.FALSE
- self.__writeFieldHeader(ctype, self.__bool_fid)
- elif self.state == CONTAINER_WRITE:
- if bool:
- self.__writeByte(CompactType.TRUE)
- else:
- self.__writeByte(CompactType.FALSE)
- else:
- raise AssertionError("Invalid state in compact protocol")
+ def writeBool(self, bool):
+ if self.state == BOOL_WRITE:
+ if bool:
+ ctype = CompactType.TRUE
+ else:
+ ctype = CompactType.FALSE
+ self.__writeFieldHeader(ctype, self.__bool_fid)
+ elif self.state == CONTAINER_WRITE:
+ if bool:
+ self.__writeByte(CompactType.TRUE)
+ else:
+ self.__writeByte(CompactType.FALSE)
+ else:
+ raise AssertionError("Invalid state in compact protocol")
- writeByte = writer(__writeByte)
- writeI16 = writer(__writeI16)
+ writeByte = writer(__writeByte)
+ writeI16 = writer(__writeI16)
- @writer
- def writeI32(self, i32):
- self.__writeVarint(makeZigZag(i32, 32))
+ @writer
+ def writeI32(self, i32):
+ self.__writeVarint(makeZigZag(i32, 32))
- @writer
- def writeI64(self, i64):
- self.__writeVarint(makeZigZag(i64, 64))
+ @writer
+ def writeI64(self, i64):
+ self.__writeVarint(makeZigZag(i64, 64))
- @writer
- def writeDouble(self, dub):
- self.trans.write(pack('<d', dub))
+ @writer
+ def writeDouble(self, dub):
+ self.trans.write(pack('<d', dub))
- def __writeBinary(self, s):
- self.__writeSize(len(s))
- self.trans.write(s)
- writeBinary = writer(__writeBinary)
+ def __writeBinary(self, s):
+ self.__writeSize(len(s))
+ self.trans.write(s)
+ writeBinary = writer(__writeBinary)
- def readFieldBegin(self):
- assert self.state == FIELD_READ, self.state
- type = self.__readUByte()
- if type & 0x0f == TType.STOP:
- return (None, 0, 0)
- delta = type >> 4
- if delta == 0:
- fid = self.__readI16()
- else:
- fid = self.__last_fid + delta
- self.__last_fid = fid
- type = type & 0x0f
- if type == CompactType.TRUE:
- self.state = BOOL_READ
- self.__bool_value = True
- elif type == CompactType.FALSE:
- self.state = BOOL_READ
- self.__bool_value = False
- else:
- self.state = VALUE_READ
- return (None, self.__getTType(type), fid)
+ def readFieldBegin(self):
+ assert self.state == FIELD_READ, self.state
+ type = self.__readUByte()
+ if type & 0x0f == TType.STOP:
+ return (None, 0, 0)
+ delta = type >> 4
+ if delta == 0:
+ fid = self.__readI16()
+ else:
+ fid = self.__last_fid + delta
+ self.__last_fid = fid
+ type = type & 0x0f
+ if type == CompactType.TRUE:
+ self.state = BOOL_READ
+ self.__bool_value = True
+ elif type == CompactType.FALSE:
+ self.state = BOOL_READ
+ self.__bool_value = False
+ else:
+ self.state = VALUE_READ
+ return (None, self.__getTType(type), fid)
- def readFieldEnd(self):
- assert self.state in (VALUE_READ, BOOL_READ), self.state
- self.state = FIELD_READ
+ def readFieldEnd(self):
+ assert self.state in (VALUE_READ, BOOL_READ), self.state
+ self.state = FIELD_READ
- def __readUByte(self):
- result, = unpack('!B', self.trans.readAll(1))
- return result
+ def __readUByte(self):
+ result, = unpack('!B', self.trans.readAll(1))
+ return result
- def __readByte(self):
- result, = unpack('!b', self.trans.readAll(1))
- return result
+ def __readByte(self):
+ result, = unpack('!b', self.trans.readAll(1))
+ return result
- def __readVarint(self):
- return readVarint(self.trans)
+ def __readVarint(self):
+ return readVarint(self.trans)
- def __readZigZag(self):
- return fromZigZag(self.__readVarint())
+ def __readZigZag(self):
+ return fromZigZag(self.__readVarint())
- def __readSize(self):
- result = self.__readVarint()
- if result < 0:
- raise TProtocolException("Length < 0")
- return result
+ def __readSize(self):
+ result = self.__readVarint()
+ if result < 0:
+ raise TProtocolException("Length < 0")
+ return result
- def readMessageBegin(self):
- assert self.state == CLEAR
- proto_id = self.__readUByte()
- if proto_id != self.PROTOCOL_ID:
- raise TProtocolException(TProtocolException.BAD_VERSION,
- '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))
- seqid = self.__readVarint()
- name = binary_to_str(self.__readBinary())
- return (name, type, seqid)
+ def readMessageBegin(self):
+ assert self.state == CLEAR
+ proto_id = self.__readUByte()
+ if proto_id != self.PROTOCOL_ID:
+ raise TProtocolException(TProtocolException.BAD_VERSION,
+ '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))
+ seqid = self.__readVarint()
+ name = binary_to_str(self.__readBinary())
+ return (name, type, seqid)
- def readMessageEnd(self):
- assert self.state == CLEAR
- assert len(self.__structs) == 0
+ def readMessageEnd(self):
+ assert self.state == CLEAR
+ assert len(self.__structs) == 0
- def readStructBegin(self):
- assert self.state in (CLEAR, CONTAINER_READ, VALUE_READ), self.state
- self.__structs.append((self.state, self.__last_fid))
- self.state = FIELD_READ
- self.__last_fid = 0
+ def readStructBegin(self):
+ assert self.state in (CLEAR, CONTAINER_READ, VALUE_READ), self.state
+ self.__structs.append((self.state, self.__last_fid))
+ self.state = FIELD_READ
+ self.__last_fid = 0
- def readStructEnd(self):
- assert self.state == FIELD_READ
- self.state, self.__last_fid = self.__structs.pop()
+ def readStructEnd(self):
+ assert self.state == FIELD_READ
+ self.state, self.__last_fid = self.__structs.pop()
- def readCollectionBegin(self):
- assert self.state in (VALUE_READ, CONTAINER_READ), self.state
- size_type = self.__readUByte()
- size = size_type >> 4
- type = self.__getTType(size_type)
- if size == 15:
- size = self.__readSize()
- self._check_container_length(size)
- self.__containers.append(self.state)
- self.state = CONTAINER_READ
- return type, size
- readSetBegin = readCollectionBegin
- readListBegin = readCollectionBegin
+ def readCollectionBegin(self):
+ assert self.state in (VALUE_READ, CONTAINER_READ), self.state
+ size_type = self.__readUByte()
+ size = size_type >> 4
+ type = self.__getTType(size_type)
+ if size == 15:
+ size = self.__readSize()
+ self._check_container_length(size)
+ self.__containers.append(self.state)
+ self.state = CONTAINER_READ
+ return type, size
+ readSetBegin = readCollectionBegin
+ readListBegin = readCollectionBegin
- def readMapBegin(self):
- assert self.state in (VALUE_READ, CONTAINER_READ), self.state
- size = self.__readSize()
- self._check_container_length(size)
- types = 0
- if size > 0:
- types = self.__readUByte()
- vtype = self.__getTType(types)
- ktype = self.__getTType(types >> 4)
- self.__containers.append(self.state)
- self.state = CONTAINER_READ
- return (ktype, vtype, size)
+ def readMapBegin(self):
+ assert self.state in (VALUE_READ, CONTAINER_READ), self.state
+ size = self.__readSize()
+ self._check_container_length(size)
+ types = 0
+ if size > 0:
+ types = self.__readUByte()
+ vtype = self.__getTType(types)
+ ktype = self.__getTType(types >> 4)
+ self.__containers.append(self.state)
+ self.state = CONTAINER_READ
+ return (ktype, vtype, size)
- def readCollectionEnd(self):
- assert self.state == CONTAINER_READ, self.state
- self.state = self.__containers.pop()
- readSetEnd = readCollectionEnd
- readListEnd = readCollectionEnd
- readMapEnd = readCollectionEnd
+ def readCollectionEnd(self):
+ assert self.state == CONTAINER_READ, self.state
+ self.state = self.__containers.pop()
+ readSetEnd = readCollectionEnd
+ readListEnd = readCollectionEnd
+ readMapEnd = readCollectionEnd
- def readBool(self):
- if self.state == BOOL_READ:
- return self.__bool_value == CompactType.TRUE
- elif self.state == CONTAINER_READ:
- return self.__readByte() == CompactType.TRUE
- else:
- raise AssertionError("Invalid state in compact protocol: %d" %
- self.state)
+ def readBool(self):
+ if self.state == BOOL_READ:
+ return self.__bool_value == CompactType.TRUE
+ elif self.state == CONTAINER_READ:
+ return self.__readByte() == CompactType.TRUE
+ else:
+ raise AssertionError("Invalid state in compact protocol: %d" %
+ self.state)
- readByte = reader(__readByte)
- __readI16 = __readZigZag
- readI16 = reader(__readZigZag)
- readI32 = reader(__readZigZag)
- readI64 = reader(__readZigZag)
+ readByte = reader(__readByte)
+ __readI16 = __readZigZag
+ readI16 = reader(__readZigZag)
+ readI32 = reader(__readZigZag)
+ readI64 = reader(__readZigZag)
- @reader
- def readDouble(self):
- buff = self.trans.readAll(8)
- val, = unpack('<d', buff)
- return val
+ @reader
+ def readDouble(self):
+ buff = self.trans.readAll(8)
+ val, = unpack('<d', buff)
+ return val
- def __readBinary(self):
- size = self.__readSize()
- self._check_string_length(size)
- return self.trans.readAll(size)
- readBinary = reader(__readBinary)
+ def __readBinary(self):
+ size = self.__readSize()
+ self._check_string_length(size)
+ return self.trans.readAll(size)
+ readBinary = reader(__readBinary)
- def __getTType(self, byte):
- return TTYPES[byte & 0x0f]
+ def __getTType(self, byte):
+ return TTYPES[byte & 0x0f]
class TCompactProtocolFactory(object):
- def __init__(self,
- string_length_limit=None,
- container_length_limit=None):
- self.string_length_limit = string_length_limit
- self.container_length_limit = container_length_limit
+ def __init__(self,
+ string_length_limit=None,
+ container_length_limit=None):
+ self.string_length_limit = string_length_limit
+ self.container_length_limit = container_length_limit
- def getProtocol(self, trans):
- return TCompactProtocol(trans,
- self.string_length_limit,
- self.container_length_limit)
+ def getProtocol(self, trans):
+ return TCompactProtocol(trans,
+ self.string_length_limit,
+ self.container_length_limit)
diff --git a/lib/py/src/protocol/TJSONProtocol.py b/lib/py/src/protocol/TJSONProtocol.py
index f9e65fb..db2099a 100644
--- a/lib/py/src/protocol/TJSONProtocol.py
+++ b/lib/py/src/protocol/TJSONProtocol.py
@@ -17,7 +17,8 @@
# under the License.
#
-from .TProtocol import TType, TProtocolBase, TProtocolException, checkIntegerLimits
+from .TProtocol import (TType, TProtocolBase, TProtocolException,
+ checkIntegerLimits)
import base64
import math
import sys
@@ -45,14 +46,14 @@
ESCSEQ0 = ord('\\')
ESCSEQ1 = ord('u')
ESCAPE_CHAR_VALS = {
- '"': '\\"',
- '\\': '\\\\',
- '\b': '\\b',
- '\f': '\\f',
- '\n': '\\n',
- '\r': '\\r',
- '\t': '\\t',
- # '/': '\\/',
+ '"': '\\"',
+ '\\': '\\\\',
+ '\b': '\\b',
+ '\f': '\\f',
+ '\n': '\\n',
+ '\r': '\\r',
+ '\t': '\\t',
+ # '/': '\\/',
}
ESCAPE_CHARS = {
b'"': '"',
@@ -66,519 +67,527 @@
}
NUMERIC_CHAR = b'+-.0123456789Ee'
-CTYPES = {TType.BOOL: 'tf',
- TType.BYTE: 'i8',
- TType.I16: 'i16',
- TType.I32: 'i32',
- TType.I64: 'i64',
- TType.DOUBLE: 'dbl',
- TType.STRING: 'str',
- TType.STRUCT: 'rec',
- TType.LIST: 'lst',
- TType.SET: 'set',
- TType.MAP: 'map'}
+CTYPES = {
+ TType.BOOL: 'tf',
+ TType.BYTE: 'i8',
+ TType.I16: 'i16',
+ TType.I32: 'i32',
+ TType.I64: 'i64',
+ TType.DOUBLE: 'dbl',
+ TType.STRING: 'str',
+ TType.STRUCT: 'rec',
+ TType.LIST: 'lst',
+ TType.SET: 'set',
+ TType.MAP: 'map',
+}
JTYPES = {}
for key in CTYPES.keys():
- JTYPES[CTYPES[key]] = key
+ JTYPES[CTYPES[key]] = key
class JSONBaseContext(object):
- def __init__(self, protocol):
- self.protocol = protocol
- self.first = True
+ def __init__(self, protocol):
+ self.protocol = protocol
+ self.first = True
- def doIO(self, function):
- pass
+ def doIO(self, function):
+ pass
- def write(self):
- pass
+ def write(self):
+ pass
- def read(self):
- pass
+ def read(self):
+ pass
- def escapeNum(self):
- return False
+ def escapeNum(self):
+ return False
- def __str__(self):
- return self.__class__.__name__
+ def __str__(self):
+ return self.__class__.__name__
class JSONListContext(JSONBaseContext):
- def doIO(self, function):
- if self.first is True:
- self.first = False
- else:
- function(COMMA)
+ def doIO(self, function):
+ if self.first is True:
+ self.first = False
+ else:
+ function(COMMA)
- def write(self):
- self.doIO(self.protocol.trans.write)
+ def write(self):
+ self.doIO(self.protocol.trans.write)
- def read(self):
- self.doIO(self.protocol.readJSONSyntaxChar)
+ def read(self):
+ self.doIO(self.protocol.readJSONSyntaxChar)
class JSONPairContext(JSONBaseContext):
- def __init__(self, protocol):
- super(JSONPairContext, self).__init__(protocol)
- self.colon = True
+ def __init__(self, protocol):
+ super(JSONPairContext, self).__init__(protocol)
+ self.colon = True
- def doIO(self, function):
- if self.first:
- self.first = False
- self.colon = True
- else:
- function(COLON if self.colon else COMMA)
- self.colon = not self.colon
+ def doIO(self, function):
+ if self.first:
+ self.first = False
+ self.colon = True
+ else:
+ function(COLON if self.colon else COMMA)
+ self.colon = not self.colon
- def write(self):
- self.doIO(self.protocol.trans.write)
+ def write(self):
+ self.doIO(self.protocol.trans.write)
- def read(self):
- self.doIO(self.protocol.readJSONSyntaxChar)
+ def read(self):
+ self.doIO(self.protocol.readJSONSyntaxChar)
- def escapeNum(self):
- return self.colon
+ def escapeNum(self):
+ return self.colon
- def __str__(self):
- return '%s, colon=%s' % (self.__class__.__name__, self.colon)
+ def __str__(self):
+ return '%s, colon=%s' % (self.__class__.__name__, self.colon)
class LookaheadReader():
- hasData = False
- data = ''
+ hasData = False
+ data = ''
- def __init__(self, protocol):
- self.protocol = protocol
+ def __init__(self, protocol):
+ self.protocol = protocol
- def read(self):
- if self.hasData is True:
- self.hasData = False
- else:
- self.data = self.protocol.trans.read(1)
- return self.data
+ def read(self):
+ if self.hasData is True:
+ self.hasData = False
+ else:
+ self.data = self.protocol.trans.read(1)
+ return self.data
- def peek(self):
- if self.hasData is False:
- self.data = self.protocol.trans.read(1)
- self.hasData = True
- return self.data
+ def peek(self):
+ if self.hasData is False:
+ self.data = self.protocol.trans.read(1)
+ self.hasData = True
+ return self.data
class TJSONProtocolBase(TProtocolBase):
- def __init__(self, trans):
- TProtocolBase.__init__(self, trans)
- self.resetWriteContext()
- self.resetReadContext()
+ def __init__(self, trans):
+ TProtocolBase.__init__(self, trans)
+ self.resetWriteContext()
+ self.resetReadContext()
- # We don't have length limit implementation for JSON protocols
- @property
- def string_length_limit(senf):
- return None
+ # We don't have length limit implementation for JSON protocols
+ @property
+ def string_length_limit(senf):
+ return None
- @property
- def container_length_limit(senf):
- return None
+ @property
+ def container_length_limit(senf):
+ return None
- def resetWriteContext(self):
- self.context = JSONBaseContext(self)
- self.contextStack = [self.context]
+ def resetWriteContext(self):
+ self.context = JSONBaseContext(self)
+ self.contextStack = [self.context]
- def resetReadContext(self):
- self.resetWriteContext()
- self.reader = LookaheadReader(self)
+ def resetReadContext(self):
+ self.resetWriteContext()
+ self.reader = LookaheadReader(self)
- def pushContext(self, ctx):
- self.contextStack.append(ctx)
- self.context = ctx
+ def pushContext(self, ctx):
+ self.contextStack.append(ctx)
+ self.context = ctx
- def popContext(self):
- self.contextStack.pop()
- if self.contextStack:
- self.context = self.contextStack[-1]
- else:
- self.context = JSONBaseContext(self)
-
- def writeJSONString(self, string):
- self.context.write()
- json_str = ['"']
- for s in string:
- escaped = ESCAPE_CHAR_VALS.get(s, s)
- json_str.append(escaped)
- json_str.append('"')
- self.trans.write(str_to_binary(''.join(json_str)))
-
- def writeJSONNumber(self, number, formatter='{0}'):
- self.context.write()
- jsNumber = str(formatter.format(number)).encode('ascii')
- if self.context.escapeNum():
- self.trans.write(QUOTE)
- self.trans.write(jsNumber)
- self.trans.write(QUOTE)
- else:
- self.trans.write(jsNumber)
-
- def writeJSONBase64(self, binary):
- self.context.write()
- self.trans.write(QUOTE)
- self.trans.write(base64.b64encode(binary))
- self.trans.write(QUOTE)
-
- def writeJSONObjectStart(self):
- self.context.write()
- self.trans.write(LBRACE)
- self.pushContext(JSONPairContext(self))
-
- def writeJSONObjectEnd(self):
- self.popContext()
- self.trans.write(RBRACE)
-
- def writeJSONArrayStart(self):
- self.context.write()
- self.trans.write(LBRACKET)
- self.pushContext(JSONListContext(self))
-
- def writeJSONArrayEnd(self):
- self.popContext()
- self.trans.write(RBRACKET)
-
- def readJSONSyntaxChar(self, character):
- current = self.reader.read()
- if character != current:
- raise TProtocolException(TProtocolException.INVALID_DATA,
- "Unexpected character: %s" % current)
-
- def _isHighSurrogate(self, codeunit):
- return codeunit >= 0xd800 and codeunit <= 0xdbff
-
- def _isLowSurrogate(self, codeunit):
- return codeunit >= 0xdc00 and codeunit <= 0xdfff
-
- def _toChar(self, high, low=None):
- if not low:
- if sys.version_info[0] == 2:
- return ("\\u%04x" % high).decode('unicode-escape').encode('utf-8')
- else:
- return chr(high)
- else:
- codepoint = (1 << 16) + ((high & 0x3ff) << 10)
- codepoint += low & 0x3ff
- if sys.version_info[0] == 2:
- s = "\\U%08x" % codepoint
- return s.decode('unicode-escape').encode('utf-8')
- else:
- return chr(codepoint)
-
- def readJSONString(self, skipContext):
- highSurrogate = None
- string = []
- if skipContext is False:
- self.context.read()
- self.readJSONSyntaxChar(QUOTE)
- while True:
- character = self.reader.read()
- if character == QUOTE:
- break
- if ord(character) == ESCSEQ0:
- character = self.reader.read()
- if ord(character) == ESCSEQ1:
- character = self.trans.read(4).decode('ascii')
- codeunit = int(character, 16)
- if self._isHighSurrogate(codeunit):
- if highSurrogate:
- raise TProtocolException(TProtocolException.INVALID_DATA,
- "Expected low surrogate char")
- highSurrogate = codeunit
- continue
- elif self._isLowSurrogate(codeunit):
- if not highSurrogate:
- raise TProtocolException(TProtocolException.INVALID_DATA,
- "Expected high surrogate char")
- character = self._toChar(highSurrogate, codeunit)
- highSurrogate = None
- else:
- character = self._toChar(codeunit)
+ def popContext(self):
+ self.contextStack.pop()
+ if self.contextStack:
+ self.context = self.contextStack[-1]
else:
- if character not in ESCAPE_CHARS:
+ self.context = JSONBaseContext(self)
+
+ def writeJSONString(self, string):
+ self.context.write()
+ json_str = ['"']
+ for s in string:
+ escaped = ESCAPE_CHAR_VALS.get(s, s)
+ json_str.append(escaped)
+ json_str.append('"')
+ self.trans.write(str_to_binary(''.join(json_str)))
+
+ def writeJSONNumber(self, number, formatter='{0}'):
+ self.context.write()
+ jsNumber = str(formatter.format(number)).encode('ascii')
+ if self.context.escapeNum():
+ self.trans.write(QUOTE)
+ self.trans.write(jsNumber)
+ self.trans.write(QUOTE)
+ else:
+ self.trans.write(jsNumber)
+
+ def writeJSONBase64(self, binary):
+ self.context.write()
+ self.trans.write(QUOTE)
+ self.trans.write(base64.b64encode(binary))
+ self.trans.write(QUOTE)
+
+ def writeJSONObjectStart(self):
+ self.context.write()
+ self.trans.write(LBRACE)
+ self.pushContext(JSONPairContext(self))
+
+ def writeJSONObjectEnd(self):
+ self.popContext()
+ self.trans.write(RBRACE)
+
+ def writeJSONArrayStart(self):
+ self.context.write()
+ self.trans.write(LBRACKET)
+ self.pushContext(JSONListContext(self))
+
+ def writeJSONArrayEnd(self):
+ self.popContext()
+ self.trans.write(RBRACKET)
+
+ def readJSONSyntaxChar(self, character):
+ current = self.reader.read()
+ if character != current:
raise TProtocolException(TProtocolException.INVALID_DATA,
- "Expected control char")
- character = ESCAPE_CHARS[character]
- elif character in ESCAPE_CHAR_VALS:
- raise TProtocolException(TProtocolException.INVALID_DATA,
- "Unescaped control char")
- elif sys.version_info[0] > 2:
- utf8_bytes = bytearray([ord(character)])
- while ord(self.reader.peek()) >= 0x80:
- utf8_bytes.append(ord(self.reader.read()))
- character = utf8_bytes.decode('utf8')
- string.append(character)
+ "Unexpected character: %s" % current)
- if highSurrogate:
- raise TProtocolException(TProtocolException.INVALID_DATA,
- "Expected low surrogate char")
- return ''.join(string)
+ def _isHighSurrogate(self, codeunit):
+ return codeunit >= 0xd800 and codeunit <= 0xdbff
- def isJSONNumeric(self, character):
- return (True if NUMERIC_CHAR.find(character) != - 1 else False)
+ def _isLowSurrogate(self, codeunit):
+ return codeunit >= 0xdc00 and codeunit <= 0xdfff
- def readJSONQuotes(self):
- if (self.context.escapeNum()):
- self.readJSONSyntaxChar(QUOTE)
+ def _toChar(self, high, low=None):
+ if not low:
+ if sys.version_info[0] == 2:
+ return ("\\u%04x" % high).decode('unicode-escape') \
+ .encode('utf-8')
+ else:
+ return chr(high)
+ else:
+ codepoint = (1 << 16) + ((high & 0x3ff) << 10)
+ codepoint += low & 0x3ff
+ if sys.version_info[0] == 2:
+ s = "\\U%08x" % codepoint
+ return s.decode('unicode-escape').encode('utf-8')
+ else:
+ return chr(codepoint)
- def readJSONNumericChars(self):
- numeric = []
- while True:
- character = self.reader.peek()
- if self.isJSONNumeric(character) is False:
- break
- numeric.append(self.reader.read())
- return b''.join(numeric).decode('ascii')
-
- def readJSONInteger(self):
- self.context.read()
- self.readJSONQuotes()
- numeric = self.readJSONNumericChars()
- self.readJSONQuotes()
- try:
- return int(numeric)
- except ValueError:
- raise TProtocolException(TProtocolException.INVALID_DATA,
- "Bad data encounted in numeric data")
-
- def readJSONDouble(self):
- self.context.read()
- if self.reader.peek() == QUOTE:
- string = self.readJSONString(True)
- try:
- double = float(string)
- if (self.context.escapeNum is False and
- not math.isinf(double) and
- not math.isnan(double)):
- raise TProtocolException(TProtocolException.INVALID_DATA,
- "Numeric data unexpectedly quoted")
- return double
- except ValueError:
- raise TProtocolException(TProtocolException.INVALID_DATA,
- "Bad data encounted in numeric data")
- else:
- if self.context.escapeNum() is True:
+ def readJSONString(self, skipContext):
+ highSurrogate = None
+ string = []
+ if skipContext is False:
+ self.context.read()
self.readJSONSyntaxChar(QUOTE)
- try:
- return float(self.readJSONNumericChars())
- except ValueError:
- raise TProtocolException(TProtocolException.INVALID_DATA,
- "Bad data encounted in numeric data")
+ while True:
+ character = self.reader.read()
+ if character == QUOTE:
+ break
+ if ord(character) == ESCSEQ0:
+ character = self.reader.read()
+ if ord(character) == ESCSEQ1:
+ character = self.trans.read(4).decode('ascii')
+ codeunit = int(character, 16)
+ if self._isHighSurrogate(codeunit):
+ if highSurrogate:
+ raise TProtocolException(
+ TProtocolException.INVALID_DATA,
+ "Expected low surrogate char")
+ highSurrogate = codeunit
+ continue
+ elif self._isLowSurrogate(codeunit):
+ if not highSurrogate:
+ raise TProtocolException(
+ TProtocolException.INVALID_DATA,
+ "Expected high surrogate char")
+ character = self._toChar(highSurrogate, codeunit)
+ highSurrogate = None
+ else:
+ character = self._toChar(codeunit)
+ else:
+ if character not in ESCAPE_CHARS:
+ raise TProtocolException(
+ TProtocolException.INVALID_DATA,
+ "Expected control char")
+ character = ESCAPE_CHARS[character]
+ elif character in ESCAPE_CHAR_VALS:
+ raise TProtocolException(TProtocolException.INVALID_DATA,
+ "Unescaped control char")
+ elif sys.version_info[0] > 2:
+ utf8_bytes = bytearray([ord(character)])
+ while ord(self.reader.peek()) >= 0x80:
+ utf8_bytes.append(ord(self.reader.read()))
+ character = utf8_bytes.decode('utf8')
+ string.append(character)
- def readJSONBase64(self):
- string = self.readJSONString(False)
- size = len(string)
- m = size % 4
- # Force padding since b64encode method does not allow it
- if m != 0:
- for i in range(4 - m):
- string += '='
- return base64.b64decode(string)
+ if highSurrogate:
+ raise TProtocolException(TProtocolException.INVALID_DATA,
+ "Expected low surrogate char")
+ return ''.join(string)
- def readJSONObjectStart(self):
- self.context.read()
- self.readJSONSyntaxChar(LBRACE)
- self.pushContext(JSONPairContext(self))
+ def isJSONNumeric(self, character):
+ return (True if NUMERIC_CHAR.find(character) != - 1 else False)
- def readJSONObjectEnd(self):
- self.readJSONSyntaxChar(RBRACE)
- self.popContext()
+ def readJSONQuotes(self):
+ if (self.context.escapeNum()):
+ self.readJSONSyntaxChar(QUOTE)
- def readJSONArrayStart(self):
- self.context.read()
- self.readJSONSyntaxChar(LBRACKET)
- self.pushContext(JSONListContext(self))
+ def readJSONNumericChars(self):
+ numeric = []
+ while True:
+ character = self.reader.peek()
+ if self.isJSONNumeric(character) is False:
+ break
+ numeric.append(self.reader.read())
+ return b''.join(numeric).decode('ascii')
- def readJSONArrayEnd(self):
- self.readJSONSyntaxChar(RBRACKET)
- self.popContext()
+ def readJSONInteger(self):
+ self.context.read()
+ self.readJSONQuotes()
+ numeric = self.readJSONNumericChars()
+ self.readJSONQuotes()
+ try:
+ return int(numeric)
+ except ValueError:
+ raise TProtocolException(TProtocolException.INVALID_DATA,
+ "Bad data encounted in numeric data")
+
+ def readJSONDouble(self):
+ self.context.read()
+ if self.reader.peek() == QUOTE:
+ string = self.readJSONString(True)
+ try:
+ double = float(string)
+ if (self.context.escapeNum is False and
+ not math.isinf(double) and
+ not math.isnan(double)):
+ raise TProtocolException(
+ TProtocolException.INVALID_DATA,
+ "Numeric data unexpectedly quoted")
+ return double
+ except ValueError:
+ raise TProtocolException(TProtocolException.INVALID_DATA,
+ "Bad data encounted in numeric data")
+ else:
+ if self.context.escapeNum() is True:
+ self.readJSONSyntaxChar(QUOTE)
+ try:
+ return float(self.readJSONNumericChars())
+ except ValueError:
+ raise TProtocolException(TProtocolException.INVALID_DATA,
+ "Bad data encounted in numeric data")
+
+ def readJSONBase64(self):
+ string = self.readJSONString(False)
+ size = len(string)
+ m = size % 4
+ # Force padding since b64encode method does not allow it
+ if m != 0:
+ for i in range(4 - m):
+ string += '='
+ return base64.b64decode(string)
+
+ def readJSONObjectStart(self):
+ self.context.read()
+ self.readJSONSyntaxChar(LBRACE)
+ self.pushContext(JSONPairContext(self))
+
+ def readJSONObjectEnd(self):
+ self.readJSONSyntaxChar(RBRACE)
+ self.popContext()
+
+ def readJSONArrayStart(self):
+ self.context.read()
+ self.readJSONSyntaxChar(LBRACKET)
+ self.pushContext(JSONListContext(self))
+
+ def readJSONArrayEnd(self):
+ self.readJSONSyntaxChar(RBRACKET)
+ self.popContext()
class TJSONProtocol(TJSONProtocolBase):
- def readMessageBegin(self):
- self.resetReadContext()
- self.readJSONArrayStart()
- if self.readJSONInteger() != VERSION:
- raise TProtocolException(TProtocolException.BAD_VERSION,
- "Message contained bad version.")
- name = self.readJSONString(False)
- typen = self.readJSONInteger()
- seqid = self.readJSONInteger()
- return (name, typen, seqid)
+ def readMessageBegin(self):
+ self.resetReadContext()
+ self.readJSONArrayStart()
+ if self.readJSONInteger() != VERSION:
+ raise TProtocolException(TProtocolException.BAD_VERSION,
+ "Message contained bad version.")
+ name = self.readJSONString(False)
+ typen = self.readJSONInteger()
+ seqid = self.readJSONInteger()
+ return (name, typen, seqid)
- def readMessageEnd(self):
- self.readJSONArrayEnd()
+ def readMessageEnd(self):
+ self.readJSONArrayEnd()
- def readStructBegin(self):
- self.readJSONObjectStart()
+ def readStructBegin(self):
+ self.readJSONObjectStart()
- def readStructEnd(self):
- self.readJSONObjectEnd()
+ def readStructEnd(self):
+ self.readJSONObjectEnd()
- def readFieldBegin(self):
- character = self.reader.peek()
- ttype = 0
- id = 0
- if character == RBRACE:
- ttype = TType.STOP
- else:
- id = self.readJSONInteger()
- self.readJSONObjectStart()
- ttype = JTYPES[self.readJSONString(False)]
- return (None, ttype, id)
+ def readFieldBegin(self):
+ character = self.reader.peek()
+ ttype = 0
+ id = 0
+ if character == RBRACE:
+ ttype = TType.STOP
+ else:
+ id = self.readJSONInteger()
+ self.readJSONObjectStart()
+ ttype = JTYPES[self.readJSONString(False)]
+ return (None, ttype, id)
- def readFieldEnd(self):
- self.readJSONObjectEnd()
+ def readFieldEnd(self):
+ self.readJSONObjectEnd()
- def readMapBegin(self):
- self.readJSONArrayStart()
- keyType = JTYPES[self.readJSONString(False)]
- valueType = JTYPES[self.readJSONString(False)]
- size = self.readJSONInteger()
- self.readJSONObjectStart()
- return (keyType, valueType, size)
+ def readMapBegin(self):
+ self.readJSONArrayStart()
+ keyType = JTYPES[self.readJSONString(False)]
+ valueType = JTYPES[self.readJSONString(False)]
+ size = self.readJSONInteger()
+ self.readJSONObjectStart()
+ return (keyType, valueType, size)
- def readMapEnd(self):
- self.readJSONObjectEnd()
- self.readJSONArrayEnd()
+ def readMapEnd(self):
+ self.readJSONObjectEnd()
+ self.readJSONArrayEnd()
- def readCollectionBegin(self):
- self.readJSONArrayStart()
- elemType = JTYPES[self.readJSONString(False)]
- size = self.readJSONInteger()
- return (elemType, size)
- readListBegin = readCollectionBegin
- readSetBegin = readCollectionBegin
+ def readCollectionBegin(self):
+ self.readJSONArrayStart()
+ elemType = JTYPES[self.readJSONString(False)]
+ size = self.readJSONInteger()
+ return (elemType, size)
+ readListBegin = readCollectionBegin
+ readSetBegin = readCollectionBegin
- def readCollectionEnd(self):
- self.readJSONArrayEnd()
- readSetEnd = readCollectionEnd
- readListEnd = readCollectionEnd
+ def readCollectionEnd(self):
+ self.readJSONArrayEnd()
+ readSetEnd = readCollectionEnd
+ readListEnd = readCollectionEnd
- def readBool(self):
- return (False if self.readJSONInteger() == 0 else True)
+ def readBool(self):
+ return (False if self.readJSONInteger() == 0 else True)
- def readNumber(self):
- return self.readJSONInteger()
- readByte = readNumber
- readI16 = readNumber
- readI32 = readNumber
- readI64 = readNumber
+ def readNumber(self):
+ return self.readJSONInteger()
+ readByte = readNumber
+ readI16 = readNumber
+ readI32 = readNumber
+ readI64 = readNumber
- def readDouble(self):
- return self.readJSONDouble()
+ def readDouble(self):
+ return self.readJSONDouble()
- def readString(self):
- return self.readJSONString(False)
+ def readString(self):
+ return self.readJSONString(False)
- def readBinary(self):
- return self.readJSONBase64()
+ def readBinary(self):
+ return self.readJSONBase64()
- def writeMessageBegin(self, name, request_type, seqid):
- self.resetWriteContext()
- self.writeJSONArrayStart()
- self.writeJSONNumber(VERSION)
- self.writeJSONString(name)
- self.writeJSONNumber(request_type)
- self.writeJSONNumber(seqid)
+ def writeMessageBegin(self, name, request_type, seqid):
+ self.resetWriteContext()
+ self.writeJSONArrayStart()
+ self.writeJSONNumber(VERSION)
+ self.writeJSONString(name)
+ self.writeJSONNumber(request_type)
+ self.writeJSONNumber(seqid)
- def writeMessageEnd(self):
- self.writeJSONArrayEnd()
+ def writeMessageEnd(self):
+ self.writeJSONArrayEnd()
- def writeStructBegin(self, name):
- self.writeJSONObjectStart()
+ def writeStructBegin(self, name):
+ self.writeJSONObjectStart()
- def writeStructEnd(self):
- self.writeJSONObjectEnd()
+ def writeStructEnd(self):
+ self.writeJSONObjectEnd()
- def writeFieldBegin(self, name, ttype, id):
- self.writeJSONNumber(id)
- self.writeJSONObjectStart()
- self.writeJSONString(CTYPES[ttype])
+ def writeFieldBegin(self, name, ttype, id):
+ self.writeJSONNumber(id)
+ self.writeJSONObjectStart()
+ self.writeJSONString(CTYPES[ttype])
- def writeFieldEnd(self):
- self.writeJSONObjectEnd()
+ def writeFieldEnd(self):
+ self.writeJSONObjectEnd()
- def writeFieldStop(self):
- pass
+ def writeFieldStop(self):
+ pass
- def writeMapBegin(self, ktype, vtype, size):
- self.writeJSONArrayStart()
- self.writeJSONString(CTYPES[ktype])
- self.writeJSONString(CTYPES[vtype])
- self.writeJSONNumber(size)
- self.writeJSONObjectStart()
+ def writeMapBegin(self, ktype, vtype, size):
+ self.writeJSONArrayStart()
+ self.writeJSONString(CTYPES[ktype])
+ self.writeJSONString(CTYPES[vtype])
+ self.writeJSONNumber(size)
+ self.writeJSONObjectStart()
- def writeMapEnd(self):
- self.writeJSONObjectEnd()
- self.writeJSONArrayEnd()
+ def writeMapEnd(self):
+ self.writeJSONObjectEnd()
+ self.writeJSONArrayEnd()
- def writeListBegin(self, etype, size):
- self.writeJSONArrayStart()
- self.writeJSONString(CTYPES[etype])
- self.writeJSONNumber(size)
+ def writeListBegin(self, etype, size):
+ self.writeJSONArrayStart()
+ self.writeJSONString(CTYPES[etype])
+ self.writeJSONNumber(size)
- def writeListEnd(self):
- self.writeJSONArrayEnd()
+ def writeListEnd(self):
+ self.writeJSONArrayEnd()
- def writeSetBegin(self, etype, size):
- self.writeJSONArrayStart()
- self.writeJSONString(CTYPES[etype])
- self.writeJSONNumber(size)
+ def writeSetBegin(self, etype, size):
+ self.writeJSONArrayStart()
+ self.writeJSONString(CTYPES[etype])
+ self.writeJSONNumber(size)
- def writeSetEnd(self):
- self.writeJSONArrayEnd()
+ def writeSetEnd(self):
+ self.writeJSONArrayEnd()
- def writeBool(self, boolean):
- self.writeJSONNumber(1 if boolean is True else 0)
+ def writeBool(self, boolean):
+ self.writeJSONNumber(1 if boolean is True else 0)
- def writeByte(self, byte):
- checkIntegerLimits(byte, 8)
- self.writeJSONNumber(byte)
+ def writeByte(self, byte):
+ checkIntegerLimits(byte, 8)
+ self.writeJSONNumber(byte)
- def writeI16(self, i16):
- checkIntegerLimits(i16, 16)
- self.writeJSONNumber(i16)
+ def writeI16(self, i16):
+ checkIntegerLimits(i16, 16)
+ self.writeJSONNumber(i16)
- def writeI32(self, i32):
- checkIntegerLimits(i32, 32)
- self.writeJSONNumber(i32)
+ def writeI32(self, i32):
+ checkIntegerLimits(i32, 32)
+ self.writeJSONNumber(i32)
- def writeI64(self, i64):
- checkIntegerLimits(i64, 64)
- self.writeJSONNumber(i64)
+ def writeI64(self, i64):
+ checkIntegerLimits(i64, 64)
+ self.writeJSONNumber(i64)
- def writeDouble(self, dbl):
- # 17 significant digits should be just enough for any double precision value.
- self.writeJSONNumber(dbl, '{0:.17g}')
+ def writeDouble(self, dbl):
+ # 17 significant digits should be just enough for any double precision
+ # value.
+ self.writeJSONNumber(dbl, '{0:.17g}')
- def writeString(self, string):
- self.writeJSONString(string)
+ def writeString(self, string):
+ self.writeJSONString(string)
- def writeBinary(self, binary):
- self.writeJSONBase64(binary)
+ def writeBinary(self, binary):
+ self.writeJSONBase64(binary)
class TJSONProtocolFactory(object):
- def getProtocol(self, trans):
- return TJSONProtocol(trans)
+ def getProtocol(self, trans):
+ return TJSONProtocol(trans)
- @property
- def string_length_limit(senf):
- return None
+ @property
+ def string_length_limit(senf):
+ return None
- @property
- def container_length_limit(senf):
- return None
+ @property
+ def container_length_limit(senf):
+ return None
class TSimpleJSONProtocol(TJSONProtocolBase):
diff --git a/lib/py/src/protocol/TMultiplexedProtocol.py b/lib/py/src/protocol/TMultiplexedProtocol.py
index d25f367..309f896 100644
--- a/lib/py/src/protocol/TMultiplexedProtocol.py
+++ b/lib/py/src/protocol/TMultiplexedProtocol.py
@@ -22,18 +22,19 @@
SEPARATOR = ":"
-class TMultiplexedProtocol(TProtocolDecorator.TProtocolDecorator):
- def __init__(self, protocol, serviceName):
- TProtocolDecorator.TProtocolDecorator.__init__(self, protocol)
- self.serviceName = serviceName
- def writeMessageBegin(self, name, type, seqid):
- if (type == TMessageType.CALL or
- type == TMessageType.ONEWAY):
- self.protocol.writeMessageBegin(
- self.serviceName + SEPARATOR + name,
- type,
- seqid
- )
- else:
- self.protocol.writeMessageBegin(name, type, seqid)
+class TMultiplexedProtocol(TProtocolDecorator.TProtocolDecorator):
+ def __init__(self, protocol, serviceName):
+ TProtocolDecorator.TProtocolDecorator.__init__(self, protocol)
+ self.serviceName = serviceName
+
+ def writeMessageBegin(self, name, type, seqid):
+ if (type == TMessageType.CALL or
+ type == TMessageType.ONEWAY):
+ self.protocol.writeMessageBegin(
+ self.serviceName + SEPARATOR + name,
+ type,
+ seqid
+ )
+ else:
+ self.protocol.writeMessageBegin(name, type, seqid)
diff --git a/lib/py/src/protocol/TProtocol.py b/lib/py/src/protocol/TProtocol.py
index d9aa2e8..ed6938b 100644
--- a/lib/py/src/protocol/TProtocol.py
+++ b/lib/py/src/protocol/TProtocol.py
@@ -28,373 +28,373 @@
class TProtocolException(TException):
- """Custom Protocol Exception class"""
+ """Custom Protocol Exception class"""
- UNKNOWN = 0
- INVALID_DATA = 1
- NEGATIVE_SIZE = 2
- SIZE_LIMIT = 3
- BAD_VERSION = 4
- NOT_IMPLEMENTED = 5
- DEPTH_LIMIT = 6
+ UNKNOWN = 0
+ INVALID_DATA = 1
+ NEGATIVE_SIZE = 2
+ SIZE_LIMIT = 3
+ BAD_VERSION = 4
+ NOT_IMPLEMENTED = 5
+ DEPTH_LIMIT = 6
- def __init__(self, type=UNKNOWN, message=None):
- TException.__init__(self, message)
- self.type = type
+ def __init__(self, type=UNKNOWN, message=None):
+ TException.__init__(self, message)
+ self.type = type
class TProtocolBase(object):
- """Base class for Thrift protocol driver."""
+ """Base class for Thrift protocol driver."""
- def __init__(self, trans):
- self.trans = trans
+ def __init__(self, trans):
+ self.trans = trans
- @staticmethod
- def _check_length(limit, length):
- if length < 0:
- raise TTransportException(TTransportException.NEGATIVE_SIZE,
- 'Negative length: %d' % length)
- if limit is not None and length > limit:
- raise TTransportException(TTransportException.SIZE_LIMIT,
- 'Length exceeded max allowed: %d' % limit)
+ @staticmethod
+ def _check_length(limit, length):
+ if length < 0:
+ raise TTransportException(TTransportException.NEGATIVE_SIZE,
+ 'Negative length: %d' % length)
+ if limit is not None and length > limit:
+ raise TTransportException(TTransportException.SIZE_LIMIT,
+ 'Length exceeded max allowed: %d' % limit)
- def writeMessageBegin(self, name, ttype, seqid):
- pass
+ def writeMessageBegin(self, name, ttype, seqid):
+ pass
- def writeMessageEnd(self):
- pass
+ def writeMessageEnd(self):
+ pass
- def writeStructBegin(self, name):
- pass
+ def writeStructBegin(self, name):
+ pass
- def writeStructEnd(self):
- pass
+ def writeStructEnd(self):
+ pass
- def writeFieldBegin(self, name, ttype, fid):
- pass
+ def writeFieldBegin(self, name, ttype, fid):
+ pass
- def writeFieldEnd(self):
- pass
+ def writeFieldEnd(self):
+ pass
- def writeFieldStop(self):
- pass
+ def writeFieldStop(self):
+ pass
- def writeMapBegin(self, ktype, vtype, size):
- pass
+ def writeMapBegin(self, ktype, vtype, size):
+ pass
- def writeMapEnd(self):
- pass
+ def writeMapEnd(self):
+ pass
- def writeListBegin(self, etype, size):
- pass
+ def writeListBegin(self, etype, size):
+ pass
- def writeListEnd(self):
- pass
+ def writeListEnd(self):
+ pass
- def writeSetBegin(self, etype, size):
- pass
+ def writeSetBegin(self, etype, size):
+ pass
- def writeSetEnd(self):
- pass
+ def writeSetEnd(self):
+ pass
- def writeBool(self, bool_val):
- pass
+ def writeBool(self, bool_val):
+ pass
- def writeByte(self, byte):
- pass
+ def writeByte(self, byte):
+ pass
- def writeI16(self, i16):
- pass
+ def writeI16(self, i16):
+ pass
- def writeI32(self, i32):
- pass
+ def writeI32(self, i32):
+ pass
- def writeI64(self, i64):
- pass
+ def writeI64(self, i64):
+ pass
- def writeDouble(self, dub):
- pass
+ def writeDouble(self, dub):
+ pass
- def writeString(self, str_val):
- self.writeBinary(str_to_binary(str_val))
+ def writeString(self, str_val):
+ self.writeBinary(str_to_binary(str_val))
- def writeBinary(self, str_val):
- pass
+ def writeBinary(self, str_val):
+ pass
- def writeUtf8(self, str_val):
- self.writeString(str_val.encode('utf8'))
+ def writeUtf8(self, str_val):
+ self.writeString(str_val.encode('utf8'))
- def readMessageBegin(self):
- pass
+ def readMessageBegin(self):
+ pass
- def readMessageEnd(self):
- pass
+ def readMessageEnd(self):
+ pass
- def readStructBegin(self):
- pass
+ def readStructBegin(self):
+ pass
- def readStructEnd(self):
- pass
+ def readStructEnd(self):
+ pass
- def readFieldBegin(self):
- pass
+ def readFieldBegin(self):
+ pass
- def readFieldEnd(self):
- pass
+ def readFieldEnd(self):
+ pass
- def readMapBegin(self):
- pass
+ def readMapBegin(self):
+ pass
- def readMapEnd(self):
- pass
+ def readMapEnd(self):
+ pass
- def readListBegin(self):
- pass
+ def readListBegin(self):
+ pass
- def readListEnd(self):
- pass
+ def readListEnd(self):
+ pass
- def readSetBegin(self):
- pass
+ def readSetBegin(self):
+ pass
- def readSetEnd(self):
- pass
+ def readSetEnd(self):
+ pass
- def readBool(self):
- pass
+ def readBool(self):
+ pass
- def readByte(self):
- pass
+ def readByte(self):
+ pass
- def readI16(self):
- pass
+ def readI16(self):
+ pass
- def readI32(self):
- pass
+ def readI32(self):
+ pass
- def readI64(self):
- pass
+ def readI64(self):
+ pass
- def readDouble(self):
- pass
+ def readDouble(self):
+ pass
- def readString(self):
- return binary_to_str(self.readBinary())
+ def readString(self):
+ return binary_to_str(self.readBinary())
- def readBinary(self):
- pass
+ def readBinary(self):
+ pass
- def readUtf8(self):
- return self.readString().decode('utf8')
+ def readUtf8(self):
+ return self.readString().decode('utf8')
- def skip(self, ttype):
- if ttype == TType.STOP:
- return
- elif ttype == TType.BOOL:
- self.readBool()
- elif ttype == TType.BYTE:
- self.readByte()
- elif ttype == TType.I16:
- self.readI16()
- elif ttype == TType.I32:
- self.readI32()
- elif ttype == TType.I64:
- self.readI64()
- elif ttype == TType.DOUBLE:
- self.readDouble()
- elif ttype == TType.STRING:
- self.readString()
- elif ttype == TType.STRUCT:
- name = self.readStructBegin()
- while True:
- (name, ttype, id) = self.readFieldBegin()
+ def skip(self, ttype):
if ttype == TType.STOP:
- break
- self.skip(ttype)
- self.readFieldEnd()
- self.readStructEnd()
- elif ttype == TType.MAP:
- (ktype, vtype, size) = self.readMapBegin()
- for i in range(size):
- self.skip(ktype)
- self.skip(vtype)
- self.readMapEnd()
- elif ttype == TType.SET:
- (etype, size) = self.readSetBegin()
- for i in range(size):
- self.skip(etype)
- self.readSetEnd()
- elif ttype == TType.LIST:
- (etype, size) = self.readListBegin()
- for i in range(size):
- self.skip(etype)
- self.readListEnd()
+ return
+ elif ttype == TType.BOOL:
+ self.readBool()
+ elif ttype == TType.BYTE:
+ self.readByte()
+ elif ttype == TType.I16:
+ self.readI16()
+ elif ttype == TType.I32:
+ self.readI32()
+ elif ttype == TType.I64:
+ self.readI64()
+ elif ttype == TType.DOUBLE:
+ self.readDouble()
+ elif ttype == TType.STRING:
+ self.readString()
+ elif ttype == TType.STRUCT:
+ name = self.readStructBegin()
+ while True:
+ (name, ttype, id) = self.readFieldBegin()
+ if ttype == TType.STOP:
+ break
+ self.skip(ttype)
+ self.readFieldEnd()
+ self.readStructEnd()
+ elif ttype == TType.MAP:
+ (ktype, vtype, size) = self.readMapBegin()
+ for i in range(size):
+ self.skip(ktype)
+ self.skip(vtype)
+ self.readMapEnd()
+ elif ttype == TType.SET:
+ (etype, size) = self.readSetBegin()
+ for i in range(size):
+ self.skip(etype)
+ self.readSetEnd()
+ elif ttype == TType.LIST:
+ (etype, size) = self.readListBegin()
+ for i in range(size):
+ self.skip(etype)
+ self.readListEnd()
- # tuple of: ( 'reader method' name, is_container bool, 'writer_method' name )
- _TTYPE_HANDLERS = (
- (None, None, False), # 0 TType.STOP
- (None, None, False), # 1 TType.VOID # TODO: handle void?
- ('readBool', 'writeBool', False), # 2 TType.BOOL
- ('readByte', 'writeByte', False), # 3 TType.BYTE and I08
- ('readDouble', 'writeDouble', False), # 4 TType.DOUBLE
- (None, None, False), # 5 undefined
- ('readI16', 'writeI16', False), # 6 TType.I16
- (None, None, False), # 7 undefined
- ('readI32', 'writeI32', False), # 8 TType.I32
- (None, None, False), # 9 undefined
- ('readI64', 'writeI64', False), # 10 TType.I64
- ('readString', 'writeString', False), # 11 TType.STRING and UTF7
- ('readContainerStruct', 'writeContainerStruct', True), # 12 *.STRUCT
- ('readContainerMap', 'writeContainerMap', True), # 13 TType.MAP
- ('readContainerSet', 'writeContainerSet', True), # 14 TType.SET
- ('readContainerList', 'writeContainerList', True), # 15 TType.LIST
- (None, None, False), # 16 TType.UTF8 # TODO: handle utf8 types?
- (None, None, False) # 17 TType.UTF16 # TODO: handle utf16 types?
- )
+ # tuple of: ( 'reader method' name, is_container bool, 'writer_method' name )
+ _TTYPE_HANDLERS = (
+ (None, None, False), # 0 TType.STOP
+ (None, None, False), # 1 TType.VOID # TODO: handle void?
+ ('readBool', 'writeBool', False), # 2 TType.BOOL
+ ('readByte', 'writeByte', False), # 3 TType.BYTE and I08
+ ('readDouble', 'writeDouble', False), # 4 TType.DOUBLE
+ (None, None, False), # 5 undefined
+ ('readI16', 'writeI16', False), # 6 TType.I16
+ (None, None, False), # 7 undefined
+ ('readI32', 'writeI32', False), # 8 TType.I32
+ (None, None, False), # 9 undefined
+ ('readI64', 'writeI64', False), # 10 TType.I64
+ ('readString', 'writeString', False), # 11 TType.STRING and UTF7
+ ('readContainerStruct', 'writeContainerStruct', True), # 12 *.STRUCT
+ ('readContainerMap', 'writeContainerMap', True), # 13 TType.MAP
+ ('readContainerSet', 'writeContainerSet', True), # 14 TType.SET
+ ('readContainerList', 'writeContainerList', True), # 15 TType.LIST
+ (None, None, False), # 16 TType.UTF8 # TODO: handle utf8 types?
+ (None, None, False) # 17 TType.UTF16 # TODO: handle utf16 types?
+ )
- def _ttype_handlers(self, ttype, spec):
- if spec == 'BINARY':
- if ttype != TType.STRING:
- raise TProtocolException(type=TProtocolException.INVALID_DATA,
- message='Invalid binary field type %d' % ttype)
- return ('readBinary', 'writeBinary', False)
- if sys.version_info[0] == 2 and spec == 'UTF8':
- if ttype != TType.STRING:
- raise TProtocolException(type=TProtocolException.INVALID_DATA,
- message='Invalid string field type %d' % ttype)
- return ('readUtf8', 'writeUtf8', False)
- return self._TTYPE_HANDLERS[ttype] if ttype < len(self._TTYPE_HANDLERS) else (None, None, False)
+ def _ttype_handlers(self, ttype, spec):
+ if spec == 'BINARY':
+ if ttype != TType.STRING:
+ raise TProtocolException(type=TProtocolException.INVALID_DATA,
+ message='Invalid binary field type %d' % ttype)
+ return ('readBinary', 'writeBinary', False)
+ if sys.version_info[0] == 2 and spec == 'UTF8':
+ if ttype != TType.STRING:
+ raise TProtocolException(type=TProtocolException.INVALID_DATA,
+ message='Invalid string field type %d' % ttype)
+ return ('readUtf8', 'writeUtf8', False)
+ return self._TTYPE_HANDLERS[ttype] if ttype < len(self._TTYPE_HANDLERS) else (None, None, False)
- def _read_by_ttype(self, ttype, spec, espec):
- reader_name, _, is_container = self._ttype_handlers(ttype, spec)
- if reader_name is None:
- raise TProtocolException(type=TProtocolException.INVALID_DATA,
- message='Invalid type %d' % (ttype))
- reader_func = getattr(self, reader_name)
- read = (lambda: reader_func(espec)) if is_container else reader_func
- while True:
- yield read()
+ def _read_by_ttype(self, ttype, spec, espec):
+ reader_name, _, is_container = self._ttype_handlers(ttype, spec)
+ if reader_name is None:
+ raise TProtocolException(type=TProtocolException.INVALID_DATA,
+ message='Invalid type %d' % (ttype))
+ reader_func = getattr(self, reader_name)
+ read = (lambda: reader_func(espec)) if is_container else reader_func
+ while True:
+ yield read()
- def readFieldByTType(self, ttype, spec):
- return self._read_by_ttype(ttype, spec, spec).next()
+ def readFieldByTType(self, ttype, spec):
+ return self._read_by_ttype(ttype, spec, spec).next()
- def readContainerList(self, spec):
- ttype, tspec, is_immutable = spec
- (list_type, list_len) = self.readListBegin()
- # TODO: compare types we just decoded with thrift_spec
- elems = islice(self._read_by_ttype(ttype, spec, tspec), list_len)
- results = (tuple if is_immutable else list)(elems)
- self.readListEnd()
- return results
+ def readContainerList(self, spec):
+ ttype, tspec, is_immutable = spec
+ (list_type, list_len) = self.readListBegin()
+ # TODO: compare types we just decoded with thrift_spec
+ elems = islice(self._read_by_ttype(ttype, spec, tspec), list_len)
+ results = (tuple if is_immutable else list)(elems)
+ self.readListEnd()
+ return results
- def readContainerSet(self, spec):
- ttype, tspec, is_immutable = spec
- (set_type, set_len) = self.readSetBegin()
- # TODO: compare types we just decoded with thrift_spec
- elems = islice(self._read_by_ttype(ttype, spec, tspec), set_len)
- results = (frozenset if is_immutable else set)(elems)
- self.readSetEnd()
- return results
+ def readContainerSet(self, spec):
+ ttype, tspec, is_immutable = spec
+ (set_type, set_len) = self.readSetBegin()
+ # TODO: compare types we just decoded with thrift_spec
+ elems = islice(self._read_by_ttype(ttype, spec, tspec), set_len)
+ results = (frozenset if is_immutable else set)(elems)
+ self.readSetEnd()
+ return results
- def readContainerStruct(self, spec):
- (obj_class, obj_spec) = spec
- obj = obj_class()
- obj.read(self)
- return obj
+ def readContainerStruct(self, spec):
+ (obj_class, obj_spec) = spec
+ obj = obj_class()
+ obj.read(self)
+ return obj
- def readContainerMap(self, spec):
- ktype, kspec, vtype, vspec, is_immutable = spec
- (map_ktype, map_vtype, map_len) = self.readMapBegin()
- # TODO: compare types we just decoded with thrift_spec and
- # abort/skip if types disagree
- keys = self._read_by_ttype(ktype, spec, kspec)
- vals = self._read_by_ttype(vtype, spec, vspec)
- keyvals = islice(zip(keys, vals), map_len)
- results = (TFrozenDict if is_immutable else dict)(keyvals)
- self.readMapEnd()
- return results
+ def readContainerMap(self, spec):
+ ktype, kspec, vtype, vspec, is_immutable = spec
+ (map_ktype, map_vtype, map_len) = self.readMapBegin()
+ # TODO: compare types we just decoded with thrift_spec and
+ # abort/skip if types disagree
+ keys = self._read_by_ttype(ktype, spec, kspec)
+ vals = self._read_by_ttype(vtype, spec, vspec)
+ keyvals = islice(zip(keys, vals), map_len)
+ results = (TFrozenDict if is_immutable else dict)(keyvals)
+ self.readMapEnd()
+ return results
- def readStruct(self, obj, thrift_spec, is_immutable=False):
- if is_immutable:
- fields = {}
- self.readStructBegin()
- while True:
- (fname, ftype, fid) = self.readFieldBegin()
- if ftype == TType.STOP:
- break
- try:
- field = thrift_spec[fid]
- except IndexError:
- self.skip(ftype)
- else:
- if field is not None and ftype == field[1]:
- fname = field[2]
- fspec = field[3]
- val = self.readFieldByTType(ftype, fspec)
- if is_immutable:
- fields[fname] = val
- else:
- setattr(obj, fname, val)
- else:
- self.skip(ftype)
- self.readFieldEnd()
- self.readStructEnd()
- if is_immutable:
- return obj(**fields)
+ def readStruct(self, obj, thrift_spec, is_immutable=False):
+ if is_immutable:
+ fields = {}
+ self.readStructBegin()
+ while True:
+ (fname, ftype, fid) = self.readFieldBegin()
+ if ftype == TType.STOP:
+ break
+ try:
+ field = thrift_spec[fid]
+ except IndexError:
+ self.skip(ftype)
+ else:
+ if field is not None and ftype == field[1]:
+ fname = field[2]
+ fspec = field[3]
+ val = self.readFieldByTType(ftype, fspec)
+ if is_immutable:
+ fields[fname] = val
+ else:
+ setattr(obj, fname, val)
+ else:
+ self.skip(ftype)
+ self.readFieldEnd()
+ self.readStructEnd()
+ if is_immutable:
+ return obj(**fields)
- def writeContainerStruct(self, val, spec):
- val.write(self)
+ def writeContainerStruct(self, val, spec):
+ val.write(self)
- def writeContainerList(self, val, spec):
- ttype, tspec, _ = spec
- self.writeListBegin(ttype, len(val))
- for _ in self._write_by_ttype(ttype, val, spec, tspec):
- pass
- self.writeListEnd()
+ def writeContainerList(self, val, spec):
+ ttype, tspec, _ = spec
+ self.writeListBegin(ttype, len(val))
+ for _ in self._write_by_ttype(ttype, val, spec, tspec):
+ pass
+ self.writeListEnd()
- def writeContainerSet(self, val, spec):
- ttype, tspec, _ = spec
- self.writeSetBegin(ttype, len(val))
- for _ in self._write_by_ttype(ttype, val, spec, tspec):
- pass
- self.writeSetEnd()
+ def writeContainerSet(self, val, spec):
+ ttype, tspec, _ = spec
+ self.writeSetBegin(ttype, len(val))
+ for _ in self._write_by_ttype(ttype, val, spec, tspec):
+ pass
+ self.writeSetEnd()
- def writeContainerMap(self, val, spec):
- ktype, kspec, vtype, vspec, _ = spec
- self.writeMapBegin(ktype, vtype, len(val))
- for _ in zip(self._write_by_ttype(ktype, six.iterkeys(val), spec, kspec),
- self._write_by_ttype(vtype, six.itervalues(val), spec, vspec)):
- pass
- self.writeMapEnd()
+ def writeContainerMap(self, val, spec):
+ ktype, kspec, vtype, vspec, _ = spec
+ self.writeMapBegin(ktype, vtype, len(val))
+ for _ in zip(self._write_by_ttype(ktype, six.iterkeys(val), spec, kspec),
+ self._write_by_ttype(vtype, six.itervalues(val), spec, vspec)):
+ pass
+ self.writeMapEnd()
- def writeStruct(self, obj, thrift_spec):
- self.writeStructBegin(obj.__class__.__name__)
- for field in thrift_spec:
- if field is None:
- continue
- fname = field[2]
- val = getattr(obj, fname)
- if val is None:
- # skip writing out unset fields
- continue
- fid = field[0]
- ftype = field[1]
- fspec = field[3]
- self.writeFieldBegin(fname, ftype, fid)
- self.writeFieldByTType(ftype, val, fspec)
- self.writeFieldEnd()
- self.writeFieldStop()
- self.writeStructEnd()
+ def writeStruct(self, obj, thrift_spec):
+ self.writeStructBegin(obj.__class__.__name__)
+ for field in thrift_spec:
+ if field is None:
+ continue
+ fname = field[2]
+ val = getattr(obj, fname)
+ if val is None:
+ # skip writing out unset fields
+ continue
+ fid = field[0]
+ ftype = field[1]
+ fspec = field[3]
+ self.writeFieldBegin(fname, ftype, fid)
+ self.writeFieldByTType(ftype, val, fspec)
+ self.writeFieldEnd()
+ self.writeFieldStop()
+ self.writeStructEnd()
- def _write_by_ttype(self, ttype, vals, spec, espec):
- _, writer_name, is_container = self._ttype_handlers(ttype, spec)
- writer_func = getattr(self, writer_name)
- write = (lambda v: writer_func(v, espec)) if is_container else writer_func
- for v in vals:
- yield write(v)
+ def _write_by_ttype(self, ttype, vals, spec, espec):
+ _, writer_name, is_container = self._ttype_handlers(ttype, spec)
+ writer_func = getattr(self, writer_name)
+ write = (lambda v: writer_func(v, espec)) if is_container else writer_func
+ for v in vals:
+ yield write(v)
- def writeFieldByTType(self, ttype, val, spec):
- self._write_by_ttype(ttype, [val], spec, spec).next()
+ def writeFieldByTType(self, ttype, val, spec):
+ self._write_by_ttype(ttype, [val], spec, spec).next()
def checkIntegerLimits(i, bits):
@@ -408,10 +408,10 @@
raise TProtocolException(TProtocolException.INVALID_DATA,
"i32 requires -2147483648 <= number <= 2147483647")
elif bits == 64 and (i < -9223372036854775808 or i > 9223372036854775807):
- raise TProtocolException(TProtocolException.INVALID_DATA,
- "i64 requires -9223372036854775808 <= number <= 9223372036854775807")
+ raise TProtocolException(TProtocolException.INVALID_DATA,
+ "i64 requires -9223372036854775808 <= number <= 9223372036854775807")
class TProtocolFactory(object):
- def getProtocol(self, trans):
- pass
+ def getProtocol(self, trans):
+ pass
diff --git a/lib/py/src/protocol/TProtocolDecorator.py b/lib/py/src/protocol/TProtocolDecorator.py
index bf50bfa..8b270a4 100644
--- a/lib/py/src/protocol/TProtocolDecorator.py
+++ b/lib/py/src/protocol/TProtocolDecorator.py
@@ -17,26 +17,34 @@
# under the License.
#
+import types
+
from thrift.protocol.TProtocol import TProtocolBase
-from types import *
+
class TProtocolDecorator():
- def __init__(self, protocol):
- TProtocolBase(protocol)
- self.protocol = protocol
+ def __init__(self, protocol):
+ TProtocolBase(protocol)
+ self.protocol = protocol
- def __getattr__(self, name):
- if hasattr(self.protocol, name):
- member = getattr(self.protocol, name)
- if type(member) in [MethodType, FunctionType, LambdaType, BuiltinFunctionType, BuiltinMethodType]:
- return lambda *args, **kwargs: self._wrap(member, args, kwargs)
- else:
- return member
- raise AttributeError(name)
+ def __getattr__(self, name):
+ if hasattr(self.protocol, name):
+ member = getattr(self.protocol, name)
+ if type(member) in [
+ types.MethodType,
+ types.FunctionType,
+ types.LambdaType,
+ types.BuiltinFunctionType,
+ types.BuiltinMethodType,
+ ]:
+ return lambda *args, **kwargs: self._wrap(member, args, kwargs)
+ else:
+ return member
+ raise AttributeError(name)
- def _wrap(self, func, args, kwargs):
- if type(func) == MethodType:
- result = func(*args, **kwargs)
- else:
- result = func(self.protocol, *args, **kwargs)
- return result
+ def _wrap(self, func, args, kwargs):
+ if isinstance(func, types.MethodType):
+ result = func(*args, **kwargs)
+ else:
+ result = func(self.protocol, *args, **kwargs)
+ return result
diff --git a/lib/py/src/protocol/__init__.py b/lib/py/src/protocol/__init__.py
index 7eefb45..7148f66 100644
--- a/lib/py/src/protocol/__init__.py
+++ b/lib/py/src/protocol/__init__.py
@@ -17,4 +17,5 @@
# under the License.
#
-__all__ = ['fastbinary', 'TBase', 'TBinaryProtocol', 'TCompactProtocol', 'TJSONProtocol', 'TProtocol']
+__all__ = ['fastbinary', 'TBase', 'TBinaryProtocol', 'TCompactProtocol',
+ 'TJSONProtocol', 'TProtocol']