Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | # |
Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 19 | |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 20 | from thrift.Thrift import TException, TType, TFrozenDict |
Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 21 | from thrift.transport.TTransport import TTransportException |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 22 | from ..compat import binary_to_str, str_to_binary |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 23 | |
Nobuaki Sukegawa | 299255a | 2016-01-06 14:52:50 +0900 | [diff] [blame] | 24 | import six |
| 25 | import sys |
| 26 | from itertools import islice |
| 27 | from six.moves import zip |
| 28 | |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 29 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 30 | class TProtocolException(TException): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 31 | """Custom Protocol Exception class""" |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 32 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 33 | UNKNOWN = 0 |
| 34 | INVALID_DATA = 1 |
| 35 | NEGATIVE_SIZE = 2 |
| 36 | SIZE_LIMIT = 3 |
| 37 | BAD_VERSION = 4 |
| 38 | NOT_IMPLEMENTED = 5 |
| 39 | DEPTH_LIMIT = 6 |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 40 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 41 | def __init__(self, type=UNKNOWN, message=None): |
| 42 | TException.__init__(self, message) |
| 43 | self.type = type |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 44 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 45 | |
Nobuaki Sukegawa | b9c859a | 2015-12-21 01:10:25 +0900 | [diff] [blame] | 46 | class TProtocolBase(object): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 47 | """Base class for Thrift protocol driver.""" |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 48 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 49 | def __init__(self, trans): |
| 50 | self.trans = trans |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 51 | self._fast_decode = None |
| 52 | self._fast_encode = None |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 53 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 54 | @staticmethod |
| 55 | def _check_length(limit, length): |
| 56 | if length < 0: |
| 57 | raise TTransportException(TTransportException.NEGATIVE_SIZE, |
| 58 | 'Negative length: %d' % length) |
| 59 | if limit is not None and length > limit: |
| 60 | raise TTransportException(TTransportException.SIZE_LIMIT, |
| 61 | 'Length exceeded max allowed: %d' % limit) |
Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 62 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 63 | def writeMessageBegin(self, name, ttype, seqid): |
| 64 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 65 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 66 | def writeMessageEnd(self): |
| 67 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 68 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 69 | def writeStructBegin(self, name): |
| 70 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 71 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 72 | def writeStructEnd(self): |
| 73 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 74 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 75 | def writeFieldBegin(self, name, ttype, fid): |
| 76 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 77 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 78 | def writeFieldEnd(self): |
| 79 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 80 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 81 | def writeFieldStop(self): |
| 82 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 83 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 84 | def writeMapBegin(self, ktype, vtype, size): |
| 85 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 86 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 87 | def writeMapEnd(self): |
| 88 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 89 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 90 | def writeListBegin(self, etype, size): |
| 91 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 92 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 93 | def writeListEnd(self): |
| 94 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 95 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 96 | def writeSetBegin(self, etype, size): |
| 97 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 98 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 99 | def writeSetEnd(self): |
| 100 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 101 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 102 | def writeBool(self, bool_val): |
| 103 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 104 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 105 | def writeByte(self, byte): |
| 106 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 107 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 108 | def writeI16(self, i16): |
| 109 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 110 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 111 | def writeI32(self, i32): |
| 112 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 113 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 114 | def writeI64(self, i64): |
| 115 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 116 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 117 | def writeDouble(self, dub): |
| 118 | pass |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 119 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 120 | def writeString(self, str_val): |
| 121 | self.writeBinary(str_to_binary(str_val)) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 122 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 123 | def writeBinary(self, str_val): |
| 124 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 125 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 126 | def writeUtf8(self, str_val): |
| 127 | self.writeString(str_val.encode('utf8')) |
Nobuaki Sukegawa | 397bd51 | 2016-01-06 14:43:15 +0900 | [diff] [blame] | 128 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 129 | def readMessageBegin(self): |
| 130 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 131 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 132 | def readMessageEnd(self): |
| 133 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 134 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 135 | def readStructBegin(self): |
| 136 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 137 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 138 | def readStructEnd(self): |
| 139 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 140 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 141 | def readFieldBegin(self): |
| 142 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 143 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 144 | def readFieldEnd(self): |
| 145 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 146 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 147 | def readMapBegin(self): |
| 148 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 149 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 150 | def readMapEnd(self): |
| 151 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 152 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 153 | def readListBegin(self): |
| 154 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 155 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 156 | def readListEnd(self): |
| 157 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 158 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 159 | def readSetBegin(self): |
| 160 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 161 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 162 | def readSetEnd(self): |
| 163 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 164 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 165 | def readBool(self): |
| 166 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 167 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 168 | def readByte(self): |
| 169 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 170 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 171 | def readI16(self): |
| 172 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 173 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 174 | def readI32(self): |
| 175 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 176 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 177 | def readI64(self): |
| 178 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 179 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 180 | def readDouble(self): |
| 181 | pass |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 182 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 183 | def readString(self): |
| 184 | return binary_to_str(self.readBinary()) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 185 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 186 | def readBinary(self): |
| 187 | pass |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 188 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 189 | def readUtf8(self): |
| 190 | return self.readString().decode('utf8') |
Nobuaki Sukegawa | 397bd51 | 2016-01-06 14:43:15 +0900 | [diff] [blame] | 191 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 192 | def skip(self, ttype): |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 193 | if ttype == TType.STOP: |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 194 | return |
| 195 | elif ttype == TType.BOOL: |
| 196 | self.readBool() |
| 197 | elif ttype == TType.BYTE: |
| 198 | self.readByte() |
| 199 | elif ttype == TType.I16: |
| 200 | self.readI16() |
| 201 | elif ttype == TType.I32: |
| 202 | self.readI32() |
| 203 | elif ttype == TType.I64: |
| 204 | self.readI64() |
| 205 | elif ttype == TType.DOUBLE: |
| 206 | self.readDouble() |
| 207 | elif ttype == TType.STRING: |
| 208 | self.readString() |
| 209 | elif ttype == TType.STRUCT: |
| 210 | name = self.readStructBegin() |
| 211 | while True: |
| 212 | (name, ttype, id) = self.readFieldBegin() |
| 213 | if ttype == TType.STOP: |
| 214 | break |
| 215 | self.skip(ttype) |
| 216 | self.readFieldEnd() |
| 217 | self.readStructEnd() |
| 218 | elif ttype == TType.MAP: |
| 219 | (ktype, vtype, size) = self.readMapBegin() |
| 220 | for i in range(size): |
| 221 | self.skip(ktype) |
| 222 | self.skip(vtype) |
| 223 | self.readMapEnd() |
| 224 | elif ttype == TType.SET: |
| 225 | (etype, size) = self.readSetBegin() |
| 226 | for i in range(size): |
| 227 | self.skip(etype) |
| 228 | self.readSetEnd() |
| 229 | elif ttype == TType.LIST: |
| 230 | (etype, size) = self.readListBegin() |
| 231 | for i in range(size): |
| 232 | self.skip(etype) |
| 233 | self.readListEnd() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 234 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 235 | # tuple of: ( 'reader method' name, is_container bool, 'writer_method' name ) |
| 236 | _TTYPE_HANDLERS = ( |
| 237 | (None, None, False), # 0 TType.STOP |
| 238 | (None, None, False), # 1 TType.VOID # TODO: handle void? |
| 239 | ('readBool', 'writeBool', False), # 2 TType.BOOL |
| 240 | ('readByte', 'writeByte', False), # 3 TType.BYTE and I08 |
| 241 | ('readDouble', 'writeDouble', False), # 4 TType.DOUBLE |
| 242 | (None, None, False), # 5 undefined |
| 243 | ('readI16', 'writeI16', False), # 6 TType.I16 |
| 244 | (None, None, False), # 7 undefined |
| 245 | ('readI32', 'writeI32', False), # 8 TType.I32 |
| 246 | (None, None, False), # 9 undefined |
| 247 | ('readI64', 'writeI64', False), # 10 TType.I64 |
| 248 | ('readString', 'writeString', False), # 11 TType.STRING and UTF7 |
| 249 | ('readContainerStruct', 'writeContainerStruct', True), # 12 *.STRUCT |
| 250 | ('readContainerMap', 'writeContainerMap', True), # 13 TType.MAP |
| 251 | ('readContainerSet', 'writeContainerSet', True), # 14 TType.SET |
| 252 | ('readContainerList', 'writeContainerList', True), # 15 TType.LIST |
| 253 | (None, None, False), # 16 TType.UTF8 # TODO: handle utf8 types? |
| 254 | (None, None, False) # 17 TType.UTF16 # TODO: handle utf16 types? |
| 255 | ) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 256 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 257 | def _ttype_handlers(self, ttype, spec): |
| 258 | if spec == 'BINARY': |
| 259 | if ttype != TType.STRING: |
| 260 | raise TProtocolException(type=TProtocolException.INVALID_DATA, |
| 261 | message='Invalid binary field type %d' % ttype) |
| 262 | return ('readBinary', 'writeBinary', False) |
| 263 | if sys.version_info[0] == 2 and spec == 'UTF8': |
| 264 | if ttype != TType.STRING: |
| 265 | raise TProtocolException(type=TProtocolException.INVALID_DATA, |
| 266 | message='Invalid string field type %d' % ttype) |
| 267 | return ('readUtf8', 'writeUtf8', False) |
| 268 | return self._TTYPE_HANDLERS[ttype] if ttype < len(self._TTYPE_HANDLERS) else (None, None, False) |
Nobuaki Sukegawa | 299255a | 2016-01-06 14:52:50 +0900 | [diff] [blame] | 269 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 270 | def _read_by_ttype(self, ttype, spec, espec): |
Elvis Pranskevichus | 2d1fe88 | 2017-05-23 11:36:23 -0400 | [diff] [blame] | 271 | reader_name, _, is_container = self._ttype_handlers(ttype, espec) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 272 | if reader_name is None: |
| 273 | raise TProtocolException(type=TProtocolException.INVALID_DATA, |
| 274 | message='Invalid type %d' % (ttype)) |
| 275 | reader_func = getattr(self, reader_name) |
| 276 | read = (lambda: reader_func(espec)) if is_container else reader_func |
| 277 | while True: |
| 278 | yield read() |
Nobuaki Sukegawa | bc7e480 | 2015-11-14 19:23:27 +0900 | [diff] [blame] | 279 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 280 | def readFieldByTType(self, ttype, spec): |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 281 | return next(self._read_by_ttype(ttype, spec, spec)) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 282 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 283 | def readContainerList(self, spec): |
| 284 | ttype, tspec, is_immutable = spec |
| 285 | (list_type, list_len) = self.readListBegin() |
| 286 | # TODO: compare types we just decoded with thrift_spec |
| 287 | elems = islice(self._read_by_ttype(ttype, spec, tspec), list_len) |
| 288 | results = (tuple if is_immutable else list)(elems) |
| 289 | self.readListEnd() |
| 290 | return results |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 291 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 292 | def readContainerSet(self, spec): |
| 293 | ttype, tspec, is_immutable = spec |
| 294 | (set_type, set_len) = self.readSetBegin() |
| 295 | # TODO: compare types we just decoded with thrift_spec |
| 296 | elems = islice(self._read_by_ttype(ttype, spec, tspec), set_len) |
| 297 | results = (frozenset if is_immutable else set)(elems) |
| 298 | self.readSetEnd() |
| 299 | return results |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 300 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 301 | def readContainerStruct(self, spec): |
| 302 | (obj_class, obj_spec) = spec |
| 303 | obj = obj_class() |
| 304 | obj.read(self) |
| 305 | return obj |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 306 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 307 | def readContainerMap(self, spec): |
| 308 | ktype, kspec, vtype, vspec, is_immutable = spec |
| 309 | (map_ktype, map_vtype, map_len) = self.readMapBegin() |
| 310 | # TODO: compare types we just decoded with thrift_spec and |
| 311 | # abort/skip if types disagree |
| 312 | keys = self._read_by_ttype(ktype, spec, kspec) |
| 313 | vals = self._read_by_ttype(vtype, spec, vspec) |
| 314 | keyvals = islice(zip(keys, vals), map_len) |
| 315 | results = (TFrozenDict if is_immutable else dict)(keyvals) |
| 316 | self.readMapEnd() |
| 317 | return results |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 318 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 319 | def readStruct(self, obj, thrift_spec, is_immutable=False): |
| 320 | if is_immutable: |
| 321 | fields = {} |
| 322 | self.readStructBegin() |
| 323 | while True: |
| 324 | (fname, ftype, fid) = self.readFieldBegin() |
| 325 | if ftype == TType.STOP: |
| 326 | break |
| 327 | try: |
| 328 | field = thrift_spec[fid] |
| 329 | except IndexError: |
| 330 | self.skip(ftype) |
| 331 | else: |
| 332 | if field is not None and ftype == field[1]: |
| 333 | fname = field[2] |
| 334 | fspec = field[3] |
| 335 | val = self.readFieldByTType(ftype, fspec) |
| 336 | if is_immutable: |
| 337 | fields[fname] = val |
| 338 | else: |
| 339 | setattr(obj, fname, val) |
| 340 | else: |
| 341 | self.skip(ftype) |
| 342 | self.readFieldEnd() |
| 343 | self.readStructEnd() |
| 344 | if is_immutable: |
| 345 | return obj(**fields) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 346 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 347 | def writeContainerStruct(self, val, spec): |
| 348 | val.write(self) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 349 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 350 | def writeContainerList(self, val, spec): |
| 351 | ttype, tspec, _ = spec |
| 352 | self.writeListBegin(ttype, len(val)) |
| 353 | for _ in self._write_by_ttype(ttype, val, spec, tspec): |
| 354 | pass |
| 355 | self.writeListEnd() |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 356 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 357 | def writeContainerSet(self, val, spec): |
| 358 | ttype, tspec, _ = spec |
| 359 | self.writeSetBegin(ttype, len(val)) |
| 360 | for _ in self._write_by_ttype(ttype, val, spec, tspec): |
| 361 | pass |
| 362 | self.writeSetEnd() |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 363 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 364 | def writeContainerMap(self, val, spec): |
| 365 | ktype, kspec, vtype, vspec, _ = spec |
| 366 | self.writeMapBegin(ktype, vtype, len(val)) |
| 367 | for _ in zip(self._write_by_ttype(ktype, six.iterkeys(val), spec, kspec), |
| 368 | self._write_by_ttype(vtype, six.itervalues(val), spec, vspec)): |
| 369 | pass |
| 370 | self.writeMapEnd() |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 371 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 372 | def writeStruct(self, obj, thrift_spec): |
| 373 | self.writeStructBegin(obj.__class__.__name__) |
| 374 | for field in thrift_spec: |
| 375 | if field is None: |
| 376 | continue |
| 377 | fname = field[2] |
| 378 | val = getattr(obj, fname) |
| 379 | if val is None: |
| 380 | # skip writing out unset fields |
| 381 | continue |
| 382 | fid = field[0] |
| 383 | ftype = field[1] |
| 384 | fspec = field[3] |
| 385 | self.writeFieldBegin(fname, ftype, fid) |
| 386 | self.writeFieldByTType(ftype, val, fspec) |
| 387 | self.writeFieldEnd() |
| 388 | self.writeFieldStop() |
| 389 | self.writeStructEnd() |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 390 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 391 | def _write_by_ttype(self, ttype, vals, spec, espec): |
Elvis Pranskevichus | 2d1fe88 | 2017-05-23 11:36:23 -0400 | [diff] [blame] | 392 | _, writer_name, is_container = self._ttype_handlers(ttype, espec) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 393 | writer_func = getattr(self, writer_name) |
| 394 | write = (lambda v: writer_func(v, espec)) if is_container else writer_func |
| 395 | for v in vals: |
| 396 | yield write(v) |
Nobuaki Sukegawa | 299255a | 2016-01-06 14:52:50 +0900 | [diff] [blame] | 397 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 398 | def writeFieldByTType(self, ttype, val, spec): |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 399 | next(self._write_by_ttype(ttype, [val], spec, spec)) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 400 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 401 | |
Konrad Grochowski | 93fea15 | 2014-10-02 16:29:14 +0200 | [diff] [blame] | 402 | def checkIntegerLimits(i, bits): |
Konrad Grochowski | 0721867 | 2014-10-09 12:18:25 +0200 | [diff] [blame] | 403 | if bits == 8 and (i < -128 or i > 127): |
Konrad Grochowski | 93fea15 | 2014-10-02 16:29:14 +0200 | [diff] [blame] | 404 | raise TProtocolException(TProtocolException.INVALID_DATA, |
Konrad Grochowski | d5f3be5 | 2014-10-08 15:32:21 +0200 | [diff] [blame] | 405 | "i8 requires -128 <= number <= 127") |
Konrad Grochowski | 0721867 | 2014-10-09 12:18:25 +0200 | [diff] [blame] | 406 | elif bits == 16 and (i < -32768 or i > 32767): |
Konrad Grochowski | d5f3be5 | 2014-10-08 15:32:21 +0200 | [diff] [blame] | 407 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 408 | "i16 requires -32768 <= number <= 32767") |
Konrad Grochowski | 0721867 | 2014-10-09 12:18:25 +0200 | [diff] [blame] | 409 | elif bits == 32 and (i < -2147483648 or i > 2147483647): |
Konrad Grochowski | d5f3be5 | 2014-10-08 15:32:21 +0200 | [diff] [blame] | 410 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 411 | "i32 requires -2147483648 <= number <= 2147483647") |
Konrad Grochowski | 0721867 | 2014-10-09 12:18:25 +0200 | [diff] [blame] | 412 | elif bits == 64 and (i < -9223372036854775808 or i > 9223372036854775807): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 413 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 414 | "i64 requires -9223372036854775808 <= number <= 9223372036854775807") |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 415 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 416 | |
Nobuaki Sukegawa | b9c859a | 2015-12-21 01:10:25 +0900 | [diff] [blame] | 417 | class TProtocolFactory(object): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 418 | def getProtocol(self, trans): |
| 419 | pass |