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