8000 Use constants (#1028) · PyMySQL/PyMySQL@3fb9dd9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3fb9dd9

Browse files
authored
Use constants (#1028)
1 parent 2beebd9 commit 3fb9dd9

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

pymysql/connections.py

+22-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from . import _auth
1414

1515
from .charset import charset_by_name, charset_by_id
16-
from .constants import CLIENT, COMMAND, CR, FIELD_TYPE, SERVER_STATUS
16+
from .constants import CLIENT, COMMAND, CR, ER, FIELD_TYPE, SERVER_STATUS
1717
from . import converters
1818
from .cursors import Cursor
1919
from .optionfile import Parser
@@ -441,7 +441,10 @@ def get_autocommit(self):
441441
def _read_ok_packet(self):
442442
pkt = self._read_packet()
443443
if not pkt.is_ok_packet():
444-
raise err.OperationalError(2014, "Command Out of Sync")
444+
raise err.OperationalError(
445+
CR.CR_COMMANDS_OUT_OF_SYNC,
446+
"Command Out of Sync",
447+
)
445448
ok = OKPacketWrapper(pkt)
446449
self.server_status = ok.server_status
447450
return ok
@@ -654,7 +657,8 @@ def connect(self, sock=None):
654657

655658
if isinstance(e, (OSError, IOError, socket.error)):
656659
exc = err.OperationalError(
657-
2003, "Can't connect to MySQL server on %r (%s)" % (self.host, e)
660+
CR.CR_CONN_HOST_ERROR,
661+
"Can't connect to MySQL server on %r (%s)" % (self.host, e),
658662
)
659663
# Keep original exception and traceback to investigate error.
660664
exc.original_exception = e
@@ -945,7 +949,7 @@ def _process_auth(self, plugin_name, auth_packet):
945949
except AttributeError:
946950
if plugin_name != b"dialog":
947951
raise err.OperationalError(
948-
2059,
952+
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
949953
"Authentication plugin '%s'"
950954
" not loaded: - %r missing authenticate method"
951955
% (plugin_name, type(handler)),
@@ -983,21 +987,21 @@ def _process_auth(self, plugin_name, auth_packet):
983987
self.write_packet(resp + b"\0")
984988
except AttributeError:
985989
raise err.OperationalError(
986-
2059,
990+
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
987991
"Authentication plugin '%s'"
988992
" not loaded: - %r missing prompt method"
989993
% (plugin_name, handler),
990994
)
991995
except TypeError:
992996
raise err.OperationalError(
993-
2061,
997+
CR.CR_AUTH_PLUGIN_ERR,
994998
"Authentication plugin '%s'"
995999
" %r didn't respond with string. Returned '%r' to prompt %r"
9961000
% (plugin_name, handler, resp, prompt),
9971001
)
9981002
else:
9991003
raise err.OperationalError(
1000-
2059,
1004+
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
10011005
"Authentication plugin '%s' not configured" % (plugin_name,),
10021006
)
10031007
pkt = self._read_packet()
@@ -1007,7 +1011,8 @@ def _process_auth(self, plugin_name, auth_packet):
10071011
return pkt
10081012
else:
10091013
raise err.OperationalError(
1010-
2059, "Authentication plugin '%s' not configured" % plugin_name
1014+
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
1015+
"Authentication plugin '%s' not configured" % plugin_name,
10111016
)
10121017

10131018
self.write_packet(data)
@@ -1024,7 +1029,7 @@ def _get_auth_plugin_handler(self, plugin_name):
10241029
handler = plugin_class(self)
10251030
except TypeError:
10261031
raise err.OperationalError(
1027-
2059,
1032+
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
10281033
"Authentication plugin '%s'"
10291034
" not loaded: - %r cannot be constructed with connection object"
10301035
% (plugin_name, plugin_class),
@@ -1211,7 +1216,10 @@ def _read_load_local_packet(self, first_packet):
12111216
if (
12121217
not ok_packet.is_ok_packet()
12131218
): # pragma: no cover - upstream induced protocol error
1214-
raise err.OperationalError(2014, "Commands Out of Sync")
1219+
raise err.OperationalError(
1220+
CR.CR_COMMANDS_OUT_OF_SYNC,
1221+
"Commands Out of Sync",
1222+
)
12151223
self._read_ok_packet(ok_packet)
12161224

12171225
def _check_packet_is_eof(self, packet):
@@ -1357,7 +1365,10 @@ def send_data(self):
13571365
break
13581366
conn.write_packet(chunk)
13591367
except IOError:
1360-
raise err.OperationalError(1017, f"Can't find file '{self.filename}'")
1368+
raise err.OperationalError(
1369+
ER.FILE_NOT_FOUND,
1370+
f"Can't find file '{self.filename}'",
1371+
)
13611372
finally:
13621373
# send the empty packet to signify we are done sending data
13631374
conn.write_packet(b"")

0 commit comments

Comments
 (0)
0