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 | |
Mark Slee | e74306a | 2007-02-21 05:38:12 +0000 | [diff] [blame] | 20 | from thrift.Thrift import * |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 21 | |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 22 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 23 | class TProtocolException(TException): |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 24 | """Custom Protocol Exception class""" |
| 25 | |
| 26 | UNKNOWN = 0 |
| 27 | INVALID_DATA = 1 |
| 28 | NEGATIVE_SIZE = 2 |
| 29 | SIZE_LIMIT = 3 |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 30 | BAD_VERSION = 4 |
Jens Geyer | 6d1a83a | 2014-05-03 00:49:05 +0200 | [diff] [blame] | 31 | NOT_IMPLEMENTED = 5 |
| 32 | DEPTH_LIMIT = 6 |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 33 | |
| 34 | def __init__(self, type=UNKNOWN, message=None): |
| 35 | TException.__init__(self, message) |
| 36 | self.type = type |
| 37 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 38 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 39 | class TProtocolBase: |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 40 | """Base class for Thrift protocol driver.""" |
| 41 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 42 | def __init__(self, trans): |
| 43 | self.trans = trans |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 44 | |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 45 | def writeMessageBegin(self, name, ttype, seqid): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 46 | pass |
| 47 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 48 | def writeMessageEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 49 | pass |
| 50 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 51 | def writeStructBegin(self, name): |
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 writeStructEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 55 | pass |
| 56 | |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 57 | def writeFieldBegin(self, name, ttype, fid): |
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 writeFieldEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 61 | pass |
| 62 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 63 | def writeFieldStop(self): |
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 writeMapBegin(self, ktype, vtype, size): |
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 writeMapEnd(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 writeListBegin(self, etype, 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 writeListEnd(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 writeSetBegin(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 writeSetEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 82 | pass |
| 83 | |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 84 | def writeBool(self, bool_val): |
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 writeByte(self, byte): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 88 | pass |
| 89 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 90 | def writeI16(self, i16): |
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 writeI32(self, i32): |
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 writeI64(self, i64): |
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 writeDouble(self, dub): |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 100 | pass |
| 101 | |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 102 | def writeString(self, str_val): |
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 readMessageBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 106 | pass |
| 107 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 108 | def readMessageEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 109 | pass |
| 110 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 111 | def readStructBegin(self): |
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 readStructEnd(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 readFieldBegin(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 readFieldEnd(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 readMapBegin(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 readMapEnd(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 readListBegin(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 readListEnd(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 readSetBegin(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 readSetEnd(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 readBool(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 readByte(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 readI16(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 readI32(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 readI64(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 readDouble(self): |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 157 | pass |
| 158 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 159 | def readString(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 160 | pass |
| 161 | |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 162 | def skip(self, ttype): |
| 163 | if ttype == TType.STOP: |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 164 | return |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 165 | elif ttype == TType.BOOL: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 166 | self.readBool() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 167 | elif ttype == TType.BYTE: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 168 | self.readByte() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 169 | elif ttype == TType.I16: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 170 | self.readI16() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 171 | elif ttype == TType.I32: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 172 | self.readI32() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 173 | elif ttype == TType.I64: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 174 | self.readI64() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 175 | elif ttype == TType.DOUBLE: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 176 | self.readDouble() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 177 | elif ttype == TType.STRING: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 178 | self.readString() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 179 | elif ttype == TType.STRUCT: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 180 | name = self.readStructBegin() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 181 | while True: |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 182 | (name, ttype, id) = self.readFieldBegin() |
| 183 | if ttype == TType.STOP: |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 184 | break |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 185 | self.skip(ttype) |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 186 | self.readFieldEnd() |
| 187 | self.readStructEnd() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 188 | elif ttype == TType.MAP: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 189 | (ktype, vtype, size) = self.readMapBegin() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 190 | for i in xrange(size): |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 191 | self.skip(ktype) |
| 192 | self.skip(vtype) |
| 193 | self.readMapEnd() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 194 | elif ttype == TType.SET: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 195 | (etype, size) = self.readSetBegin() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 196 | for i in xrange(size): |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 197 | self.skip(etype) |
| 198 | self.readSetEnd() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 199 | elif ttype == TType.LIST: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 200 | (etype, size) = self.readListBegin() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 201 | for i in xrange(size): |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 202 | self.skip(etype) |
| 203 | self.readListEnd() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 204 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 205 | # tuple of: ( 'reader method' name, is_container bool, 'writer_method' name ) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 206 | _TTYPE_HANDLERS = ( |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 207 | (None, None, False), # 0 TType.STOP |
| 208 | (None, None, False), # 1 TType.VOID # TODO: handle void? |
| 209 | ('readBool', 'writeBool', False), # 2 TType.BOOL |
| 210 | ('readByte', 'writeByte', False), # 3 TType.BYTE and I08 |
| 211 | ('readDouble', 'writeDouble', False), # 4 TType.DOUBLE |
| 212 | (None, None, False), # 5 undefined |
| 213 | ('readI16', 'writeI16', False), # 6 TType.I16 |
| 214 | (None, None, False), # 7 undefined |
| 215 | ('readI32', 'writeI32', False), # 8 TType.I32 |
| 216 | (None, None, False), # 9 undefined |
| 217 | ('readI64', 'writeI64', False), # 10 TType.I64 |
| 218 | ('readString', 'writeString', False), # 11 TType.STRING and UTF7 |
| 219 | ('readContainerStruct', 'writeContainerStruct', True), # 12 *.STRUCT |
| 220 | ('readContainerMap', 'writeContainerMap', True), # 13 TType.MAP |
| 221 | ('readContainerSet', 'writeContainerSet', True), # 14 TType.SET |
| 222 | ('readContainerList', 'writeContainerList', True), # 15 TType.LIST |
| 223 | (None, None, False), # 16 TType.UTF8 # TODO: handle utf8 types? |
| 224 | (None, None, False) # 17 TType.UTF16 # TODO: handle utf16 types? |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 225 | ) |
| 226 | |
| 227 | def readFieldByTType(self, ttype, spec): |
| 228 | try: |
| 229 | (r_handler, w_handler, is_container) = self._TTYPE_HANDLERS[ttype] |
| 230 | except IndexError: |
| 231 | raise TProtocolException(type=TProtocolException.INVALID_DATA, |
| 232 | message='Invalid field type %d' % (ttype)) |
| 233 | if r_handler is None: |
| 234 | raise TProtocolException(type=TProtocolException.INVALID_DATA, |
| 235 | message='Invalid field type %d' % (ttype)) |
| 236 | reader = getattr(self, r_handler) |
| 237 | if not is_container: |
| 238 | return reader() |
| 239 | return reader(spec) |
| 240 | |
| 241 | def readContainerList(self, spec): |
| 242 | results = [] |
| 243 | ttype, tspec = spec[0], spec[1] |
| 244 | r_handler = self._TTYPE_HANDLERS[ttype][0] |
| 245 | reader = getattr(self, r_handler) |
| 246 | (list_type, list_len) = self.readListBegin() |
| 247 | if tspec is None: |
| 248 | # list values are simple types |
| 249 | for idx in xrange(list_len): |
| 250 | results.append(reader()) |
| 251 | else: |
| 252 | # this is like an inlined readFieldByTType |
| 253 | container_reader = self._TTYPE_HANDLERS[list_type][0] |
| 254 | val_reader = getattr(self, container_reader) |
| 255 | for idx in xrange(list_len): |
| 256 | val = val_reader(tspec) |
| 257 | results.append(val) |
| 258 | self.readListEnd() |
| 259 | return results |
| 260 | |
| 261 | def readContainerSet(self, spec): |
| 262 | results = set() |
| 263 | ttype, tspec = spec[0], spec[1] |
| 264 | r_handler = self._TTYPE_HANDLERS[ttype][0] |
| 265 | reader = getattr(self, r_handler) |
| 266 | (set_type, set_len) = self.readSetBegin() |
| 267 | if tspec is None: |
| 268 | # set members are simple types |
| 269 | for idx in xrange(set_len): |
| 270 | results.add(reader()) |
| 271 | else: |
| 272 | container_reader = self._TTYPE_HANDLERS[set_type][0] |
| 273 | val_reader = getattr(self, container_reader) |
| 274 | for idx in xrange(set_len): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 275 | results.add(val_reader(tspec)) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 276 | self.readSetEnd() |
| 277 | return results |
| 278 | |
| 279 | def readContainerStruct(self, spec): |
| 280 | (obj_class, obj_spec) = spec |
| 281 | obj = obj_class() |
| 282 | obj.read(self) |
| 283 | return obj |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 284 | |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 285 | def readContainerMap(self, spec): |
| 286 | results = dict() |
| 287 | key_ttype, key_spec = spec[0], spec[1] |
| 288 | val_ttype, val_spec = spec[2], spec[3] |
| 289 | (map_ktype, map_vtype, map_len) = self.readMapBegin() |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 290 | # TODO: compare types we just decoded with thrift_spec and |
| 291 | # abort/skip if types disagree |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 292 | key_reader = getattr(self, self._TTYPE_HANDLERS[key_ttype][0]) |
| 293 | val_reader = getattr(self, self._TTYPE_HANDLERS[val_ttype][0]) |
| 294 | # list values are simple types |
| 295 | for idx in xrange(map_len): |
| 296 | if key_spec is None: |
| 297 | k_val = key_reader() |
| 298 | else: |
| 299 | k_val = self.readFieldByTType(key_ttype, key_spec) |
| 300 | if val_spec is None: |
| 301 | v_val = val_reader() |
| 302 | else: |
| 303 | v_val = self.readFieldByTType(val_ttype, val_spec) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 304 | # this raises a TypeError with unhashable keys types |
| 305 | # i.e. this fails: d=dict(); d[[0,1]] = 2 |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 306 | results[k_val] = v_val |
| 307 | self.readMapEnd() |
| 308 | return results |
| 309 | |
| 310 | def readStruct(self, obj, thrift_spec): |
| 311 | self.readStructBegin() |
| 312 | while True: |
| 313 | (fname, ftype, fid) = self.readFieldBegin() |
| 314 | if ftype == TType.STOP: |
| 315 | break |
| 316 | try: |
| 317 | field = thrift_spec[fid] |
| 318 | except IndexError: |
| 319 | self.skip(ftype) |
| 320 | else: |
| 321 | if field is not None and ftype == field[1]: |
| 322 | fname = field[2] |
| 323 | fspec = field[3] |
| 324 | val = self.readFieldByTType(ftype, fspec) |
| 325 | setattr(obj, fname, val) |
| 326 | else: |
| 327 | self.skip(ftype) |
| 328 | self.readFieldEnd() |
| 329 | self.readStructEnd() |
| 330 | |
| 331 | def writeContainerStruct(self, val, spec): |
| 332 | val.write(self) |
| 333 | |
| 334 | def writeContainerList(self, val, spec): |
| 335 | self.writeListBegin(spec[0], len(val)) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 336 | r_handler, w_handler, is_container = self._TTYPE_HANDLERS[spec[0]] |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 337 | e_writer = getattr(self, w_handler) |
| 338 | if not is_container: |
| 339 | for elem in val: |
| 340 | e_writer(elem) |
| 341 | else: |
| 342 | for elem in val: |
| 343 | e_writer(elem, spec[1]) |
| 344 | self.writeListEnd() |
| 345 | |
| 346 | def writeContainerSet(self, val, spec): |
| 347 | self.writeSetBegin(spec[0], len(val)) |
| 348 | r_handler, w_handler, is_container = self._TTYPE_HANDLERS[spec[0]] |
| 349 | e_writer = getattr(self, w_handler) |
| 350 | if not is_container: |
| 351 | for elem in val: |
| 352 | e_writer(elem) |
| 353 | else: |
| 354 | for elem in val: |
| 355 | e_writer(elem, spec[1]) |
| 356 | self.writeSetEnd() |
| 357 | |
| 358 | def writeContainerMap(self, val, spec): |
| 359 | k_type = spec[0] |
| 360 | v_type = spec[2] |
| 361 | ignore, ktype_name, k_is_container = self._TTYPE_HANDLERS[k_type] |
| 362 | ignore, vtype_name, v_is_container = self._TTYPE_HANDLERS[v_type] |
| 363 | k_writer = getattr(self, ktype_name) |
| 364 | v_writer = getattr(self, vtype_name) |
| 365 | self.writeMapBegin(k_type, v_type, len(val)) |
| 366 | for m_key, m_val in val.iteritems(): |
| 367 | if not k_is_container: |
| 368 | k_writer(m_key) |
| 369 | else: |
| 370 | k_writer(m_key, spec[1]) |
| 371 | if not v_is_container: |
| 372 | v_writer(m_val) |
| 373 | else: |
| 374 | v_writer(m_val, spec[3]) |
| 375 | self.writeMapEnd() |
| 376 | |
| 377 | def writeStruct(self, obj, thrift_spec): |
| 378 | self.writeStructBegin(obj.__class__.__name__) |
| 379 | for field in thrift_spec: |
| 380 | if field is None: |
| 381 | continue |
| 382 | fname = field[2] |
| 383 | val = getattr(obj, fname) |
| 384 | if val is None: |
| 385 | # skip writing out unset fields |
| 386 | continue |
| 387 | fid = field[0] |
| 388 | ftype = field[1] |
| 389 | fspec = field[3] |
| 390 | # get the writer method for this value |
| 391 | self.writeFieldBegin(fname, ftype, fid) |
| 392 | self.writeFieldByTType(ftype, val, fspec) |
| 393 | self.writeFieldEnd() |
| 394 | self.writeFieldStop() |
| 395 | self.writeStructEnd() |
| 396 | |
| 397 | def writeFieldByTType(self, ttype, val, spec): |
| 398 | r_handler, w_handler, is_container = self._TTYPE_HANDLERS[ttype] |
| 399 | writer = getattr(self, w_handler) |
| 400 | if is_container: |
| 401 | writer(val, spec) |
| 402 | else: |
| 403 | writer(val) |
| 404 | |
Konrad Grochowski | 93fea15 | 2014-10-02 16:29:14 +0200 | [diff] [blame] | 405 | def checkIntegerLimits(i, bits): |
Konrad Grochowski | 0721867 | 2014-10-09 12:18:25 +0200 | [diff] [blame^] | 406 | if bits == 8 and (i < -128 or i > 127): |
Konrad Grochowski | 93fea15 | 2014-10-02 16:29:14 +0200 | [diff] [blame] | 407 | raise TProtocolException(TProtocolException.INVALID_DATA, |
Konrad Grochowski | d5f3be5 | 2014-10-08 15:32:21 +0200 | [diff] [blame] | 408 | "i8 requires -128 <= number <= 127") |
Konrad Grochowski | 0721867 | 2014-10-09 12:18:25 +0200 | [diff] [blame^] | 409 | elif bits == 16 and (i < -32768 or i > 32767): |
Konrad Grochowski | d5f3be5 | 2014-10-08 15:32:21 +0200 | [diff] [blame] | 410 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 411 | "i16 requires -32768 <= number <= 32767") |
Konrad Grochowski | 0721867 | 2014-10-09 12:18:25 +0200 | [diff] [blame^] | 412 | elif bits == 32 and (i < -2147483648 or i > 2147483647): |
Konrad Grochowski | d5f3be5 | 2014-10-08 15:32:21 +0200 | [diff] [blame] | 413 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 414 | "i32 requires -2147483648 <= number <= 2147483647") |
Konrad Grochowski | 0721867 | 2014-10-09 12:18:25 +0200 | [diff] [blame^] | 415 | elif bits == 64 and (i < -9223372036854775808 or i > 9223372036854775807): |
Konrad Grochowski | d5f3be5 | 2014-10-08 15:32:21 +0200 | [diff] [blame] | 416 | raise TProtocolException(TProtocolException.INVALID_DATA, |
| 417 | "i64 requires -9223372036854775808 <= number <= 9223372036854775807") |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 418 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 419 | class TProtocolFactory: |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 420 | def getProtocol(self, trans): |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 421 | pass |