PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/service/lib/py/thrift/protocol/TBinaryProtocol.py

#
Python | 259 lines | 180 code | 53 blank | 26 comment | 12 complexity | bc165c1bbaca894c718e24f4619a3825 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  1. #
  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. #
  19. from TProtocol import *
  20. from struct import pack, unpack
  21. class TBinaryProtocol(TProtocolBase):
  22. """Binary implementation of the Thrift protocol driver."""
  23. # NastyHaxx. Python 2.4+ on 32-bit machines forces hex constants to be
  24. # positive, converting this into a long. If we hardcode the int value
  25. # instead it'll stay in 32 bit-land.
  26. # VERSION_MASK = 0xffff0000
  27. VERSION_MASK = -65536
  28. # VERSION_1 = 0x80010000
  29. VERSION_1 = -2147418112
  30. TYPE_MASK = 0x000000ff
  31. def __init__(self, trans, strictRead=False, strictWrite=True):
  32. TProtocolBase.__init__(self, trans)
  33. self.strictRead = strictRead
  34. self.strictWrite = strictWrite
  35. def writeMessageBegin(self, name, type, seqid):
  36. if self.strictWrite:
  37. self.writeI32(TBinaryProtocol.VERSION_1 | type)
  38. self.writeString(name)
  39. self.writeI32(seqid)
  40. else:
  41. self.writeString(name)
  42. self.writeByte(type)
  43. self.writeI32(seqid)
  44. def writeMessageEnd(self):
  45. pass
  46. def writeStructBegin(self, name):
  47. pass
  48. def writeStructEnd(self):
  49. pass
  50. def writeFieldBegin(self, name, type, id):
  51. self.writeByte(type)
  52. self.writeI16(id)
  53. def writeFieldEnd(self):
  54. pass
  55. def writeFieldStop(self):
  56. self.writeByte(TType.STOP);
  57. def writeMapBegin(self, ktype, vtype, size):
  58. self.writeByte(ktype)
  59. self.writeByte(vtype)
  60. self.writeI32(size)
  61. def writeMapEnd(self):
  62. pass
  63. def writeListBegin(self, etype, size):
  64. self.writeByte(etype)
  65. self.writeI32(size)
  66. def writeListEnd(self):
  67. pass
  68. def writeSetBegin(self, etype, size):
  69. self.writeByte(etype)
  70. self.writeI32(size)
  71. def writeSetEnd(self):
  72. pass
  73. def writeBool(self, bool):
  74. if bool:
  75. self.writeByte(1)
  76. else:
  77. self.writeByte(0)
  78. def writeByte(self, byte):
  79. buff = pack("!b", byte)
  80. self.trans.write(buff)
  81. def writeI16(self, i16):
  82. buff = pack("!h", i16)
  83. self.trans.write(buff)
  84. def writeI32(self, i32):
  85. buff = pack("!i", i32)
  86. self.trans.write(buff)
  87. def writeI64(self, i64):
  88. buff = pack("!q", i64)
  89. self.trans.write(buff)
  90. def writeDouble(self, dub):
  91. buff = pack("!d", dub)
  92. self.trans.write(buff)
  93. def writeString(self, str):
  94. self.writeI32(len(str))
  95. self.trans.write(str)
  96. def readMessageBegin(self):
  97. sz = self.readI32()
  98. if sz < 0:
  99. version = sz & TBinaryProtocol.VERSION_MASK
  100. if version != TBinaryProtocol.VERSION_1:
  101. raise TProtocolException(TProtocolException.BAD_VERSION, 'Bad version in readMessageBegin: %d' % (sz))
  102. type = sz & TBinaryProtocol.TYPE_MASK
  103. name = self.readString()
  104. seqid = self.readI32()
  105. else:
  106. if self.strictRead:
  107. raise TProtocolException(TProtocolException.BAD_VERSION, 'No protocol version header')
  108. name = self.trans.readAll(sz)
  109. type = self.readByte()
  110. seqid = self.readI32()
  111. return (name, type, seqid)
  112. def readMessageEnd(self):
  113. pass
  114. def readStructBegin(self):
  115. pass
  116. def readStructEnd(self):
  117. pass
  118. def readFieldBegin(self):
  119. type = self.readByte()
  120. if type == TType.STOP:
  121. return (None, type, 0)
  122. id = self.readI16()
  123. return (None, type, id)
  124. def readFieldEnd(self):
  125. pass
  126. def readMapBegin(self):
  127. ktype = self.readByte()
  128. vtype = self.readByte()
  129. size = self.readI32()
  130. return (ktype, vtype, size)
  131. def readMapEnd(self):
  132. pass
  133. def readListBegin(self):
  134. etype = self.readByte()
  135. size = self.readI32()
  136. return (etype, size)
  137. def readListEnd(self):
  138. pass
  139. def readSetBegin(self):
  140. etype = self.readByte()
  141. size = self.readI32()
  142. return (etype, size)
  143. def readSetEnd(self):
  144. pass
  145. def readBool(self):
  146. byte = self.readByte()
  147. if byte == 0:
  148. return False
  149. return True
  150. def readByte(self):
  151. buff = self.trans.readAll(1)
  152. val, = unpack('!b', buff)
  153. return val
  154. def readI16(self):
  155. buff = self.trans.readAll(2)
  156. val, = unpack('!h', buff)
  157. return val
  158. def readI32(self):
  159. buff = self.trans.readAll(4)
  160. val, = unpack('!i', buff)
  161. return val
  162. def readI64(self):
  163. buff = self.trans.readAll(8)
  164. val, = unpack('!q', buff)
  165. return val
  166. def readDouble(self):
  167. buff = self.trans.readAll(8)
  168. val, = unpack('!d', buff)
  169. return val
  170. def readString(self):
  171. len = self.readI32()
  172. str = self.trans.readAll(len)
  173. return str
  174. class TBinaryProtocolFactory:
  175. def __init__(self, strictRead=False, strictWrite=True):
  176. self.strictRead = strictRead
  177. self.strictWrite = strictWrite
  178. def getProtocol(self, trans):
  179. prot = TBinaryProtocol(trans, self.strictRead, self.strictWrite)
  180. return prot
  181. class TBinaryProtocolAccelerated(TBinaryProtocol):
  182. """C-Accelerated version of TBinaryProtocol.
  183. This class does not override any of TBinaryProtocol's methods,
  184. but the generated code recognizes it directly and will call into
  185. our C module to do the encoding, bypassing this object entirely.
  186. We inherit from TBinaryProtocol so that the normal TBinaryProtocol
  187. encoding can happen if the fastbinary module doesn't work for some
  188. reason. (TODO(dreiss): Make this happen sanely in more cases.)
  189. In order to take advantage of the C module, just use
  190. TBinaryProtocolAccelerated instead of TBinaryProtocol.
  191. NOTE: This code was contributed by an external developer.
  192. The internal Thrift team has reviewed and tested it,
  193. but we cannot guarantee that it is production-ready.
  194. Please feel free to report bugs and/or success stories
  195. to the public mailing list.
  196. """
  197. pass
  198. class TBinaryProtocolAcceleratedFactory:
  199. def getProtocol(self, trans):
  200. return TBinaryProtocolAccelerated(trans)