| 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 | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 20 | from struct import pack, unpack |
| Carel Combrink | a715bdf | 2025-10-30 07:44:21 +0100 | [diff] [blame] | 21 | import uuid |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 22 | |
| Alexandre Detiste | 3494e1c | 2025-02-19 21:53:40 +0100 | [diff] [blame] | 23 | from .TProtocol import TType, TProtocolBase, TProtocolException, TProtocolFactory |
| 24 | |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 25 | |
| Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 26 | class TBinaryProtocol(TProtocolBase): |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 27 | """Binary implementation of the Thrift protocol driver.""" |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 28 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 29 | # NastyHaxx. Python 2.4+ on 32-bit machines forces hex constants to be |
| 30 | # positive, converting this into a long. If we hardcode the int value |
| 31 | # instead it'll stay in 32 bit-land. |
| Mark Slee | 9b36ef3 | 2007-10-02 04:44:48 +0000 | [diff] [blame] | 32 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 33 | # VERSION_MASK = 0xffff0000 |
| 34 | VERSION_MASK = -65536 |
| Mark Slee | 9b36ef3 | 2007-10-02 04:44:48 +0000 | [diff] [blame] | 35 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 36 | # VERSION_1 = 0x80010000 |
| 37 | VERSION_1 = -2147418112 |
| Mark Slee | 9b36ef3 | 2007-10-02 04:44:48 +0000 | [diff] [blame] | 38 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 39 | TYPE_MASK = 0x000000ff |
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 40 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 41 | def __init__(self, trans, strictRead=False, strictWrite=True, **kwargs): |
| 42 | TProtocolBase.__init__(self, trans) |
| 43 | self.strictRead = strictRead |
| 44 | self.strictWrite = strictWrite |
| 45 | self.string_length_limit = kwargs.get('string_length_limit', None) |
| 46 | self.container_length_limit = kwargs.get('container_length_limit', None) |
| Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 47 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 48 | def _check_string_length(self, length): |
| 49 | self._check_length(self.string_length_limit, length) |
| Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 50 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 51 | def _check_container_length(self, length): |
| 52 | self._check_length(self.container_length_limit, length) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 53 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 54 | def writeMessageBegin(self, name, type, seqid): |
| 55 | if self.strictWrite: |
| 56 | self.writeI32(TBinaryProtocol.VERSION_1 | type) |
| 57 | self.writeString(name) |
| 58 | self.writeI32(seqid) |
| 59 | else: |
| 60 | self.writeString(name) |
| 61 | self.writeByte(type) |
| 62 | self.writeI32(seqid) |
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 63 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 64 | def writeMessageEnd(self): |
| 65 | pass |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 66 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 67 | def writeStructBegin(self, name): |
| 68 | pass |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 69 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 70 | def writeStructEnd(self): |
| 71 | pass |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 72 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 73 | def writeFieldBegin(self, name, type, id): |
| 74 | self.writeByte(type) |
| 75 | self.writeI16(id) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 76 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 77 | def writeFieldEnd(self): |
| 78 | pass |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 79 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 80 | def writeFieldStop(self): |
| 81 | self.writeByte(TType.STOP) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 82 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 83 | def writeMapBegin(self, ktype, vtype, size): |
| 84 | self.writeByte(ktype) |
| 85 | self.writeByte(vtype) |
| 86 | self.writeI32(size) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 87 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 88 | def writeMapEnd(self): |
| 89 | pass |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 90 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 91 | def writeListBegin(self, etype, size): |
| 92 | self.writeByte(etype) |
| 93 | self.writeI32(size) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 94 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 95 | def writeListEnd(self): |
| 96 | pass |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 97 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 98 | def writeSetBegin(self, etype, size): |
| 99 | self.writeByte(etype) |
| 100 | self.writeI32(size) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 101 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 102 | def writeSetEnd(self): |
| 103 | pass |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 104 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 105 | def writeBool(self, bool): |
| 106 | if bool: |
| 107 | self.writeByte(1) |
| 108 | else: |
| 109 | self.writeByte(0) |
| David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 110 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 111 | def writeByte(self, byte): |
| 112 | buff = pack("!b", byte) |
| 113 | self.trans.write(buff) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 114 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 115 | def writeI16(self, i16): |
| 116 | buff = pack("!h", i16) |
| 117 | self.trans.write(buff) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 118 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 119 | def writeI32(self, i32): |
| 120 | buff = pack("!i", i32) |
| 121 | self.trans.write(buff) |
| David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 122 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 123 | def writeI64(self, i64): |
| 124 | buff = pack("!q", i64) |
| 125 | self.trans.write(buff) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 126 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 127 | def writeDouble(self, dub): |
| 128 | buff = pack("!d", dub) |
| 129 | self.trans.write(buff) |
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 130 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 131 | def writeBinary(self, str): |
| 132 | self.writeI32(len(str)) |
| 133 | self.trans.write(str) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 134 | |
| Carel Combrink | a715bdf | 2025-10-30 07:44:21 +0100 | [diff] [blame] | 135 | def writeUuid(self, uuid): |
| 136 | self.trans.write(uuid.bytes) |
| 137 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 138 | def readMessageBegin(self): |
| 139 | sz = self.readI32() |
| 140 | if sz < 0: |
| 141 | version = sz & TBinaryProtocol.VERSION_MASK |
| 142 | if version != TBinaryProtocol.VERSION_1: |
| 143 | raise TProtocolException( |
| 144 | type=TProtocolException.BAD_VERSION, |
| 145 | message='Bad version in readMessageBegin: %d' % (sz)) |
| 146 | type = sz & TBinaryProtocol.TYPE_MASK |
| 147 | name = self.readString() |
| 148 | seqid = self.readI32() |
| 149 | else: |
| 150 | if self.strictRead: |
| 151 | raise TProtocolException(type=TProtocolException.BAD_VERSION, |
| 152 | message='No protocol version header') |
| Alexandre Detiste | 3494e1c | 2025-02-19 21:53:40 +0100 | [diff] [blame] | 153 | name = self.trans.readAll(sz).decode('utf-8') |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 154 | type = self.readByte() |
| 155 | seqid = self.readI32() |
| 156 | return (name, type, seqid) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 157 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 158 | def readMessageEnd(self): |
| 159 | pass |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 160 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 161 | def readStructBegin(self): |
| 162 | pass |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 163 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 164 | def readStructEnd(self): |
| 165 | pass |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 166 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 167 | def readFieldBegin(self): |
| 168 | type = self.readByte() |
| 169 | if type == TType.STOP: |
| 170 | return (None, type, 0) |
| 171 | id = self.readI16() |
| 172 | return (None, type, id) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 173 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 174 | def readFieldEnd(self): |
| 175 | pass |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 176 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 177 | def readMapBegin(self): |
| 178 | ktype = self.readByte() |
| 179 | vtype = self.readByte() |
| 180 | size = self.readI32() |
| 181 | self._check_container_length(size) |
| 182 | return (ktype, vtype, size) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 183 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 184 | def readMapEnd(self): |
| 185 | pass |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 186 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 187 | def readListBegin(self): |
| 188 | etype = self.readByte() |
| 189 | size = self.readI32() |
| 190 | self._check_container_length(size) |
| 191 | return (etype, size) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 192 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 193 | def readListEnd(self): |
| 194 | pass |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 195 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 196 | def readSetBegin(self): |
| 197 | etype = self.readByte() |
| 198 | size = self.readI32() |
| 199 | self._check_container_length(size) |
| 200 | return (etype, size) |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 201 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 202 | def readSetEnd(self): |
| 203 | pass |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 204 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 205 | def readBool(self): |
| 206 | byte = self.readByte() |
| 207 | if byte == 0: |
| 208 | return False |
| 209 | return True |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 210 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 211 | def readByte(self): |
| 212 | buff = self.trans.readAll(1) |
| 213 | val, = unpack('!b', buff) |
| 214 | return val |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 215 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 216 | def readI16(self): |
| 217 | buff = self.trans.readAll(2) |
| 218 | val, = unpack('!h', buff) |
| 219 | return val |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 220 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 221 | def readI32(self): |
| 222 | buff = self.trans.readAll(4) |
| 223 | val, = unpack('!i', buff) |
| 224 | return val |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 225 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 226 | def readI64(self): |
| 227 | buff = self.trans.readAll(8) |
| 228 | val, = unpack('!q', buff) |
| 229 | return val |
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 230 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 231 | def readDouble(self): |
| 232 | buff = self.trans.readAll(8) |
| 233 | val, = unpack('!d', buff) |
| 234 | return val |
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 235 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 236 | def readBinary(self): |
| 237 | size = self.readI32() |
| 238 | self._check_string_length(size) |
| 239 | s = self.trans.readAll(size) |
| 240 | return s |
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 241 | |
| Carel Combrink | a715bdf | 2025-10-30 07:44:21 +0100 | [diff] [blame] | 242 | def readUuid(self): |
| 243 | buff = self.trans.readAll(16) |
| 244 | val = uuid.UUID(bytes=buff) |
| 245 | return val |
| 246 | |
| David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 247 | |
| James E. King III | e44f6a9 | 2019-02-07 17:11:21 -0500 | [diff] [blame] | 248 | class TBinaryProtocolFactory(TProtocolFactory): |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 249 | def __init__(self, strictRead=False, strictWrite=True, **kwargs): |
| 250 | self.strictRead = strictRead |
| 251 | self.strictWrite = strictWrite |
| 252 | self.string_length_limit = kwargs.get('string_length_limit', None) |
| 253 | self.container_length_limit = kwargs.get('container_length_limit', None) |
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 254 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 255 | def getProtocol(self, trans): |
| 256 | prot = TBinaryProtocol(trans, self.strictRead, self.strictWrite, |
| 257 | string_length_limit=self.string_length_limit, |
| 258 | container_length_limit=self.container_length_limit) |
| 259 | return prot |
| David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 260 | |
| 261 | |
| 262 | class TBinaryProtocolAccelerated(TBinaryProtocol): |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 263 | """C-Accelerated version of TBinaryProtocol. |
| David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 264 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 265 | This class does not override any of TBinaryProtocol's methods, |
| 266 | but the generated code recognizes it directly and will call into |
| 267 | our C module to do the encoding, bypassing this object entirely. |
| 268 | We inherit from TBinaryProtocol so that the normal TBinaryProtocol |
| 269 | encoding can happen if the fastbinary module doesn't work for some |
| 270 | reason. (TODO(dreiss): Make this happen sanely in more cases.) |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 271 | To disable this behavior, pass fallback=False constructor argument. |
| David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 272 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 273 | In order to take advantage of the C module, just use |
| 274 | TBinaryProtocolAccelerated instead of TBinaryProtocol. |
| David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 275 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 276 | NOTE: This code was contributed by an external developer. |
| 277 | The internal Thrift team has reviewed and tested it, |
| 278 | but we cannot guarantee that it is production-ready. |
| 279 | Please feel free to report bugs and/or success stories |
| 280 | to the public mailing list. |
| 281 | """ |
| 282 | pass |
| David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 283 | |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 284 | def __init__(self, *args, **kwargs): |
| 285 | fallback = kwargs.pop('fallback', True) |
| 286 | super(TBinaryProtocolAccelerated, self).__init__(*args, **kwargs) |
| 287 | try: |
| 288 | from thrift.protocol import fastbinary |
| 289 | except ImportError: |
| 290 | if not fallback: |
| 291 | raise |
| 292 | else: |
| 293 | self._fast_decode = fastbinary.decode_binary |
| 294 | self._fast_encode = fastbinary.encode_binary |
| 295 | |
| David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 296 | |
| James E. King III | e44f6a9 | 2019-02-07 17:11:21 -0500 | [diff] [blame] | 297 | class TBinaryProtocolAcceleratedFactory(TProtocolFactory): |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 298 | def __init__(self, |
| 299 | string_length_limit=None, |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 300 | container_length_limit=None, |
| 301 | fallback=True): |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 302 | self.string_length_limit = string_length_limit |
| 303 | self.container_length_limit = container_length_limit |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 304 | self._fallback = fallback |
| Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 305 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 306 | def getProtocol(self, trans): |
| 307 | return TBinaryProtocolAccelerated( |
| 308 | trans, |
| 309 | string_length_limit=self.string_length_limit, |
| Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 310 | container_length_limit=self.container_length_limit, |
| 311 | fallback=self._fallback) |