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