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