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