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 TProtocol import * |
| 21 | from struct import pack, unpack |
| 22 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 23 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 24 | class TBinaryProtocol(TProtocolBase): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 25 | """Binary implementation of the Thrift protocol driver.""" |
| 26 | |
Mark Slee | 9b36ef3 | 2007-10-02 04:44:48 +0000 | [diff] [blame] | 27 | # NastyHaxx. Python 2.4+ on 32-bit machines forces hex constants to be |
| 28 | # positive, converting this into a long. If we hardcode the int value |
| 29 | # instead it'll stay in 32 bit-land. |
| 30 | |
| 31 | # VERSION_MASK = 0xffff0000 |
| 32 | VERSION_MASK = -65536 |
| 33 | |
| 34 | # VERSION_1 = 0x80010000 |
| 35 | VERSION_1 = -2147418112 |
| 36 | |
| 37 | TYPE_MASK = 0x000000ff |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 38 | |
| 39 | def __init__(self, trans, strictRead=False, strictWrite=True): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 40 | TProtocolBase.__init__(self, trans) |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 41 | self.strictRead = strictRead |
| 42 | self.strictWrite = strictWrite |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 43 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 44 | def writeMessageBegin(self, name, type, seqid): |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 45 | if self.strictWrite: |
Mark Slee | 552410c | 2007-06-22 01:03:55 +0000 | [diff] [blame] | 46 | self.writeI32(TBinaryProtocol.VERSION_1 | type) |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 47 | self.writeString(name) |
| 48 | self.writeI32(seqid) |
| 49 | else: |
| 50 | self.writeString(name) |
| 51 | self.writeByte(type) |
| 52 | self.writeI32(seqid) |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 53 | |
| 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 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 63 | def writeFieldBegin(self, name, type, id): |
| 64 | self.writeByte(type) |
| 65 | self.writeI16(id) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 66 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 67 | def writeFieldEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 68 | pass |
| 69 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 70 | def writeFieldStop(self): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 71 | self.writeByte(TType.STOP) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 72 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 73 | def writeMapBegin(self, ktype, vtype, size): |
| 74 | self.writeByte(ktype) |
| 75 | self.writeByte(vtype) |
| 76 | self.writeI32(size) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 77 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 78 | def writeMapEnd(self): |
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 writeListBegin(self, etype, size): |
| 82 | self.writeByte(etype) |
| 83 | self.writeI32(size) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 84 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 85 | def writeListEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 86 | pass |
| 87 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 88 | def writeSetBegin(self, etype, size): |
| 89 | self.writeByte(etype) |
| 90 | self.writeI32(size) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 91 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 92 | def writeSetEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 93 | pass |
| 94 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 95 | def writeBool(self, bool): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 96 | if bool: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 97 | self.writeByte(1) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 98 | else: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 99 | self.writeByte(0) |
David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 100 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 101 | def writeByte(self, byte): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 102 | buff = pack("!b", byte) |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 103 | self.trans.write(buff) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 104 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 105 | def writeI16(self, i16): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 106 | buff = pack("!h", i16) |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 107 | self.trans.write(buff) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 108 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 109 | def writeI32(self, i32): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 110 | buff = pack("!i", i32) |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 111 | self.trans.write(buff) |
David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 112 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 113 | def writeI64(self, i64): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 114 | buff = pack("!q", i64) |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 115 | self.trans.write(buff) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 116 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 117 | def writeDouble(self, dub): |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 118 | buff = pack("!d", dub) |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 119 | self.trans.write(buff) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 120 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 121 | def writeString(self, str): |
| 122 | self.writeI32(len(str)) |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 123 | self.trans.write(str) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 124 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 125 | def readMessageBegin(self): |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 126 | sz = self.readI32() |
| 127 | if sz < 0: |
Mark Slee | 552410c | 2007-06-22 01:03:55 +0000 | [diff] [blame] | 128 | version = sz & TBinaryProtocol.VERSION_MASK |
| 129 | if version != TBinaryProtocol.VERSION_1: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 130 | raise TProtocolException( |
| 131 | type=TProtocolException.BAD_VERSION, |
| 132 | message='Bad version in readMessageBegin: %d' % (sz)) |
Mark Slee | 9b36ef3 | 2007-10-02 04:44:48 +0000 | [diff] [blame] | 133 | type = sz & TBinaryProtocol.TYPE_MASK |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 134 | name = self.readString() |
| 135 | seqid = self.readI32() |
| 136 | else: |
| 137 | if self.strictRead: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 138 | raise TProtocolException(type=TProtocolException.BAD_VERSION, |
| 139 | message='No protocol version header') |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 140 | name = self.trans.readAll(sz) |
| 141 | type = self.readByte() |
| 142 | seqid = self.readI32() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 143 | return (name, type, seqid) |
| 144 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 145 | def readMessageEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 146 | pass |
| 147 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 148 | def readStructBegin(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 149 | pass |
| 150 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 151 | def readStructEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 152 | pass |
| 153 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 154 | def readFieldBegin(self): |
| 155 | type = self.readByte() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 156 | if type == TType.STOP: |
| 157 | return (None, type, 0) |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 158 | id = self.readI16() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 159 | return (None, type, id) |
| 160 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 161 | def readFieldEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 162 | pass |
| 163 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 164 | def readMapBegin(self): |
| 165 | ktype = self.readByte() |
| 166 | vtype = self.readByte() |
| 167 | size = self.readI32() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 168 | return (ktype, vtype, size) |
| 169 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 170 | def readMapEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 171 | pass |
| 172 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 173 | def readListBegin(self): |
| 174 | etype = self.readByte() |
| 175 | size = self.readI32() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 176 | return (etype, size) |
| 177 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 178 | def readListEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 179 | pass |
| 180 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 181 | def readSetBegin(self): |
| 182 | etype = self.readByte() |
| 183 | size = self.readI32() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 184 | return (etype, size) |
| 185 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 186 | def readSetEnd(self): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 187 | pass |
| 188 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 189 | def readBool(self): |
| 190 | byte = self.readByte() |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 191 | if byte == 0: |
| 192 | return False |
| 193 | return True |
| 194 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 195 | def readByte(self): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 196 | buff = self.trans.readAll(1) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 197 | val, = unpack('!b', buff) |
| 198 | return val |
| 199 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 200 | def readI16(self): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 201 | buff = self.trans.readAll(2) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 202 | val, = unpack('!h', buff) |
| 203 | return val |
| 204 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 205 | def readI32(self): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 206 | buff = self.trans.readAll(4) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 207 | val, = unpack('!i', buff) |
| 208 | return val |
| 209 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 210 | def readI64(self): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 211 | buff = self.trans.readAll(8) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 212 | val, = unpack('!q', buff) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 213 | return val |
| 214 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 215 | def readDouble(self): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 216 | buff = self.trans.readAll(8) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 217 | val, = unpack('!d', buff) |
| 218 | return val |
| 219 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 220 | def readString(self): |
| 221 | len = self.readI32() |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 222 | str = self.trans.readAll(len) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 223 | return str |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 224 | |
David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 225 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 226 | class TBinaryProtocolFactory: |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 227 | def __init__(self, strictRead=False, strictWrite=True): |
| 228 | self.strictRead = strictRead |
| 229 | self.strictWrite = strictWrite |
| 230 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 231 | def getProtocol(self, trans): |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 232 | prot = TBinaryProtocol(trans, self.strictRead, self.strictWrite) |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 233 | return prot |
David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 234 | |
| 235 | |
| 236 | class TBinaryProtocolAccelerated(TBinaryProtocol): |
David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 237 | """C-Accelerated version of TBinaryProtocol. |
| 238 | |
| 239 | This class does not override any of TBinaryProtocol's methods, |
| 240 | but the generated code recognizes it directly and will call into |
| 241 | our C module to do the encoding, bypassing this object entirely. |
| 242 | We inherit from TBinaryProtocol so that the normal TBinaryProtocol |
| 243 | encoding can happen if the fastbinary module doesn't work for some |
David Reiss | 5db3e92 | 2007-08-30 23:07:45 +0000 | [diff] [blame] | 244 | reason. (TODO(dreiss): Make this happen sanely in more cases.) |
David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 245 | |
| 246 | In order to take advantage of the C module, just use |
| 247 | TBinaryProtocolAccelerated instead of TBinaryProtocol. |
| 248 | |
| 249 | NOTE: This code was contributed by an external developer. |
| 250 | The internal Thrift team has reviewed and tested it, |
| 251 | but we cannot guarantee that it is production-ready. |
| 252 | Please feel free to report bugs and/or success stories |
| 253 | to the public mailing list. |
| 254 | """ |
David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 255 | pass |
| 256 | |
| 257 | |
| 258 | class TBinaryProtocolAcceleratedFactory: |
| 259 | def getProtocol(self, trans): |
| 260 | return TBinaryProtocolAccelerated(trans) |