Prevent UnboundLocalError during unbuffered query (#1174) · PyMySQL/PyMySQL@95635f5 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
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)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.