| 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 |  | 
|  | 23 | class TBinaryProtocol(TProtocolBase): | 
|  | 24 |  | 
|  | 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): | 
|  | 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: | 
| Esteve Fernandez | bff2a35 | 2009-09-24 10:22:00 +0000 | [diff] [blame] | 130 | raise TProtocolException(type=TProtocolException.BAD_VERSION, message='Bad version in readMessageBegin: %d' % (sz)) | 
| Mark Slee | 9b36ef3 | 2007-10-02 04:44:48 +0000 | [diff] [blame] | 131 | type = sz & TBinaryProtocol.TYPE_MASK | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 132 | name = self.readString() | 
|  | 133 | seqid = self.readI32() | 
|  | 134 | else: | 
|  | 135 | if self.strictRead: | 
| Esteve Fernandez | bff2a35 | 2009-09-24 10:22:00 +0000 | [diff] [blame] | 136 | raise TProtocolException(type=TProtocolException.BAD_VERSION, message='No protocol version header') | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 137 | name = self.trans.readAll(sz) | 
|  | 138 | type = self.readByte() | 
|  | 139 | seqid = self.readI32() | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 140 | return (name, type, seqid) | 
|  | 141 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 142 | def readMessageEnd(self): | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 143 | pass | 
|  | 144 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 145 | def readStructBegin(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 readStructEnd(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 readFieldBegin(self): | 
|  | 152 | type = self.readByte() | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 153 | if type == TType.STOP: | 
|  | 154 | return (None, type, 0) | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 155 | id = self.readI16() | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 156 | return (None, type, id) | 
|  | 157 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 158 | def readFieldEnd(self): | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 159 | pass | 
|  | 160 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 161 | def readMapBegin(self): | 
|  | 162 | ktype = self.readByte() | 
|  | 163 | vtype = self.readByte() | 
|  | 164 | size = self.readI32() | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 165 | return (ktype, vtype, size) | 
|  | 166 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 167 | def readMapEnd(self): | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 168 | pass | 
|  | 169 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 170 | def readListBegin(self): | 
|  | 171 | etype = self.readByte() | 
|  | 172 | size = self.readI32() | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 173 | return (etype, size) | 
|  | 174 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 175 | def readListEnd(self): | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 176 | pass | 
|  | 177 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 178 | def readSetBegin(self): | 
|  | 179 | etype = self.readByte() | 
|  | 180 | size = self.readI32() | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 181 | return (etype, size) | 
|  | 182 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 183 | def readSetEnd(self): | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 184 | pass | 
|  | 185 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 186 | def readBool(self): | 
|  | 187 | byte = self.readByte() | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 188 | if byte == 0: | 
|  | 189 | return False | 
|  | 190 | return True | 
|  | 191 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 192 | def readByte(self): | 
| Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 193 | buff = self.trans.readAll(1) | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 194 | val, = unpack('!b', buff) | 
|  | 195 | return val | 
|  | 196 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 197 | def readI16(self): | 
| Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 198 | buff = self.trans.readAll(2) | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 199 | val, = unpack('!h', buff) | 
|  | 200 | return val | 
|  | 201 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 202 | def readI32(self): | 
| Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 203 | buff = self.trans.readAll(4) | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 204 | val, = unpack('!i', buff) | 
|  | 205 | return val | 
|  | 206 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 207 | def readI64(self): | 
| Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 208 | buff = self.trans.readAll(8) | 
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 209 | val, = unpack('!q', buff) | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 210 | return val | 
|  | 211 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 212 | def readDouble(self): | 
| Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 213 | buff = self.trans.readAll(8) | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 214 | val, = unpack('!d', buff) | 
|  | 215 | return val | 
|  | 216 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 217 | def readString(self): | 
|  | 218 | len = self.readI32() | 
| Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 219 | str = self.trans.readAll(len) | 
| Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 220 | return str | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 221 |  | 
| David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 222 |  | 
| Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 223 | class TBinaryProtocolFactory: | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 224 | def __init__(self, strictRead=False, strictWrite=True): | 
|  | 225 | self.strictRead = strictRead | 
|  | 226 | self.strictWrite = strictWrite | 
|  | 227 |  | 
| Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 228 | def getProtocol(self, trans): | 
| Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 229 | prot = TBinaryProtocol(trans, self.strictRead, self.strictWrite) | 
| Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 230 | return prot | 
| David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 231 |  | 
|  | 232 |  | 
|  | 233 | class TBinaryProtocolAccelerated(TBinaryProtocol): | 
|  | 234 |  | 
|  | 235 | """C-Accelerated version of TBinaryProtocol. | 
|  | 236 |  | 
|  | 237 | This class does not override any of TBinaryProtocol's methods, | 
|  | 238 | but the generated code recognizes it directly and will call into | 
|  | 239 | our C module to do the encoding, bypassing this object entirely. | 
|  | 240 | We inherit from TBinaryProtocol so that the normal TBinaryProtocol | 
|  | 241 | encoding can happen if the fastbinary module doesn't work for some | 
| David Reiss | 5db3e92 | 2007-08-30 23:07:45 +0000 | [diff] [blame] | 242 | reason.  (TODO(dreiss): Make this happen sanely in more cases.) | 
| David Reiss | 382fc30 | 2007-08-25 18:01:30 +0000 | [diff] [blame] | 243 |  | 
|  | 244 | In order to take advantage of the C module, just use | 
|  | 245 | TBinaryProtocolAccelerated instead of TBinaryProtocol. | 
|  | 246 |  | 
|  | 247 | NOTE:  This code was contributed by an external developer. | 
|  | 248 | The internal Thrift team has reviewed and tested it, | 
|  | 249 | but we cannot guarantee that it is production-ready. | 
|  | 250 | Please feel free to report bugs and/or success stories | 
|  | 251 | to the public mailing list. | 
|  | 252 | """ | 
|  | 253 |  | 
|  | 254 | pass | 
|  | 255 |  | 
|  | 256 |  | 
|  | 257 | class TBinaryProtocolAcceleratedFactory: | 
|  | 258 | def getProtocol(self, trans): | 
|  | 259 | return TBinaryProtocolAccelerated(trans) |