blob: 450e0fa7bacac0a90fe23e29d9a8ba7f2b1a4f46 [file] [log] [blame]
Mark Slee89e2bb82007-03-01 00:20:36 +00001#
David Reissea2cba82009-03-30 21:35:00 +00002# 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 Slee89e2bb82007-03-01 00:20:36 +000019
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090020from thrift.Thrift import TException, TType, TFrozenDict
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090021from ..compat import binary_to_str, str_to_binary
Mark Sleecde2b612006-09-03 21:13:07 +000022
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +090023import six
24import sys
25from itertools import islice
26from six.moves import zip
27
Mark Slee92195ae2007-02-21 05:16:30 +000028
Bryan Duxbury69720412012-01-03 17:32:30 +000029class TProtocolException(TException):
Mark Slee92195ae2007-02-21 05:16:30 +000030 """Custom Protocol Exception class"""
31
32 UNKNOWN = 0
33 INVALID_DATA = 1
34 NEGATIVE_SIZE = 2
35 SIZE_LIMIT = 3
Mark Slee808454e2007-06-20 21:51:57 +000036 BAD_VERSION = 4
Jens Geyer6d1a83a2014-05-03 00:49:05 +020037 NOT_IMPLEMENTED = 5
38 DEPTH_LIMIT = 6
Mark Slee92195ae2007-02-21 05:16:30 +000039
40 def __init__(self, type=UNKNOWN, message=None):
41 TException.__init__(self, message)
42 self.type = type
43
Mark Sleecde2b612006-09-03 21:13:07 +000044
Nobuaki Sukegawab9c859a2015-12-21 01:10:25 +090045class TProtocolBase(object):
Mark Sleecde2b612006-09-03 21:13:07 +000046 """Base class for Thrift protocol driver."""
47
Aditya Agarwal5c468192007-02-06 01:14:33 +000048 def __init__(self, trans):
49 self.trans = trans
Mark Slee4ac459f2006-10-25 21:39:01 +000050
Roger Meierad8154a2012-12-18 21:02:16 +010051 def writeMessageBegin(self, name, ttype, seqid):
Mark Sleecde2b612006-09-03 21:13:07 +000052 pass
53
Mark Slee4ac459f2006-10-25 21:39:01 +000054 def writeMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000055 pass
56
Mark Slee4ac459f2006-10-25 21:39:01 +000057 def writeStructBegin(self, name):
Mark Sleecde2b612006-09-03 21:13:07 +000058 pass
59
Mark Slee4ac459f2006-10-25 21:39:01 +000060 def writeStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000061 pass
62
Roger Meierad8154a2012-12-18 21:02:16 +010063 def writeFieldBegin(self, name, ttype, fid):
Mark Sleecde2b612006-09-03 21:13:07 +000064 pass
65
Mark Slee4ac459f2006-10-25 21:39:01 +000066 def writeFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000067 pass
68
Mark Slee4ac459f2006-10-25 21:39:01 +000069 def writeFieldStop(self):
Mark Sleecde2b612006-09-03 21:13:07 +000070 pass
71
Mark Slee4ac459f2006-10-25 21:39:01 +000072 def writeMapBegin(self, ktype, vtype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000073 pass
74
Mark Slee4ac459f2006-10-25 21:39:01 +000075 def writeMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000076 pass
77
Mark Slee4ac459f2006-10-25 21:39:01 +000078 def writeListBegin(self, etype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000079 pass
80
Mark Slee4ac459f2006-10-25 21:39:01 +000081 def writeListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000082 pass
83
Mark Slee4ac459f2006-10-25 21:39:01 +000084 def writeSetBegin(self, etype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000085 pass
86
Mark Slee4ac459f2006-10-25 21:39:01 +000087 def writeSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000088 pass
89
Roger Meierad8154a2012-12-18 21:02:16 +010090 def writeBool(self, bool_val):
Mark Sleecde2b612006-09-03 21:13:07 +000091 pass
92
Mark Slee4ac459f2006-10-25 21:39:01 +000093 def writeByte(self, byte):
Mark Sleecde2b612006-09-03 21:13:07 +000094 pass
95
Mark Slee4ac459f2006-10-25 21:39:01 +000096 def writeI16(self, i16):
Mark Sleecde2b612006-09-03 21:13:07 +000097 pass
98
Mark Slee4ac459f2006-10-25 21:39:01 +000099 def writeI32(self, i32):
Mark Sleecde2b612006-09-03 21:13:07 +0000100 pass
101
Mark Slee4ac459f2006-10-25 21:39:01 +0000102 def writeI64(self, i64):
Mark Sleecde2b612006-09-03 21:13:07 +0000103 pass
104
Mark Slee4ac459f2006-10-25 21:39:01 +0000105 def writeDouble(self, dub):
Mark Sleec98d0502006-09-06 02:42:25 +0000106 pass
107
Roger Meierad8154a2012-12-18 21:02:16 +0100108 def writeString(self, str_val):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900109 self.writeBinary(str_to_binary(str_val))
110
111 def writeBinary(self, str_val):
Mark Sleecde2b612006-09-03 21:13:07 +0000112 pass
113
Nobuaki Sukegawa397bd512016-01-06 14:43:15 +0900114 def writeUtf8(self, str_val):
115 self.writeString(str_val.encode('utf8'))
116
Mark Slee4ac459f2006-10-25 21:39:01 +0000117 def readMessageBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000118 pass
119
Mark Slee4ac459f2006-10-25 21:39:01 +0000120 def readMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000121 pass
122
Mark Slee4ac459f2006-10-25 21:39:01 +0000123 def readStructBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000124 pass
125
Mark Slee4ac459f2006-10-25 21:39:01 +0000126 def readStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000127 pass
128
Mark Slee4ac459f2006-10-25 21:39:01 +0000129 def readFieldBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000130 pass
131
Mark Slee4ac459f2006-10-25 21:39:01 +0000132 def readFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000133 pass
134
Mark Slee4ac459f2006-10-25 21:39:01 +0000135 def readMapBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000136 pass
137
Mark Slee4ac459f2006-10-25 21:39:01 +0000138 def readMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000139 pass
140
Mark Slee4ac459f2006-10-25 21:39:01 +0000141 def readListBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000142 pass
143
Mark Slee4ac459f2006-10-25 21:39:01 +0000144 def readListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000145 pass
146
Mark Slee4ac459f2006-10-25 21:39:01 +0000147 def readSetBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000148 pass
149
Mark Slee4ac459f2006-10-25 21:39:01 +0000150 def readSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000151 pass
152
Mark Slee4ac459f2006-10-25 21:39:01 +0000153 def readBool(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000154 pass
155
Mark Slee4ac459f2006-10-25 21:39:01 +0000156 def readByte(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000157 pass
158
Mark Slee4ac459f2006-10-25 21:39:01 +0000159 def readI16(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000160 pass
161
Mark Slee4ac459f2006-10-25 21:39:01 +0000162 def readI32(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000163 pass
164
Mark Slee4ac459f2006-10-25 21:39:01 +0000165 def readI64(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000166 pass
167
Mark Slee4ac459f2006-10-25 21:39:01 +0000168 def readDouble(self):
Mark Sleec98d0502006-09-06 02:42:25 +0000169 pass
170
Mark Slee4ac459f2006-10-25 21:39:01 +0000171 def readString(self):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900172 return binary_to_str(self.readBinary())
173
174 def readBinary(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000175 pass
176
Nobuaki Sukegawa397bd512016-01-06 14:43:15 +0900177 def readUtf8(self):
178 return self.readString().decode('utf8')
179
Roger Meierad8154a2012-12-18 21:02:16 +0100180 def skip(self, ttype):
181 if ttype == TType.STOP:
Mark Sleecde2b612006-09-03 21:13:07 +0000182 return
Roger Meierad8154a2012-12-18 21:02:16 +0100183 elif ttype == TType.BOOL:
Mark Slee4ac459f2006-10-25 21:39:01 +0000184 self.readBool()
Roger Meierad8154a2012-12-18 21:02:16 +0100185 elif ttype == TType.BYTE:
Mark Slee4ac459f2006-10-25 21:39:01 +0000186 self.readByte()
Roger Meierad8154a2012-12-18 21:02:16 +0100187 elif ttype == TType.I16:
Mark Slee4ac459f2006-10-25 21:39:01 +0000188 self.readI16()
Roger Meierad8154a2012-12-18 21:02:16 +0100189 elif ttype == TType.I32:
Mark Slee4ac459f2006-10-25 21:39:01 +0000190 self.readI32()
Roger Meierad8154a2012-12-18 21:02:16 +0100191 elif ttype == TType.I64:
Mark Slee4ac459f2006-10-25 21:39:01 +0000192 self.readI64()
Roger Meierad8154a2012-12-18 21:02:16 +0100193 elif ttype == TType.DOUBLE:
Mark Slee4ac459f2006-10-25 21:39:01 +0000194 self.readDouble()
Roger Meierad8154a2012-12-18 21:02:16 +0100195 elif ttype == TType.STRING:
Mark Slee4ac459f2006-10-25 21:39:01 +0000196 self.readString()
Roger Meierad8154a2012-12-18 21:02:16 +0100197 elif ttype == TType.STRUCT:
Mark Slee4ac459f2006-10-25 21:39:01 +0000198 name = self.readStructBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000199 while True:
Roger Meierad8154a2012-12-18 21:02:16 +0100200 (name, ttype, id) = self.readFieldBegin()
201 if ttype == TType.STOP:
Mark Sleecde2b612006-09-03 21:13:07 +0000202 break
Roger Meierad8154a2012-12-18 21:02:16 +0100203 self.skip(ttype)
Mark Slee4ac459f2006-10-25 21:39:01 +0000204 self.readFieldEnd()
205 self.readStructEnd()
Roger Meierad8154a2012-12-18 21:02:16 +0100206 elif ttype == TType.MAP:
Mark Slee4ac459f2006-10-25 21:39:01 +0000207 (ktype, vtype, size) = self.readMapBegin()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900208 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000209 self.skip(ktype)
210 self.skip(vtype)
211 self.readMapEnd()
Roger Meierad8154a2012-12-18 21:02:16 +0100212 elif ttype == TType.SET:
Mark Slee4ac459f2006-10-25 21:39:01 +0000213 (etype, size) = self.readSetBegin()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900214 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000215 self.skip(etype)
216 self.readSetEnd()
Roger Meierad8154a2012-12-18 21:02:16 +0100217 elif ttype == TType.LIST:
Mark Slee4ac459f2006-10-25 21:39:01 +0000218 (etype, size) = self.readListBegin()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900219 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000220 self.skip(etype)
221 self.readListEnd()
Mark Sleecde2b612006-09-03 21:13:07 +0000222
Bryan Duxbury69720412012-01-03 17:32:30 +0000223 # tuple of: ( 'reader method' name, is_container bool, 'writer_method' name )
Roger Meierf4eec7a2011-09-11 18:16:21 +0000224 _TTYPE_HANDLERS = (
Bryan Duxbury69720412012-01-03 17:32:30 +0000225 (None, None, False), # 0 TType.STOP
226 (None, None, False), # 1 TType.VOID # TODO: handle void?
227 ('readBool', 'writeBool', False), # 2 TType.BOOL
Nobuaki Sukegawabc7e4802015-11-14 19:23:27 +0900228 ('readByte', 'writeByte', False), # 3 TType.BYTE and I08
Bryan Duxbury69720412012-01-03 17:32:30 +0000229 ('readDouble', 'writeDouble', False), # 4 TType.DOUBLE
230 (None, None, False), # 5 undefined
231 ('readI16', 'writeI16', False), # 6 TType.I16
232 (None, None, False), # 7 undefined
233 ('readI32', 'writeI32', False), # 8 TType.I32
234 (None, None, False), # 9 undefined
235 ('readI64', 'writeI64', False), # 10 TType.I64
236 ('readString', 'writeString', False), # 11 TType.STRING and UTF7
237 ('readContainerStruct', 'writeContainerStruct', True), # 12 *.STRUCT
238 ('readContainerMap', 'writeContainerMap', True), # 13 TType.MAP
239 ('readContainerSet', 'writeContainerSet', True), # 14 TType.SET
240 ('readContainerList', 'writeContainerList', True), # 15 TType.LIST
241 (None, None, False), # 16 TType.UTF8 # TODO: handle utf8 types?
242 (None, None, False) # 17 TType.UTF16 # TODO: handle utf16 types?
Roger Meierf4eec7a2011-09-11 18:16:21 +0000243 )
244
Nobuaki Sukegawabc7e4802015-11-14 19:23:27 +0900245 def _ttype_handlers(self, ttype, spec):
246 if spec == 'BINARY':
247 if ttype != TType.STRING:
248 raise TProtocolException(type=TProtocolException.INVALID_DATA,
249 message='Invalid binary field type %d' % ttype)
250 return ('readBinary', 'writeBinary', False)
Nobuaki Sukegawa397bd512016-01-06 14:43:15 +0900251 if sys.version_info[0] == 2 and spec == 'UTF8':
252 if ttype != TType.STRING:
253 raise TProtocolException(type=TProtocolException.INVALID_DATA,
254 message='Invalid string field type %d' % ttype)
255 return ('readUtf8', 'writeUtf8', False)
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900256 return self._TTYPE_HANDLERS[ttype] if ttype < len(self._TTYPE_HANDLERS) else (None, None, False)
257
258 def _read_by_ttype(self, ttype, spec, espec):
259 reader_name, _, is_container = self._ttype_handlers(ttype, spec)
260 if reader_name is None:
261 raise TProtocolException(type=TProtocolException.INVALID_DATA,
262 message='Invalid type %d' % (ttype))
263 reader_func = getattr(self, reader_name)
264 read = (lambda: reader_func(espec)) if is_container else reader_func
265 while True:
266 yield read()
Nobuaki Sukegawabc7e4802015-11-14 19:23:27 +0900267
Roger Meierf4eec7a2011-09-11 18:16:21 +0000268 def readFieldByTType(self, ttype, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900269 return self._read_by_ttype(ttype, spec, spec).next()
Roger Meierf4eec7a2011-09-11 18:16:21 +0000270
271 def readContainerList(self, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900272 ttype, tspec, is_immutable = spec
Roger Meierf4eec7a2011-09-11 18:16:21 +0000273 (list_type, list_len) = self.readListBegin()
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900274 # TODO: compare types we just decoded with thrift_spec
275 elems = islice(self._read_by_ttype(ttype, spec, tspec), list_len)
276 results = (tuple if is_immutable else list)(elems)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000277 self.readListEnd()
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900278 return results
Roger Meierf4eec7a2011-09-11 18:16:21 +0000279
280 def readContainerSet(self, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900281 ttype, tspec, is_immutable = spec
Roger Meierf4eec7a2011-09-11 18:16:21 +0000282 (set_type, set_len) = self.readSetBegin()
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900283 # TODO: compare types we just decoded with thrift_spec
284 elems = islice(self._read_by_ttype(ttype, spec, tspec), set_len)
285 results = (frozenset if is_immutable else set)(elems)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000286 self.readSetEnd()
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900287 return results
Roger Meierf4eec7a2011-09-11 18:16:21 +0000288
289 def readContainerStruct(self, spec):
290 (obj_class, obj_spec) = spec
291 obj = obj_class()
292 obj.read(self)
293 return obj
Bryan Duxbury69720412012-01-03 17:32:30 +0000294
Roger Meierf4eec7a2011-09-11 18:16:21 +0000295 def readContainerMap(self, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900296 ktype, kspec, vtype, vspec, is_immutable = spec
Roger Meierf4eec7a2011-09-11 18:16:21 +0000297 (map_ktype, map_vtype, map_len) = self.readMapBegin()
Bryan Duxbury69720412012-01-03 17:32:30 +0000298 # TODO: compare types we just decoded with thrift_spec and
299 # abort/skip if types disagree
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900300 keys = self._read_by_ttype(ktype, spec, kspec)
301 vals = self._read_by_ttype(vtype, spec, vspec)
302 keyvals = islice(zip(keys, vals), map_len)
303 results = (TFrozenDict if is_immutable else dict)(keyvals)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000304 self.readMapEnd()
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900305 return results
Roger Meierf4eec7a2011-09-11 18:16:21 +0000306
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900307 def readStruct(self, obj, thrift_spec, is_immutable=False):
308 if is_immutable:
309 fields = {}
Roger Meierf4eec7a2011-09-11 18:16:21 +0000310 self.readStructBegin()
311 while True:
312 (fname, ftype, fid) = self.readFieldBegin()
313 if ftype == TType.STOP:
314 break
315 try:
316 field = thrift_spec[fid]
317 except IndexError:
318 self.skip(ftype)
319 else:
320 if field is not None and ftype == field[1]:
321 fname = field[2]
322 fspec = field[3]
323 val = self.readFieldByTType(ftype, fspec)
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900324 if is_immutable:
325 fields[fname] = val
326 else:
327 setattr(obj, fname, val)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000328 else:
329 self.skip(ftype)
330 self.readFieldEnd()
331 self.readStructEnd()
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900332 if is_immutable:
333 return obj(**fields)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000334
335 def writeContainerStruct(self, val, spec):
336 val.write(self)
337
338 def writeContainerList(self, val, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900339 ttype, tspec, _ = spec
340 self.writeListBegin(ttype, len(val))
341 for _ in self._write_by_ttype(ttype, val, spec, tspec):
342 pass
Roger Meierf4eec7a2011-09-11 18:16:21 +0000343 self.writeListEnd()
344
345 def writeContainerSet(self, val, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900346 ttype, tspec, _ = spec
347 self.writeSetBegin(ttype, len(val))
348 for _ in self._write_by_ttype(ttype, val, spec, tspec):
349 pass
Roger Meierf4eec7a2011-09-11 18:16:21 +0000350 self.writeSetEnd()
351
352 def writeContainerMap(self, val, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900353 ktype, kspec, vtype, vspec, _ = spec
354 self.writeMapBegin(ktype, vtype, len(val))
355 for _ in zip(self._write_by_ttype(ktype, six.iterkeys(val), spec, kspec),
356 self._write_by_ttype(vtype, six.itervalues(val), spec, vspec)):
357 pass
Roger Meierf4eec7a2011-09-11 18:16:21 +0000358 self.writeMapEnd()
359
360 def writeStruct(self, obj, thrift_spec):
361 self.writeStructBegin(obj.__class__.__name__)
362 for field in thrift_spec:
363 if field is None:
364 continue
365 fname = field[2]
366 val = getattr(obj, fname)
367 if val is None:
368 # skip writing out unset fields
369 continue
370 fid = field[0]
371 ftype = field[1]
372 fspec = field[3]
Roger Meierf4eec7a2011-09-11 18:16:21 +0000373 self.writeFieldBegin(fname, ftype, fid)
374 self.writeFieldByTType(ftype, val, fspec)
375 self.writeFieldEnd()
376 self.writeFieldStop()
377 self.writeStructEnd()
378
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900379 def _write_by_ttype(self, ttype, vals, spec, espec):
380 _, writer_name, is_container = self._ttype_handlers(ttype, spec)
381 writer_func = getattr(self, writer_name)
382 write = (lambda v: writer_func(v, espec)) if is_container else writer_func
383 for v in vals:
384 yield write(v)
385
Roger Meierf4eec7a2011-09-11 18:16:21 +0000386 def writeFieldByTType(self, ttype, val, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900387 self._write_by_ttype(ttype, [val], spec, spec).next()
Roger Meierf4eec7a2011-09-11 18:16:21 +0000388
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900389
Konrad Grochowski93fea152014-10-02 16:29:14 +0200390def checkIntegerLimits(i, bits):
Konrad Grochowski07218672014-10-09 12:18:25 +0200391 if bits == 8 and (i < -128 or i > 127):
Konrad Grochowski93fea152014-10-02 16:29:14 +0200392 raise TProtocolException(TProtocolException.INVALID_DATA,
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200393 "i8 requires -128 <= number <= 127")
Konrad Grochowski07218672014-10-09 12:18:25 +0200394 elif bits == 16 and (i < -32768 or i > 32767):
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200395 raise TProtocolException(TProtocolException.INVALID_DATA,
396 "i16 requires -32768 <= number <= 32767")
Konrad Grochowski07218672014-10-09 12:18:25 +0200397 elif bits == 32 and (i < -2147483648 or i > 2147483647):
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200398 raise TProtocolException(TProtocolException.INVALID_DATA,
399 "i32 requires -2147483648 <= number <= 2147483647")
Konrad Grochowski07218672014-10-09 12:18:25 +0200400 elif bits == 64 and (i < -9223372036854775808 or i > 9223372036854775807):
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200401 raise TProtocolException(TProtocolException.INVALID_DATA,
402 "i64 requires -9223372036854775808 <= number <= 9223372036854775807")
Bryan Duxbury69720412012-01-03 17:32:30 +0000403
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900404
Nobuaki Sukegawab9c859a2015-12-21 01:10:25 +0900405class TProtocolFactory(object):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000406 def getProtocol(self, trans):
Mark Slee4ac459f2006-10-25 21:39:01 +0000407 pass