@@ -221,34 +221,14 @@ def pack_int24(n):
221
221
def unpack_uint16 (n ):
222
222
return struct .unpack ('<H' , n [0 :2 ])[0 ]
223
223
224
-
225
- # TODO: stop using bit-shifting in these functions...
226
- # TODO: rename to "uint" to make it clear they're unsigned...
227
224
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 ]
234
226
235
227
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 ]
242
229
243
230
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 ]
252
232
253
233
254
234
class MysqlPacket (object ):
@@ -355,7 +335,6 @@ def read_length_coded_binary(self):
355
335
elif c == UNSIGNED_INT24_COLUMN :
356
336
return unpack_int24 (self .read (UNSIGNED_INT24_LENGTH ))
357
337
elif c == UNSIGNED_INT64_COLUMN :
358
- # TODO: what was 'longlong'? confirm it wasn't used?
359
338
return unpack_int64 (self .read (UNSIGNED_INT64_LENGTH ))
360
339
361
340
def read_length_coded_string (self ):
@@ -376,7 +355,8 @@ def is_ok_packet(self):
376
355
def is_eof_packet (self ):
377
356
# http://dev.mysql.com/doc/internals/en/generic-response-packets.html#packet-EOF_Packet
378
357
# 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 '
380
360
381
361
def is_resultset_packet (self ):
382
362
field_count = ord (self .__data [0 :1 ])
@@ -1079,7 +1059,7 @@ def init_unbuffered_query(self):
1079
1059
self ._read_ok_packet (first_packet )
1080
1060
self .unbuffered_active = False
1081
1061
else :
1082
- self .field_count = byte2int ( first_packet .read ( 1 ) )
1062
+ self .field_count = first_packet .read_length_coded_binary ( )
1083
1063
self ._get_descriptions ()
1084
1064
1085
1065
# Apparently, MySQLdb picks this number because it's the maximum
@@ -1104,7 +1084,7 @@ def _check_packet_is_eof(self, packet):
1104
1084
return False
1105
1085
1106
1086
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 ( )
1108
1088
self ._get_descriptions ()
1109
1089
self ._read_rowdata_packet ()
1110
1090
0 commit comments