blob: d9aa2e82b6023d5d27ebd820bf19ed94f0e74457 [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 Sukegawa7b545b52016-01-11 13:46:04 +090021from thrift.transport.TTransport import TTransportException
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090022from ..compat import binary_to_str, str_to_binary
Mark Sleecde2b612006-09-03 21:13:07 +000023
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +090024import six
25import sys
26from itertools import islice
27from six.moves import zip
28
Mark Slee92195ae2007-02-21 05:16:30 +000029
Bryan Duxbury69720412012-01-03 17:32:30 +000030class TProtocolException(TException):
Mark Slee92195ae2007-02-21 05:16:30 +000031 """Custom Protocol Exception class"""
32
33 UNKNOWN = 0
34 INVALID_DATA = 1
35 NEGATIVE_SIZE = 2
36 SIZE_LIMIT = 3
Mark Slee808454e2007-06-20 21:51:57 +000037 BAD_VERSION = 4
Jens Geyer6d1a83a2014-05-03 00:49:05 +020038 NOT_IMPLEMENTED = 5
39 DEPTH_LIMIT = 6
Mark Slee92195ae2007-02-21 05:16:30 +000040
41 def __init__(self, type=UNKNOWN, message=None):
42 TException.__init__(self, message)
43 self.type = type
44
Mark Sleecde2b612006-09-03 21:13:07 +000045
Nobuaki Sukegawab9c859a2015-12-21 01:10:25 +090046class TProtocolBase(object):
Mark Sleecde2b612006-09-03 21:13:07 +000047 """Base class for Thrift protocol driver."""
48
Aditya Agarwal5c468192007-02-06 01:14:33 +000049 def __init__(self, trans):
50 self.trans = trans
Mark Slee4ac459f2006-10-25 21:39:01 +000051
Nobuaki Sukegawa7b545b52016-01-11 13:46:04 +090052 @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 Meierad8154a2012-12-18 21:02:16 +010061 def writeMessageBegin(self, name, ttype, seqid):
Mark Sleecde2b612006-09-03 21:13:07 +000062 pass
63
Mark Slee4ac459f2006-10-25 21:39:01 +000064 def writeMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000065 pass
66
Mark Slee4ac459f2006-10-25 21:39:01 +000067 def writeStructBegin(self, name):
Mark Sleecde2b612006-09-03 21:13:07 +000068 pass
69
Mark Slee4ac459f2006-10-25 21:39:01 +000070 def writeStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000071 pass
72
Roger Meierad8154a2012-12-18 21:02:16 +010073 def writeFieldBegin(self, name, ttype, fid):
Mark Sleecde2b612006-09-03 21:13:07 +000074 pass
75
Mark Slee4ac459f2006-10-25 21:39:01 +000076 def writeFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000077 pass
78
Mark Slee4ac459f2006-10-25 21:39:01 +000079 def writeFieldStop(self):
Mark Sleecde2b612006-09-03 21:13:07 +000080 pass
81
Mark Slee4ac459f2006-10-25 21:39:01 +000082 def writeMapBegin(self, ktype, vtype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000083 pass
84
Mark Slee4ac459f2006-10-25 21:39:01 +000085 def writeMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000086 pass
87
Mark Slee4ac459f2006-10-25 21:39:01 +000088 def writeListBegin(self, etype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000089 pass
90
Mark Slee4ac459f2006-10-25 21:39:01 +000091 def writeListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000092 pass
93
Mark Slee4ac459f2006-10-25 21:39:01 +000094 def writeSetBegin(self, etype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000095 pass
96
Mark Slee4ac459f2006-10-25 21:39:01 +000097 def writeSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000098 pass
99
Roger Meierad8154a2012-12-18 21:02:16 +0100100 def writeBool(self, bool_val):
Mark Sleecde2b612006-09-03 21:13:07 +0000101 pass
102
Mark Slee4ac459f2006-10-25 21:39:01 +0000103 def writeByte(self, byte):
Mark Sleecde2b612006-09-03 21:13:07 +0000104 pass
105
Mark Slee4ac459f2006-10-25 21:39:01 +0000106 def writeI16(self, i16):
Mark Sleecde2b612006-09-03 21:13:07 +0000107 pass
108
Mark Slee4ac459f2006-10-25 21:39:01 +0000109 def writeI32(self, i32):
Mark Sleecde2b612006-09-03 21:13:07 +0000110 pass
111
Mark Slee4ac459f2006-10-25 21:39:01 +0000112 def writeI64(self, i64):
Mark Sleecde2b612006-09-03 21:13:07 +0000113 pass
114
Mark Slee4ac459f2006-10-25 21:39:01 +0000115 def writeDouble(self, dub):
Mark Sleec98d0502006-09-06 02:42:25 +0000116 pass
117
Roger Meierad8154a2012-12-18 21:02:16 +0100118 def writeString(self, str_val):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900119 self.writeBinary(str_to_binary(str_val))
120
121 def writeBinary(self, str_val):
Mark Sleecde2b612006-09-03 21:13:07 +0000122 pass
123
Nobuaki Sukegawa397bd512016-01-06 14:43:15 +0900124 def writeUtf8(self, str_val):
125 self.writeString(str_val.encode('utf8'))
126
Mark Slee4ac459f2006-10-25 21:39:01 +0000127 def readMessageBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000128 pass
129
Mark Slee4ac459f2006-10-25 21:39:01 +0000130 def readMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000131 pass
132
Mark Slee4ac459f2006-10-25 21:39:01 +0000133 def readStructBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000134 pass
135
Mark Slee4ac459f2006-10-25 21:39:01 +0000136 def readStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000137 pass
138
Mark Slee4ac459f2006-10-25 21:39:01 +0000139 def readFieldBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000140 pass
141
Mark Slee4ac459f2006-10-25 21:39:01 +0000142 def readFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000143 pass
144
Mark Slee4ac459f2006-10-25 21:39:01 +0000145 def readMapBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000146 pass
147
Mark Slee4ac459f2006-10-25 21:39:01 +0000148 def readMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000149 pass
150
Mark Slee4ac459f2006-10-25 21:39:01 +0000151 def readListBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000152 pass
153
Mark Slee4ac459f2006-10-25 21:39:01 +0000154 def readListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000155 pass
156
Mark Slee4ac459f2006-10-25 21:39:01 +0000157 def readSetBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000158 pass
159
Mark Slee4ac459f2006-10-25 21:39:01 +0000160 def readSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000161 pass
162
Mark Slee4ac459f2006-10-25 21:39:01 +0000163 def readBool(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000164 pass
165
Mark Slee4ac459f2006-10-25 21:39:01 +0000166 def readByte(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000167 pass
168
Mark Slee4ac459f2006-10-25 21:39:01 +0000169 def readI16(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000170 pass
171
Mark Slee4ac459f2006-10-25 21:39:01 +0000172 def readI32(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000173 pass
174
Mark Slee4ac459f2006-10-25 21:39:01 +0000175 def readI64(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000176 pass
177
Mark Slee4ac459f2006-10-25 21:39:01 +0000178 def readDouble(self):
Mark Sleec98d0502006-09-06 02:42:25 +0000179 pass
180
Mark Slee4ac459f2006-10-25 21:39:01 +0000181 def readString(self):
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900182 return binary_to_str(self.readBinary())
183
184 def readBinary(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000185 pass
186
Nobuaki Sukegawa397bd512016-01-06 14:43:15 +0900187 def readUtf8(self):
188 return self.readString().decode('utf8')
189
Roger Meierad8154a2012-12-18 21:02:16 +0100190 def skip(self, ttype):
191 if ttype == TType.STOP:
Mark Sleecde2b612006-09-03 21:13:07 +0000192 return
Roger Meierad8154a2012-12-18 21:02:16 +0100193 elif ttype == TType.BOOL:
Mark Slee4ac459f2006-10-25 21:39:01 +0000194 self.readBool()
Roger Meierad8154a2012-12-18 21:02:16 +0100195 elif ttype == TType.BYTE:
Mark Slee4ac459f2006-10-25 21:39:01 +0000196 self.readByte()
Roger Meierad8154a2012-12-18 21:02:16 +0100197 elif ttype == TType.I16:
Mark Slee4ac459f2006-10-25 21:39:01 +0000198 self.readI16()
Roger Meierad8154a2012-12-18 21:02:16 +0100199 elif ttype == TType.I32:
Mark Slee4ac459f2006-10-25 21:39:01 +0000200 self.readI32()
Roger Meierad8154a2012-12-18 21:02:16 +0100201 elif ttype == TType.I64:
Mark Slee4ac459f2006-10-25 21:39:01 +0000202 self.readI64()
Roger Meierad8154a2012-12-18 21:02:16 +0100203 elif ttype == TType.DOUBLE:
Mark Slee4ac459f2006-10-25 21:39:01 +0000204 self.readDouble()
Roger Meierad8154a2012-12-18 21:02:16 +0100205 elif ttype == TType.STRING:
Mark Slee4ac459f2006-10-25 21:39:01 +0000206 self.readString()
Roger Meierad8154a2012-12-18 21:02:16 +0100207 elif ttype == TType.STRUCT:
Mark Slee4ac459f2006-10-25 21:39:01 +0000208 name = self.readStructBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000209 while True:
Roger Meierad8154a2012-12-18 21:02:16 +0100210 (name, ttype, id) = self.readFieldBegin()
211 if ttype == TType.STOP:
Mark Sleecde2b612006-09-03 21:13:07 +0000212 break
Roger Meierad8154a2012-12-18 21:02:16 +0100213 self.skip(ttype)
Mark Slee4ac459f2006-10-25 21:39:01 +0000214 self.readFieldEnd()
215 self.readStructEnd()
Roger Meierad8154a2012-12-18 21:02:16 +0100216 elif ttype == TType.MAP:
Mark Slee4ac459f2006-10-25 21:39:01 +0000217 (ktype, vtype, size) = self.readMapBegin()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900218 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000219 self.skip(ktype)
220 self.skip(vtype)
221 self.readMapEnd()
Roger Meierad8154a2012-12-18 21:02:16 +0100222 elif ttype == TType.SET:
Mark Slee4ac459f2006-10-25 21:39:01 +0000223 (etype, size) = self.readSetBegin()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900224 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000225 self.skip(etype)
226 self.readSetEnd()
Roger Meierad8154a2012-12-18 21:02:16 +0100227 elif ttype == TType.LIST:
Mark Slee4ac459f2006-10-25 21:39:01 +0000228 (etype, size) = self.readListBegin()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900229 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000230 self.skip(etype)
231 self.readListEnd()
Mark Sleecde2b612006-09-03 21:13:07 +0000232
Bryan Duxbury69720412012-01-03 17:32:30 +0000233 # tuple of: ( 'reader method' name, is_container bool, 'writer_method' name )
Roger Meierf4eec7a2011-09-11 18:16:21 +0000234 _TTYPE_HANDLERS = (
Bryan Duxbury69720412012-01-03 17:32:30 +0000235 (None, None, False), # 0 TType.STOP
236 (None, None, False), # 1 TType.VOID # TODO: handle void?
237 ('readBool', 'writeBool', False), # 2 TType.BOOL
Nobuaki Sukegawabc7e4802015-11-14 19:23:27 +0900238 ('readByte', 'writeByte', False), # 3 TType.BYTE and I08
Bryan Duxbury69720412012-01-03 17:32:30 +0000239 ('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 Meierf4eec7a2011-09-11 18:16:21 +0000253 )
254
Nobuaki Sukegawabc7e4802015-11-14 19:23:27 +0900255 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 Sukegawa397bd512016-01-06 14:43:15 +0900261 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 Sukegawa299255a2016-01-06 14:52:50 +0900266 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 Sukegawabc7e4802015-11-14 19:23:27 +0900277
Roger Meierf4eec7a2011-09-11 18:16:21 +0000278 def readFieldByTType(self, ttype, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900279 return self._read_by_ttype(ttype, spec, spec).next()
Roger Meierf4eec7a2011-09-11 18:16:21 +0000280
281 def readContainerList(self, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900282 ttype, tspec, is_immutable = spec
Roger Meierf4eec7a2011-09-11 18:16:21 +0000283 (list_type, list_len) = self.readListBegin()
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900284 # 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 Meierf4eec7a2011-09-11 18:16:21 +0000287 self.readListEnd()
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900288 return results
Roger Meierf4eec7a2011-09-11 18:16:21 +0000289
290 def readContainerSet(self, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900291 ttype, tspec, is_immutable = spec
Roger Meierf4eec7a2011-09-11 18:16:21 +0000292 (set_type, set_len) = self.readSetBegin()
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900293 # 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 Meierf4eec7a2011-09-11 18:16:21 +0000296 self.readSetEnd()
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900297 return results
Roger Meierf4eec7a2011-09-11 18:16:21 +0000298
299 def readContainerStruct(self, spec):
300 (obj_class, obj_spec) = spec
301 obj = obj_class()
302 obj.read(self)
303 return obj
Bryan Duxbury69720412012-01-03 17:32:30 +0000304
Roger Meierf4eec7a2011-09-11 18:16:21 +0000305 def readContainerMap(self, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900306 ktype, kspec, vtype, vspec, is_immutable = spec
Roger Meierf4eec7a2011-09-11 18:16:21 +0000307 (map_ktype, map_vtype, map_len) = self.readMapBegin()
Bryan Duxbury69720412012-01-03 17:32:30 +0000308 # TODO: compare types we just decoded with thrift_spec and
309 # abort/skip if types disagree
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900310 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 Meierf4eec7a2011-09-11 18:16:21 +0000314 self.readMapEnd()
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900315 return results
Roger Meierf4eec7a2011-09-11 18:16:21 +0000316
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900317 def readStruct(self, obj, thrift_spec, is_immutable=False):
318 if is_immutable:
319 fields = {}
Roger Meierf4eec7a2011-09-11 18:16:21 +0000320 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 Sukegawae841b3d2015-11-17 11:01:17 +0900334 if is_immutable:
335 fields[fname] = val
336 else:
337 setattr(obj, fname, val)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000338 else:
339 self.skip(ftype)
340 self.readFieldEnd()
341 self.readStructEnd()
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900342 if is_immutable:
343 return obj(**fields)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000344
345 def writeContainerStruct(self, val, spec):
346 val.write(self)
347
348 def writeContainerList(self, val, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900349 ttype, tspec, _ = spec
350 self.writeListBegin(ttype, len(val))
351 for _ in self._write_by_ttype(ttype, val, spec, tspec):
352 pass
Roger Meierf4eec7a2011-09-11 18:16:21 +0000353 self.writeListEnd()
354
355 def writeContainerSet(self, val, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900356 ttype, tspec, _ = spec
357 self.writeSetBegin(ttype, len(val))
358 for _ in self._write_by_ttype(ttype, val, spec, tspec):
359 pass
Roger Meierf4eec7a2011-09-11 18:16:21 +0000360 self.writeSetEnd()
361
362 def writeContainerMap(self, val, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900363 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 Meierf4eec7a2011-09-11 18:16:21 +0000368 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 Meierf4eec7a2011-09-11 18:16:21 +0000383 self.writeFieldBegin(fname, ftype, fid)
384 self.writeFieldByTType(ftype, val, fspec)
385 self.writeFieldEnd()
386 self.writeFieldStop()
387 self.writeStructEnd()
388
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900389 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 Meierf4eec7a2011-09-11 18:16:21 +0000396 def writeFieldByTType(self, ttype, val, spec):
Nobuaki Sukegawa299255a2016-01-06 14:52:50 +0900397 self._write_by_ttype(ttype, [val], spec, spec).next()
Roger Meierf4eec7a2011-09-11 18:16:21 +0000398
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900399
Konrad Grochowski93fea152014-10-02 16:29:14 +0200400def checkIntegerLimits(i, bits):
Konrad Grochowski07218672014-10-09 12:18:25 +0200401 if bits == 8 and (i < -128 or i > 127):
Konrad Grochowski93fea152014-10-02 16:29:14 +0200402 raise TProtocolException(TProtocolException.INVALID_DATA,
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200403 "i8 requires -128 <= number <= 127")
Konrad Grochowski07218672014-10-09 12:18:25 +0200404 elif bits == 16 and (i < -32768 or i > 32767):
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200405 raise TProtocolException(TProtocolException.INVALID_DATA,
406 "i16 requires -32768 <= number <= 32767")
Konrad Grochowski07218672014-10-09 12:18:25 +0200407 elif bits == 32 and (i < -2147483648 or i > 2147483647):
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200408 raise TProtocolException(TProtocolException.INVALID_DATA,
409 "i32 requires -2147483648 <= number <= 2147483647")
Konrad Grochowski07218672014-10-09 12:18:25 +0200410 elif bits == 64 and (i < -9223372036854775808 or i > 9223372036854775807):
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200411 raise TProtocolException(TProtocolException.INVALID_DATA,
412 "i64 requires -9223372036854775808 <= number <= 9223372036854775807")
Bryan Duxbury69720412012-01-03 17:32:30 +0000413
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900414
Nobuaki Sukegawab9c859a2015-12-21 01:10:25 +0900415class TProtocolFactory(object):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000416 def getProtocol(self, trans):
Mark Slee4ac459f2006-10-25 21:39:01 +0000417 pass