|
19 | 19 |
|
20 | 20 |
|
21 | 21 | class TcpConnection(object):
|
| 22 | + def __init__(self): |
| 23 | + self.bytes_required = False |
| 24 | + |
22 | 25 | """
|
23 | 26 | Connection-related methods for TcpTransport.
|
24 | 27 | """
|
@@ -163,15 +166,32 @@ def _recv_msg(self):
|
163 | 166 | # https://github.com/basho/riak-python-client/issues/425
|
164 | 167 | raise BadResource(e)
|
165 | 168 | mv = memoryview(msgbuf)
|
166 |
| - msg_code, = struct.unpack("B", mv[0:1]) |
| 169 | + mcb = mv[0:1] |
| 170 | + if self.bytes_required: |
| 171 | + mcb = mcb.tobytes() |
| 172 | + try: |
| 173 | + msg_code, = struct.unpack("B", mcb) |
| 174 | + except struct.error: |
| 175 | + # NB: Python 2.7.3 requires this |
| 176 | + # http://bugs.python.org/issue10212 |
| 177 | + msg_code, = struct.unpack("B", mv[0:1].tobytes()) |
| 178 | + self.bytes_required = True |
167 | 179 | data = mv[1:].tobytes()
|
168 | 180 | return (msg_code, data)
|
169 | 181 |
|
170 | 182 | def _recv_pkt(self):
|
171 | 183 | # TODO FUTURE re-use buffer
|
172 | 184 | msglen_buf = self._recv(4)
|
173 | 185 | # NB: msg length is an unsigned int
|
174 |
| - msglen, = struct.unpack('!I', msglen_buf) |
| 186 | + if self.bytes_required: |
| 187 | + msglen_buf = bytes(msglen_buf) |
| 188 | + try: |
| 189 | + msglen, = struct.unpack('!I', msglen_buf) |
| 190 | + except struct.error: |
| 191 | + # NB: Python 2.7.3 requires this |
| 192 | + # http://bugs.python.org/issue10212 |
| 193 | + msglen, = struct.unpack('!I', bytes(msglen_buf)) |
| 194 | + self.bytes_required = True |
175 | 195 | return self._recv(msglen)
|
176 | 196 |
|
177 | 197 | def _recv(self, msglen):
|
|
0 commit comments