8000 Read field_count as LengthEncodedInteger. · gcmcom/PyMySQL@addf15c · GitHub
[go: up one dir, main page]

Skip to content

Commit addf15c

Browse files
committed
Read field_count as LengthEncodedInteger.
1 parent d7d47cd commit addf15c

File tree

1 file changed

+7
-27
lines changed

1 file changed

+7
-27
lines changed

pymysql/connections.py

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -221,34 +221,14 @@ def pack_int24(n):
221221
def unpack_uint16(n):
222222
return struct.unpack('<H', n[0:2])[0]
223223

224-
225-
# TODO: stop using bit-shifting in these functions...
226-
# TODO: rename to "uint" to make it clear they're unsigned...
227224
def unpack_int24(n):
228-
try:
229-
return struct.unpack('B',n[0])[0] + (struct.unpack('B', n[1])[0] << 8) +\
230-
(struct.unpack('B',n[2])[0] << 16)
231-
except TypeError:
232-
return n[0] + (n[1] << 8) + (n[2] << 16)
233-
225+
return struct.unpack('<I', n + b'\0')[0]
234226

235227
def unpack_int32(n):
236-
try:
237-
return struct.unpack('B',n[0])[0] + (struct.unpack('B', n[1])[0] << 8) +\
238-
(struct.unpack('B',n[2])[0] << 16) + (struct.unpack('B', n[3])[0] << 24)
239-
except TypeError:
240-
return n[0] + (n[1] << 8) + (n[2] << 16) + (n[3] << 24)
241-
228+
return struct.unpack('<I', n)[0]
242229

243230
def unpack_int64(n):
244-
try:
245-
return struct.unpack('B',n[0])[0] + (struct.unpack('B', n[1])[0]<<8) +\
246-
(struct.unpack('B',n[2])[0] << 16) + (struct.unpack('B',n[3])[0]<<24)+\
247-
(struct.unpack('B',n[4])[0] << 32) + (struct.unpack('B',n[5])[0]<<40)+\
248-
(struct.unpack('B',n[6])[0] << 48) + (struct.unpack('B',n[7])[0]<<56)
249-
except TypeError:
250-
return n[0] + (n[1] << 8) + (n[2] << 16) + (n[3] << 24) +\
251-
(n[4] << 32) + (n[5] << 40) + (n[6] << 48) + (n[7] << 56)
231+
return struct.unpack('<Q', n)[0]
252232

253233

254234
class MysqlPacket(object):
@@ -355,7 +335,6 @@ def read_length_coded_binary(self):
355335
elif c == UNSIGNED_INT24_COLUMN:
356336
return unpack_int24(self.read(UNSIGNED_INT24_LENGTH))
357337
elif c == UNSIGNED_INT64_COLUMN:
358-
# TODO: what was 'longlong'? confirm it wasn't used?
359338
return unpack_int64(self.read(UNSIGNED_INT64_LENGTH))
360339

361340
def read_length_coded_string(self):
@@ -376,7 +355,8 @@ def is_ok_packet(self):
376355
def is_eof_packet(self):
377356
# http://dev.mysql.com/doc/internals/en/generic-response-packets.html#packet-EOF_Packet
378357
# Caution: \xFE may be LengthEncodedInteger.
379-
return len(self.__data) < 10 and self.__data[0:1] == b'\xfe'
358+
# If \xFE is LengthEncodedInteger header, 8bytes followed.
359+
return len(self.__data) < 9 and self.__data[0:1] == b'\xfe'
380360

381361
def is_resultset_packet(self):
382362
field_count = ord(self.__data[0:1])
@@ -1079,7 +1059,7 @@ def init_unbuffered_query(self):
10791059
self._read_ok_packet(first_packet)
10801060
self.unbuffered_active = False
10811061
else:
1082-
self.field_count = byte2int(first_packet.read(1))
1062+
self.field_count = first_packet.read_length_coded_binary()
10831063
self._get_descriptions()
10841064

10851065
# Apparently, MySQLdb picks this number because it's the maximum
@@ -1104,7 +1084,7 @@ def _check_packet_is_eof(self, packet):
11041084
return False
11051085

11061086
def _read_result_packet(self, first_packet):
1107-
self.field_count = byte2int(first_packet.read(1))
1087+
self.field_count = first_packet.read_length_coded_binary()
11081088
self._get_descriptions()
11091089
self._read_rowdata_packet()
11101090

0 commit comments

Comments
 (0)
0