Skip to content

Commit 95635f5

Browse files
palm002methane
andauthored
Prevent UnboundLocalError during unbuffered query (#1174)
Addresses the issue of `UnboundLocalError` which occurs when `MySQLResult` class fails to initialize due to a `SystemExit` exception by initialising the `MySQLResult` object before `try/except` block. Co-authored-by: Inada Naoki <songofacandy@gmail.com>
1 parent 53b35f7 commit 95635f5

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

pymysql/connections.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -812,16 +812,10 @@ def _write_bytes(self, data):
812812

813813
def _read_query_result(self, unbuffered=False):
814814
self._result = None
815+
result = MySQLResult(self)
815816
if unbuffered:
816-
try:
817-
result = MySQLResult(self)
818-
result.init_unbuffered_query()
819-
except:
820-
result.unbuffered_active = False
821-
result.connection = None
822-
raise
817+
result.init_unbuffered_query()
823818
else:
824-
result = MySQLResult(self)
825819
result.read()
826820
self._result = result
827821
if result.server_status is not None:
@@ -1212,17 +1206,16 @@ def init_unbuffered_query(self):
12121206
:raise OperationalError: If the connection to the MySQL server is lost.
12131207
:raise InternalError:
12141208
"""
1215-
self.unbuffered_active = True
12161209
first_packet = self.connection._read_packet()
12171210

12181211
if first_packet.is_ok_packet():
1219-
self._read_ok_packet(first_packet)
1220-
self.unbuffered_active = False
12211212
self.connection = None
1213+
self._read_ok_packet(first_packet)
12221214
elif first_packet.is_load_local_packet():
1223-
self._read_load_local_packet(first_packet)
1224-
self.unbuffered_active = False
1225-
self.connection = None
1215+
try:
1216+
self._read_load_local_packet(first_packet)
1217+
finally:
1218+
self.connection = None
12261219
else:
12271220
self.field_count = first_packet.read_length_encoded_integer()
12281221
self._get_descriptions()
@@ -1231,6 +1224,7 @@ def init_unbuffered_query(self):
12311224
# value of a 64bit unsigned integer. Since we're emulating MySQLdb,
12321225
# we set it to this instead of None, which would be preferred.
12331226
self.affected_rows = 18446744073709551615
1227+
self.unbuffered_active = True
12341228

12351229
def _read_ok_packet(self, first_packet):
12361230
ok_packet = OKPacketWrapper(first_packet)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy