Skip to content

Commit 2622e52

Browse files
committed
Merge pull request PyMySQL#431 from methane/fix/load-local-chunk-size
Fix misuse of `max_allowed_packet`
2 parents 0b5ff77 + 735ea10 commit 2622e52

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

pymysql/connections.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ def __init__(self, host=None, user=None, password="",
569569
autocommit: Autocommit mode. None means use server default. (default: False)
570570
local_infile: Boolean to enable the use of LOAD DATA LOCAL command. (default: False)
571571
max_allowed_packet: Max size of packet sent to server in bytes. (default: 16MB)
572+
Only used to limit size of "LOAD LOCAL INFILE" data packet smaller than default (16KB).
572573
defer_connect: Don't explicitly connect on contruction - wait for connect call.
573574
(default: False)
574575
auth_plugin_map: A dict of plugin names to a class that processes that plugin.
@@ -676,7 +677,7 @@ def _config(key, arg):
676677
self.socket = None
677678
else:
678679
self.connect()
679-
680+
680681
def _create_ssl_ctx(self, sslp):
681682
if isinstance(sslp, ssl.SSLContext):
682683
return sslp
@@ -1027,26 +1028,25 @@ def _execute_command(self, command, sql):
10271028
if isinstance(sql, text_type):
10281029
sql = sql.encode(self.encoding)
10291030

1030-
# +1 is for command
1031-
chunk_size = min(self.max_allowed_packet, len(sql) + 1)
1031+
packet_size = min(MAX_PACKET_LEN, len(sql) + 1) # +1 is for command
10321032

10331033
# tiny optimization: build first packet manually instead of
10341034
# calling self..write_packet()
1035-
prelude = struct.pack('<iB', chunk_size, command)
1036-
packet = prelude + sql[:chunk_size-1]
1035+
prelude = struct.pack('<iB', packet_size, command)
1036+
packet = prelude + sql[:packet_size-1]
10371037
self._write_bytes(packet)
10381038
if DEBUG: dump_packet(packet)
10391039
self._next_seq_id = 1
10401040

1041-
if chunk_size < self.max_allowed_packet:
1041+
if packet_size < MAX_PACKET_LEN:
10421042
return
10431043

1044-
sql = sql[chunk_size-1:]
1044+
sql = sql[packet_size-1:]
10451045
while True:
1046-
chunk_size = min(self.max_allowed_packet, len(sql))
1047-
self.write_packet(sql[:chunk_size])
1048-
sql = sql[chunk_size:]
1049-
if not sql and chunk_size < self.max_allowed_packet:
1046+
packet_size = min(MAX_PACKET_LEN, len(sql))
1047+
self.write_packet(sql[:packet_size])
1048+
sql = sql[packet_size:]
1049+
if not sql and packet_size < MAX_PACKET_LEN:
10501050
break
10511051

10521052
def _request_authentication(self):
@@ -1447,11 +1447,9 @@ def send_data(self):
14471447

14481448
try:
14491449
with open(self.filename, 'rb') as open_file:
1450-
chunk_size = conn.max_allowed_packet
1451-
packet = b""
1452-
1450+
packet_size = min(conn.max_allowed_packet, 16*1024) # 16KB is efficient enough
14531451
while True:
1454-
chunk = open_file.read(chunk_size)
1452+
chunk = open_file.read(packet_size)
14551453
if not chunk:
14561454
break
14571455
conn.write_packet(chunk)

0 commit comments

Comments
 (0)