Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 20 | from .TProtocol import TType, TProtocolBase, TProtocolException, checkIntegerLimits |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 21 | from struct import pack, unpack |
| 22 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 23 | from ..compat import binary_to_str, str_to_binary |
| 24 | |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 25 | __all__ = ['TCompactProtocol', 'TCompactProtocolFactory'] |
| 26 | |
| 27 | CLEAR = 0 |
| 28 | FIELD_WRITE = 1 |
| 29 | VALUE_WRITE = 2 |
| 30 | CONTAINER_WRITE = 3 |
| 31 | BOOL_WRITE = 4 |
| 32 | FIELD_READ = 5 |
| 33 | CONTAINER_READ = 6 |
| 34 | VALUE_READ = 7 |
| 35 | BOOL_READ = 8 |
| 36 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 37 | |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 38 | def make_helper(v_from, container): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 39 | def helper(func): |
| 40 | def nested(self, *args, **kwargs): |
| 41 | assert self.state in (v_from, container), (self.state, v_from, container) |
| 42 | return func(self, *args, **kwargs) |
| 43 | return nested |
| 44 | return helper |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame^] | 45 | |
| 46 | |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 47 | writer = make_helper(VALUE_WRITE, CONTAINER_WRITE) |
| 48 | reader = make_helper(VALUE_READ, CONTAINER_READ) |
| 49 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 50 | |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 51 | def makeZigZag(n, bits): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 52 | checkIntegerLimits(n, bits) |
| 53 | return (n << 1) ^ (n >> (bits - 1)) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 54 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 55 | |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 56 | def fromZigZag(n): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 57 | return (n >> 1) ^ -(n & 1) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 58 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 59 | |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 60 | def writeVarint(trans, n): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 61 | out = bytearray() |
| 62 | while True: |
| 63 | if n & ~0x7f == 0: |
| 64 | out.append(n) |
| 65 | break |
| 66 | else: |
| 67 | out.append((n & 0xff) | 0x80) |
| 68 | n = n >> 7 |
| 69 | trans.write(bytes(out)) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 70 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 71 | |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 72 | def readVarint(trans): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 73 | result = 0 |
| 74 | shift = 0 |
| 75 | while True: |
| 76 | x = trans.readAll(1) |
| 77 | byte = ord(x) |
| 78 | result |= (byte & 0x7f) << shift |
| 79 | if byte >> 7 == 0: |
| 80 | return result |
| 81 | shift += 7 |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 82 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 83 | |
Nobuaki Sukegawa | b9c859a | 2015-12-21 01:10:25 +0900 | [diff] [blame] | 84 | class CompactType(object): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 85 | STOP = 0x00 |
| 86 | TRUE = 0x01 |
| 87 | FALSE = 0x02 |
| 88 | BYTE = 0x03 |
| 89 | I16 = 0x04 |
| 90 | I32 = 0x05 |
| 91 | I64 = 0x06 |
| 92 | DOUBLE = 0x07 |
| 93 | BINARY = 0x08 |
| 94 | LIST = 0x09 |
| 95 | SET = 0x0A |
| 96 | MAP = 0x0B |
| 97 | STRUCT = 0x0C |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 98 | |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame^] | 99 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 100 | CTYPES = { |
| 101 | TType.STOP: CompactType.STOP, |
| 102 | TType.BOOL: CompactType.TRUE, # used for collection |
| 103 | TType.BYTE: CompactType.BYTE, |
| 104 | TType.I16: CompactType.I16, |
| 105 | TType.I32: CompactType.I32, |
| 106 | TType.I64: CompactType.I64, |
| 107 | TType.DOUBLE: CompactType.DOUBLE, |
| 108 | TType.STRING: CompactType.BINARY, |
| 109 | TType.STRUCT: CompactType.STRUCT, |
| 110 | TType.LIST: CompactType.LIST, |
| 111 | TType.SET: CompactType.SET, |
| 112 | TType.MAP: CompactType.MAP, |
| 113 | } |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 114 | |
| 115 | TTYPES = {} |
| 116 | for k, v in CTYPES.items(): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 117 | TTYPES[v] = k |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 118 | TTYPES[CompactType.FALSE] = TType.BOOL |
| 119 | del k |
| 120 | del v |
| 121 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 122 | |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 123 | class TCompactProtocol(TProtocolBase): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 124 | """Compact implementation of the Thrift protocol driver.""" |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 125 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 126 | PROTOCOL_ID = 0x82 |
| 127 | VERSION = 1 |
| 128 | VERSION_MASK = 0x1f |
| 129 | TYPE_MASK = 0xe0 |
| 130 | TYPE_BITS = 0x07 |
| 131 | TYPE_SHIFT_AMOUNT = 5 |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 132 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 133 | def __init__(self, trans, |
| 134 | string_length_limit=None, |
| 135 | container_length_limit=None): |
| 136 | TProtocolBase.__init__(self, trans) |
| 137 | self.state = CLEAR |
| 138 | self.__last_fid = 0 |
| 139 | self.__bool_fid = None |
| 140 | self.__bool_value = None |
| 141 | self.__structs = [] |
| 142 | self.__containers = [] |
| 143 | self.string_length_limit = string_length_limit |
| 144 | self.container_length_limit = container_length_limit |
Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 145 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 146 | def _check_string_length(self, length): |
| 147 | self._check_length(self.string_length_limit, length) |
Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 148 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 149 | def _check_container_length(self, length): |
| 150 | self._check_length(self.container_length_limit, length) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 151 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 152 | def __writeVarint(self, n): |
| 153 | writeVarint(self.trans, n) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 154 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 155 | def writeMessageBegin(self, name, type, seqid): |
| 156 | assert self.state == CLEAR |
| 157 | self.__writeUByte(self.PROTOCOL_ID) |
| 158 | self.__writeUByte(self.VERSION | (type << self.TYPE_SHIFT_AMOUNT)) |
| 159 | self.__writeVarint(seqid) |
| 160 | self.__writeBinary(str_to_binary(name)) |
| 161 | self.state = VALUE_WRITE |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 162 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 163 | def writeMessageEnd(self): |
| 164 | assert self.state == VALUE_WRITE |
| 165 | self.state = CLEAR |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 166 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 167 | def writeStructBegin(self, name): |
| 168 | assert self.state in (CLEAR, CONTAINER_WRITE, VALUE_WRITE), self.state |
| 169 | self.__structs.append((self.state, self.__last_fid)) |
| 170 | self.state = FIELD_WRITE |
| 171 | self.__last_fid = 0 |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 172 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 173 | def writeStructEnd(self): |
| 174 | assert self.state == FIELD_WRITE |
| 175 | self.state, self.__last_fid = self.__structs.pop() |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 176 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 177 | def writeFieldStop(self): |
| 178 | self.__writeByte(0) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 179 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 180 | def __writeFieldHeader(self, type, fid): |
| 181 | delta = fid - self.__last_fid |
| 182 | if 0 < delta <= 15: |
| 183 | self.__writeUByte(delta << 4 | type) |
| 184 | else: |
| 185 | self.__writeByte(type) |
| 186 | self.__writeI16(fid) |
| 187 | self.__last_fid = fid |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 188 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 189 | def writeFieldBegin(self, name, type, fid): |
| 190 | assert self.state == FIELD_WRITE, self.state |
| 191 | if type == TType.BOOL: |
| 192 | self.state = BOOL_WRITE |
| 193 | self.__bool_fid = fid |
| 194 | else: |
| 195 | self.state = VALUE_WRITE |
| 196 | self.__writeFieldHeader(CTYPES[type], fid) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 197 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 198 | def writeFieldEnd(self): |
| 199 | assert self.state in (VALUE_WRITE, BOOL_WRITE), self.state |
| 200 | self.state = FIELD_WRITE |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 201 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 202 | def __writeUByte(self, byte): |
| 203 | self.trans.write(pack('!B', byte)) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 204 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 205 | def __writeByte(self, byte): |
| 206 | self.trans.write(pack('!b', byte)) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 207 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 208 | def __writeI16(self, i16): |
| 209 | self.__writeVarint(makeZigZag(i16, 16)) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 210 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 211 | def __writeSize(self, i32): |
| 212 | self.__writeVarint(i32) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 213 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 214 | def writeCollectionBegin(self, etype, size): |
| 215 | assert self.state in (VALUE_WRITE, CONTAINER_WRITE), self.state |
| 216 | if size <= 14: |
| 217 | self.__writeUByte(size << 4 | CTYPES[etype]) |
| 218 | else: |
| 219 | self.__writeUByte(0xf0 | CTYPES[etype]) |
| 220 | self.__writeSize(size) |
| 221 | self.__containers.append(self.state) |
| 222 | self.state = CONTAINER_WRITE |
| 223 | writeSetBegin = writeCollectionBegin |
| 224 | writeListBegin = writeCollectionBegin |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 225 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 226 | def writeMapBegin(self, ktype, vtype, size): |
| 227 | assert self.state in (VALUE_WRITE, CONTAINER_WRITE), self.state |
| 228 | if size == 0: |
| 229 | self.__writeByte(0) |
| 230 | else: |
| 231 | self.__writeSize(size) |
| 232 | self.__writeUByte(CTYPES[ktype] << 4 | CTYPES[vtype]) |
| 233 | self.__containers.append(self.state) |
| 234 | self.state = CONTAINER_WRITE |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 235 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 236 | def writeCollectionEnd(self): |
| 237 | assert self.state == CONTAINER_WRITE, self.state |
| 238 | self.state = self.__containers.pop() |
| 239 | writeMapEnd = writeCollectionEnd |
| 240 | writeSetEnd = writeCollectionEnd |
| 241 | writeListEnd = writeCollectionEnd |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 242 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 243 | def writeBool(self, bool): |
| 244 | if self.state == BOOL_WRITE: |
| 245 | if bool: |
| 246 | ctype = CompactType.TRUE |
| 247 | else: |
| 248 | ctype = CompactType.FALSE |
| 249 | self.__writeFieldHeader(ctype, self.__bool_fid) |
| 250 | elif self.state == CONTAINER_WRITE: |
| 251 | if bool: |
| 252 | self.__writeByte(CompactType.TRUE) |
| 253 | else: |
| 254 | self.__writeByte(CompactType.FALSE) |
| 255 | else: |
| 256 | raise AssertionError("Invalid state in compact protocol") |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 257 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 258 | writeByte = writer(__writeByte) |
| 259 | writeI16 = writer(__writeI16) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 260 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 261 | @writer |
| 262 | def writeI32(self, i32): |
| 263 | self.__writeVarint(makeZigZag(i32, 32)) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 264 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 265 | @writer |
| 266 | def writeI64(self, i64): |
| 267 | self.__writeVarint(makeZigZag(i64, 64)) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 268 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 269 | @writer |
| 270 | def writeDouble(self, dub): |
| 271 | self.trans.write(pack('<d', dub)) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 272 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 273 | def __writeBinary(self, s): |
| 274 | self.__writeSize(len(s)) |
| 275 | self.trans.write(s) |
| 276 | writeBinary = writer(__writeBinary) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 277 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 278 | def readFieldBegin(self): |
| 279 | assert self.state == FIELD_READ, self.state |
| 280 | type = self.__readUByte() |
| 281 | if type & 0x0f == TType.STOP: |
| 282 | return (None, 0, 0) |
| 283 | delta = type >> 4 |
| 284 | if delta == 0: |
| 285 | fid = self.__readI16() |
| 286 | else: |
| 287 | fid = self.__last_fid + delta |
| 288 | self.__last_fid = fid |
| 289 | type = type & 0x0f |
| 290 | if type == CompactType.TRUE: |
| 291 | self.state = BOOL_READ |
| 292 | self.__bool_value = True |
| 293 | elif type == CompactType.FALSE: |
| 294 | self.state = BOOL_READ |
| 295 | self.__bool_value = False |
| 296 | else: |
| 297 | self.state = VALUE_READ |
| 298 | return (None, self.__getTType(type), fid) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 299 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 300 | def readFieldEnd(self): |
| 301 | assert self.state in (VALUE_READ, BOOL_READ), self.state |
| 302 | self.state = FIELD_READ |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 303 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 304 | def __readUByte(self): |
| 305 | result, = unpack('!B', self.trans.readAll(1)) |
| 306 | return result |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 307 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 308 | def __readByte(self): |
| 309 | result, = unpack('!b', self.trans.readAll(1)) |
| 310 | return result |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 311 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 312 | def __readVarint(self): |
| 313 | return readVarint(self.trans) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 314 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 315 | def __readZigZag(self): |
| 316 | return fromZigZag(self.__readVarint()) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 317 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 318 | def __readSize(self): |
| 319 | result = self.__readVarint() |
| 320 | if result < 0: |
| 321 | raise TProtocolException("Length < 0") |
| 322 | return result |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 323 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 324 | def readMessageBegin(self): |
| 325 | assert self.state == CLEAR |
| 326 | proto_id = self.__readUByte() |
| 327 | if proto_id != self.PROTOCOL_ID: |
| 328 | raise TProtocolException(TProtocolException.BAD_VERSION, |
| 329 | 'Bad protocol id in the message: %d' % proto_id) |
| 330 | ver_type = self.__readUByte() |
| 331 | type = (ver_type >> self.TYPE_SHIFT_AMOUNT) & self.TYPE_BITS |
| 332 | version = ver_type & self.VERSION_MASK |
| 333 | if version != self.VERSION: |
| 334 | raise TProtocolException(TProtocolException.BAD_VERSION, |
| 335 | 'Bad version: %d (expect %d)' % (version, self.VERSION)) |
| 336 | seqid = self.__readVarint() |
| 337 | name = binary_to_str(self.__readBinary()) |
| 338 | return (name, type, seqid) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 339 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 340 | def readMessageEnd(self): |
| 341 | assert self.state == CLEAR |
| 342 | assert len(self.__structs) == 0 |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 343 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 344 | def readStructBegin(self): |
| 345 | assert self.state in (CLEAR, CONTAINER_READ, VALUE_READ), self.state |
| 346 | self.__structs.append((self.state, self.__last_fid)) |
| 347 | self.state = FIELD_READ |
| 348 | self.__last_fid = 0 |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 349 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 350 | def readStructEnd(self): |
| 351 | assert self.state == FIELD_READ |
| 352 | self.state, self.__last_fid = self.__structs.pop() |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 353 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 354 | def readCollectionBegin(self): |
| 355 | assert self.state in (VALUE_READ, CONTAINER_READ), self.state |
| 356 | size_type = self.__readUByte() |
| 357 | size = size_type >> 4 |
| 358 | type = self.__getTType(size_type) |
| 359 | if size == 15: |
| 360 | size = self.__readSize() |
| 361 | self._check_container_length(size) |
| 362 | self.__containers.append(self.state) |
| 363 | self.state = CONTAINER_READ |
| 364 | return type, size |
| 365 | readSetBegin = readCollectionBegin |
| 366 | readListBegin = readCollectionBegin |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 367 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 368 | def readMapBegin(self): |
| 369 | assert self.state in (VALUE_READ, CONTAINER_READ), self.state |
| 370 | size = self.__readSize() |
| 371 | self._check_container_length(size) |
| 372 | types = 0 |
| 373 | if size > 0: |
| 374 | types = self.__readUByte() |
| 375 | vtype = self.__getTType(types) |
| 376 | ktype = self.__getTType(types >> 4) |
| 377 | self.__containers.append(self.state) |
| 378 | self.state = CONTAINER_READ |
| 379 | return (ktype, vtype, size) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 380 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 381 | def readCollectionEnd(self): |
| 382 | assert self.state == CONTAINER_READ, self.state |
| 383 | self.state = self.__containers.pop() |
| 384 | readSetEnd = readCollectionEnd |
| 385 | readListEnd = readCollectionEnd |
| 386 | readMapEnd = readCollectionEnd |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 387 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 388 | def readBool(self): |
| 389 | if self.state == BOOL_READ: |
| 390 | return self.__bool_value == CompactType.TRUE |
| 391 | elif self.state == CONTAINER_READ: |
| 392 | return self.__readByte() == CompactType.TRUE |
| 393 | else: |
| 394 | raise AssertionError("Invalid state in compact protocol: %d" % |
| 395 | self.state) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 396 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 397 | readByte = reader(__readByte) |
| 398 | __readI16 = __readZigZag |
| 399 | readI16 = reader(__readZigZag) |
| 400 | readI32 = reader(__readZigZag) |
| 401 | readI64 = reader(__readZigZag) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 402 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 403 | @reader |
| 404 | def readDouble(self): |
| 405 | buff = self.trans.readAll(8) |
| 406 | val, = unpack('<d', buff) |
| 407 | return val |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 408 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 409 | def __readBinary(self): |
| 410 | size = self.__readSize() |
| 411 | self._check_string_length(size) |
| 412 | return self.trans.readAll(size) |
| 413 | readBinary = reader(__readBinary) |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 414 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 415 | def __getTType(self, byte): |
| 416 | return TTYPES[byte & 0x0f] |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 417 | |
| 418 | |
Nobuaki Sukegawa | b9c859a | 2015-12-21 01:10:25 +0900 | [diff] [blame] | 419 | class TCompactProtocolFactory(object): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 420 | def __init__(self, |
| 421 | string_length_limit=None, |
| 422 | container_length_limit=None): |
| 423 | self.string_length_limit = string_length_limit |
| 424 | self.container_length_limit = container_length_limit |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 425 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 426 | def getProtocol(self, trans): |
| 427 | return TCompactProtocol(trans, |
| 428 | self.string_length_limit, |
| 429 | self.container_length_limit) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 430 | |
| 431 | |
| 432 | class TCompactProtocolAccelerated(TCompactProtocol): |
| 433 | """C-Accelerated version of TCompactProtocol. |
| 434 | |
| 435 | This class does not override any of TCompactProtocol's methods, |
| 436 | but the generated code recognizes it directly and will call into |
| 437 | our C module to do the encoding, bypassing this object entirely. |
| 438 | We inherit from TCompactProtocol so that the normal TCompactProtocol |
| 439 | encoding can happen if the fastbinary module doesn't work for some |
| 440 | reason. |
| 441 | To disable this behavior, pass fallback=False constructor argument. |
| 442 | |
| 443 | In order to take advantage of the C module, just use |
| 444 | TCompactProtocolAccelerated instead of TCompactProtocol. |
| 445 | """ |
| 446 | pass |
| 447 | |
| 448 | def __init__(self, *args, **kwargs): |
| 449 | fallback = kwargs.pop('fallback', True) |
| 450 | super(TCompactProtocolAccelerated, self).__init__(*args, **kwargs) |
| 451 | try: |
| 452 | from thrift.protocol import fastbinary |
| 453 | except ImportError: |
| 454 | if not fallback: |
| 455 | raise |
| 456 | else: |
| 457 | self._fast_decode = fastbinary.decode_compact |
| 458 | self._fast_encode = fastbinary.encode_compact |
| 459 | |
| 460 | |
| 461 | class TCompactProtocolAcceleratedFactory(object): |
| 462 | def __init__(self, |
| 463 | string_length_limit=None, |
| 464 | container_length_limit=None, |
| 465 | fallback=True): |
| 466 | self.string_length_limit = string_length_limit |
| 467 | self.container_length_limit = container_length_limit |
| 468 | self._fallback = fallback |
| 469 | |
| 470 | def getProtocol(self, trans): |
| 471 | return TCompactProtocolAccelerated( |
| 472 | trans, |
| 473 | string_length_limit=self.string_length_limit, |
| 474 | container_length_limit=self.container_length_limit, |
| 475 | fallback=self._fallback) |