Skip to content

we have these constants, why not make use of them? #1028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions pymysql/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from . import _auth

from .charset import charset_by_name, charset_by_id
from .constants import CLIENT, COMMAND, CR, FIELD_TYPE, SERVER_STATUS
from .constants import CLIENT, COMMAND, CR, ER, FIELD_TYPE, SERVER_STATUS
from . import converters
from .cursors import Cursor
from .optionfile import Parser
Expand Down Expand Up @@ -441,7 +441,10 @@ def get_autocommit(self):
def _read_ok_packet(self):
pkt = self._read_packet()
if not pkt.is_ok_packet():
raise err.OperationalError(2014, "Command Out of Sync")
raise err.OperationalError(
CR.CR_COMMANDS_OUT_OF_SYNC,
"Command Out of Sync",
)
ok = OKPacketWrapper(pkt)
self.server_status = ok.server_status
return ok
Expand Down Expand Up @@ -654,7 +657,8 @@ def connect(self, sock=None):

if isinstance(e, (OSError, IOError, socket.error)):
exc = err.OperationalError(
2003, "Can't connect to MySQL server on %r (%s)" % (self.host, e)
CR.CR_CONN_HOST_ERROR,
"Can't connect to MySQL server on %r (%s)" % (self.host, e),
)
# Keep original exception and traceback to investigate error.
exc.original_exception = e
Expand Down Expand Up @@ -945,7 +949,7 @@ def _process_auth(self, plugin_name, auth_packet):
except AttributeError:
if plugin_name != b"dialog":
raise err.OperationalError(
2059,
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
"Authentication plugin '%s'"
" not loaded: - %r missing authenticate method"
% (plugin_name, type(handler)),
Expand Down Expand Up @@ -983,21 +987,21 @@ def _process_auth(self, plugin_name, auth_packet):
self.write_packet(resp + b"\0")
except AttributeError:
raise err.OperationalError(
2059,
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
"Authentication plugin '%s'"
" not loaded: - %r missing prompt method"
% (plugin_name, handler),
)
except TypeError:
raise err.OperationalError(
2061,
CR.CR_AUTH_PLUGIN_ERR,
"Authentication plugin '%s'"
" %r didn't respond with string. Returned '%r' to prompt %r"
% (plugin_name, handler, resp, prompt),
)
else:
raise err.OperationalError(
2059,
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
"Authentication plugin '%s' not configured" % (plugin_name,),
)
pkt = self._read_packet()
Expand All @@ -1007,7 +1011,8 @@ def _process_auth(self, plugin_name, auth_packet):
return pkt
else:
raise err.OperationalError(
2059, "Authentication plugin '%s' not configured" % plugin_name
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
"Authentication plugin '%s' not configured" % plugin_name,
)

self.write_packet(data)
Expand All @@ -1024,7 +1029,7 @@ def _get_auth_plugin_handler(self, plugin_name):
handler = plugin_class(self)
except TypeError:
raise err.OperationalError(
2059,
CR.CR_AUTH_PLUGIN_CANNOT_LOAD,
"Authentication plugin '%s'"
" not loaded: - %r cannot be constructed with connection object"
% (plugin_name, plugin_class),
Expand Down Expand Up @@ -1211,7 +1216,10 @@ def _read_load_local_packet(self, first_packet):
if (
not ok_packet.is_ok_packet()
): # pragma: no cover - upstream induced protocol error
raise err.OperationalError(2014, "Commands Out of Sync")
raise err.OperationalError(
CR.CR_COMMANDS_OUT_OF_SYNC,
"Commands Out of Sync",
)
self._read_ok_packet(ok_packet)

def _check_packet_is_eof(self, packet):
Expand Down Expand Up @@ -1357,7 +1365,10 @@ def send_data(self):
break
conn.write_packet(chunk)
except IOError:
raise err.OperationalError(1017, f"Can't find file '{self.filename}'")
raise err.OperationalError(
ER.FILE_NOT_FOUND,
f"Can't find file '{self.filename}'",
)
finally:
# send the empty packet to signify we are done sending data
conn.write_packet(b"")