Compact implementation of the Thrift protocol driver.
| 124 | |
| 125 | |
| 126 | class TCompactProtocol(TProtocolBase): |
| 127 | """Compact implementation of the Thrift protocol driver.""" |
| 128 | |
| 129 | PROTOCOL_ID = 0x82 |
| 130 | VERSION = 1 |
| 131 | VERSION_MASK = 0x1f |
| 132 | TYPE_MASK = 0xe0 |
| 133 | TYPE_BITS = 0x07 |
| 134 | TYPE_SHIFT_AMOUNT = 5 |
| 135 | |
| 136 | def __init__(self, trans, |
| 137 | string_length_limit=None, |
| 138 | container_length_limit=None): |
| 139 | TProtocolBase.__init__(self, trans) |
| 140 | self.state = CLEAR |
| 141 | self.__last_fid = 0 |
| 142 | self.__bool_fid = None |
| 143 | self.__bool_value = None |
| 144 | self.__structs = [] |
| 145 | self.__containers = [] |
| 146 | self.string_length_limit = string_length_limit |
| 147 | self.container_length_limit = container_length_limit |
| 148 | |
| 149 | def _check_string_length(self, length): |
| 150 | self._check_length(self.string_length_limit, length) |
| 151 | |
| 152 | def _check_container_length(self, length): |
| 153 | self._check_length(self.container_length_limit, length) |
| 154 | |
| 155 | def __writeVarint(self, n): |
| 156 | writeVarint(self.trans, n) |
| 157 | |
| 158 | def writeMessageBegin(self, name, type, seqid): |
| 159 | assert self.state == CLEAR |
| 160 | self.__writeUByte(self.PROTOCOL_ID) |
| 161 | self.__writeUByte(self.VERSION | (type << self.TYPE_SHIFT_AMOUNT)) |
| 162 | # The sequence id is a signed 32-bit integer but the compact protocol |
| 163 | # writes this out as a "var int" which is always positive, and attempting |
| 164 | # to write a negative number results in an infinite loop, so we may |
| 165 | # need to do some conversion here... |
| 166 | tseqid = seqid |
| 167 | if tseqid < 0: |
| 168 | tseqid = 2147483648 + (2147483648 + tseqid) |
| 169 | self.__writeVarint(tseqid) |
| 170 | self.__writeBinary(bytes(name, 'utf-8')) |
| 171 | self.state = VALUE_WRITE |
| 172 | |
| 173 | def writeMessageEnd(self): |
| 174 | assert self.state == VALUE_WRITE |
| 175 | self.state = CLEAR |
| 176 | |
| 177 | def writeStructBegin(self, name): |
| 178 | assert self.state in (CLEAR, CONTAINER_WRITE, VALUE_WRITE), self.state |
| 179 | self.__structs.append((self.state, self.__last_fid)) |
| 180 | self.state = FIELD_WRITE |
| 181 | self.__last_fid = 0 |
| 182 | |
| 183 | def writeStructEnd(self): |
no test coverage detected