Skip to content

Commit c7d4858

Browse files
committed
Fix coding style.
1 parent 3a9ec7f commit c7d4858

File tree

4 files changed

+63
-49
lines changed

4 files changed

+63
-49
lines changed

Diff for: pymysql/connections.py

+51-44
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
11
# Python implementation of the MySQL client-server protocol
22
# http://dev.mysql.com/doc/internals/en/client-server-protocol.html
3+
# Error codes:
4+
# http://dev.mysql.com/doc/refman/5.5/en/error-messages-client.html
35

46
from __future__ import print_function
57
from ._compat import PY2, range_type, text_type, str_type, JYTHON, IRONPYTHON
8+
DEBUG = False
69

710
import errno
811
from functools import partial
9-
import os
1012
import hashlib
13+
import io
14+
import os
1115
import socket
16+
import struct
17+
import sys
1218

1319
try:
1420
import ssl
1521
SSL_ENABLED = True
1622
except ImportError:
23+
ssl = None
1724
SSL_ENABLED = False
1825

19-
import struct
20-
import sys
2126
if PY2:
2227
import ConfigParser as configparser
2328
else:
2429
import configparser
2530

26-
import io
27-
2831
try:
2932
import getpass
3033
DEFAULT_USER = getpass.getuser()
34+
del getpass
3135
except ImportError:
3236
DEFAULT_USER = None
3337

3438

3539
from .charset import MBLENGTH, charset_by_name, charset_by_id
3640
from .cursors import Cursor
37-
from .constants import FIELD_TYPE
38-
from .constants import SERVER_STATUS
39-
from .constants.CLIENT import *
40-
from .constants.COMMAND import *
41+
from .constants import CLIENT, COMMAND, FIELD_TYPE, SERVER_STATUS
4142
from .util import byte2int, int2byte
4243
from .converters import escape_item, encoders, decoders, escape_string
4344
from .err import (
@@ -96,8 +97,6 @@ def _makefile(sock, mode):
9697

9798
sha_new = partial(hashlib.new, 'sha1')
9899

99-
DEBUG = False
100-
101100
NULL_COLUMN = 251
102101
UNSIGNED_CHAR_COLUMN = 251
103102
UNSIGNED_SHORT_COLUMN = 252
@@ -114,9 +113,8 @@ def _makefile(sock, mode):
114113

115114

116115
def dump_packet(data):
117-
118116
def is_ascii(data):
119-
if 65 <= byte2int(data) <= 122: #data.isalnum():
117+
if 65 <= byte2int(data) <= 122:
120118
if isinstance(data, int):
121119
return chr(data)
122120
return data
@@ -134,9 +132,9 @@ def is_ascii(data):
134132
pass
135133
dump_data = [data[i:i+16] for i in range_type(0, min(len(data), 256), 16)]
136134
for d in dump_data:
137-
print(' '.join(map(lambda x:"{:02X}".format(byte2int(x)), d)) +
135+
print(' '.join(map(lambda x: "{:02X}".format(byte2int(x)), d)) +
138136
' ' * (16 - len(d)) + ' ' * 2 +
139-
' '.join(map(lambda x:"{}".format(is_ascii(x)), d)))
137+
' '.join(map(lambda x: "{}".format(is_ascii(x)), d)))
140138
print("-" * 88)
141139
print()
142140

@@ -487,14 +485,21 @@ def __init__(self, host="localhost", user=None, password="",
487485
unix_socket: Optionally, you can use a unix socket rather than TCP/IP.
488486
charset: Charset you want to use.
489487
sql_mode: Default SQL_MODE to use.
490-
read_default_file: Specifies my.cnf file to read these parameters from under the [client] section.
491-
conv: Decoders dictionary to use instead of the default one. This is used to provide custom marshalling of types. See converters.
492-
use_unicode: Whether or not to default to unicode strings. This option defaults to true for Py3k.
488+
read_default_file:
489+
Specifies my.cnf file to read these parameters from under the [client] section.
490+
conv:
491+
Decoders dictionary to use instead of the default one.
492+
This is used to provide custom marshalling of types. See converters.
493+
use_unicode:
494+
Whether or not to default to unicode strings.
495+
This option defaults to true for Py3k.
493496
client_flag: Custom flags to send to MySQL. Find potential values in constants.CLIENT.
494497
cursorclass: Custom cursor class to use.
495498
init_command: Initial SQL statement to run when connection is established.
496499
connect_timeout: Timeout before throwing an exception when connecting.
497-
ssl: A dict of arguments similar to mysql_ssl_set()'s parameters. For now the capath and cipher arguments are not supported.
500+
ssl:
501+
A dict of arguments similar to mysql_ssl_set()'s parameters.
502+
For now the capath and cipher arguments are not supported.
498503
read_default_group: Group to read from in the configuration file.
499504
compress; Not supported
500505
named_pipe: Not supported
@@ -524,7 +529,7 @@ def __init__(self, host="localhost", user=None, password="",
524529
if not SSL_ENABLED:
525530
raise NotImplementedError("ssl module not found")
526531
self.ssl = True
527-
client_flag |= SSL
532+
client_flag |= CLIENT.SSL
528533
for k in ('key', 'cert', 'ca'):
529534
v = None
530535
if k in ssl:
@@ -577,10 +582,9 @@ def _config(key, default):
577582

578583
self.encoding = charset_by_name(self.charset).encoding
579584

580-
client_flag |= CAPABILITIES
581-
client_flag |= MULTI_STATEMENTS
585+
client_flag |= CLIENT.CAPABILITIES | CLIENT.MULTI_STATEMENTS
582586
if self.db:
583-
client_flag |= CONNECT_WITH_DB
587+
client_flag |= CLIENT.CONNECT_WITH_DB
584588
self.client_flag = client_flag
585589

586590
self.cursorclass = cursorclass
@@ -603,7 +607,7 @@ def close(self):
603607
''' Send the quit message and close the socket '''
604608
if self.socket is None:
605609
raise Error("Already closed")
606-
send_data = struct.pack('<i', 1) + int2byte(COM_QUIT)
610+
send_data = struct.pack('<i', 1) + int2byte(COMMAND.COM_QUIT)
607611
try:
608612
self._write_bytes(send_data)
609613
except Exception:
@@ -647,28 +651,28 @@ def _read_ok_packet(self):
647651

648652
def _send_autocommit_mode(self):
649653
''' Set whether or not to commit after every execute() '''
650-
self._execute_command(COM_QUERY, "SET AUTOCOMMIT = %s" %
654+
self._execute_command(COMMAND.COM_QUERY, "SET AUTOCOMMIT = %s" %
651655
self.escape(self.autocommit_mode))
652656
self._read_ok_packet()
653657

654658
def begin(self):
655659
"""Begin transaction."""
656-
self._execute_command(COM_QUERY, "BEGIN")
660+
self._execute_command(COMMAND.COM_QUERY, "BEGIN")
657661
self._read_ok_packet()
658662

659663
def commit(self):
660664
''' Commit changes to stable storage '''
661-
self._execute_command(COM_QUERY, "COMMIT")
665+
self._execute_command(COMMAND.COM_QUERY, "COMMIT")
662666
self._read_ok_packet()
663667

664668
def rollback(self):
665669
''' Roll back the current transaction '''
666-
self._execute_command(COM_QUERY, "ROLLBACK")
670+
self._execute_command(COMMAND.COM_QUERY, "ROLLBACK")
667671
self._read_ok_packet()
668672

669673
def select_db(self, db):
670674
'''Set current db'''
671-
self._execute_command(COM_INIT_DB, db)
675+
self._execute_command(COMMAND.COM_INIT_DB, db)
672676
self._read_ok_packet()
673677

674678
def escape(self, obj):
@@ -710,7 +714,7 @@ def query(self, sql, unbuffered=False):
710714
# print("DEBUG: sending query:", sql)
711715
if isinstance(sql, text_type) and not (JYTHON or IRONPYTHON):
712716
sql = sql.encode(self.encoding)
713-
self._execute_command(COM_QUERY, sql)
717+
self._execute_command(COMMAND.COM_QUERY, sql)
714718
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
715719
return self._affected_rows
716720

@@ -723,7 +727,7 @@ def affected_rows(self):
723727

724728
def kill(self, thread_id):
725729
arg = struct.pack('<I', thread_id)
726-
self._execute_command(COM_PROCESS_KILL, arg)
730+
self._execute_command(COMMAND.COM_PROCESS_KILL, arg)
727731
return self._read_ok_packet()
728732

729733
def ping(self, reconnect=True):
@@ -735,7 +739,7 @@ def ping(self, reconnect=True):
735739
else:
736740
raise Error("Already closed")
737741
try:
738-
self._execute_command(COM_PING, "")
742+
self._execute_command(COMMAND.COM_PING, "")
739743
return self._read_ok_packet()
740744
except Exception:
741745
if reconnect:
@@ -748,7 +752,7 @@ def set_charset(self, charset):
748752
# Make sure charset is supported.
749753
encoding = charset_by_name(charset).encoding
750754

751-
self._execute_command(COM_QUERY, "SET NAMES %s" % self.escape(charset))
755+
self._execute_command(COMMAND.COM_QUERY, "SET NAMES %s" % self.escape(charset))
752756
self._read_packet()
753757
self.charset = charset
754758
self.encoding = encoding
@@ -766,7 +770,7 @@ def _connect(self):
766770
while True:
767771
try:
768772
sock = socket.create_connection(
769-
(self.host, self.port), self.connect_timeout)
773+
(self.host, self.port), self.connect_timeout)
770774
break
771775
except (OSError, IOError) as e:
772776
if e.errno == errno.EINTR:
@@ -804,7 +808,6 @@ def _connect(self):
804808
raise OperationalError(
805809
2003, "Can't connect to MySQL server on %r (%s)" % (self.host, e))
806810

807-
808811
def _read_packet(self, packet_type=MysqlPacket):
809812
"""Read an entire "mysql packet" in its entirety from the network
810813
and return a MysqlPacket type that represents the results.
@@ -839,10 +842,11 @@ def _read_bytes(self, num_bytes):
839842
if e.errno == errno.EINTR:
840843
continue
841844
raise OperationalError(
842-
2013, "Lost connection to MySQL server during query (%r)" % (e,))
845+
2013,
846+
"Lost connection to MySQL server during query (%s)" % (e,))
843847
if len(data) < num_bytes:
844-
raise OperationalError(2013,
845-
"Lost connection to MySQL server during query")
848+
raise OperationalError(
849+
2013, "Lost connection to MySQL server during query")
846850
return data
847851

848852
def _write_bytes(self, data):
@@ -909,9 +913,9 @@ def _execute_command(self, command, sql):
909913
seq_id += 1
910914

911915
def _request_authentication(self):
912-
self.client_flag |= CAPABILITIES
916+
self.client_flag |= CLIENT.CAPABILITIES
913917
if self.server_version.startswith('5'):
914-
self.client_flag |= MULTI_RESULTS
918+
self.client_flag |= CLIENT.MULTI_RESULTS
915919

916920
if self.user is None:
917921
raise ValueError("Did not specify a username")
@@ -920,8 +924,8 @@ def _request_authentication(self):
920924
if isinstance(self.user, text_type):
921925
self.user = self.user.encode(self.encoding)
922926

923-
data_init = struct.pack('<i', self.client_flag) + struct.pack("<I", 1) + \
924-
int2byte(charset_id) + int2byte(0)*23
927+
data_init = (struct.pack('<i', self.client_flag) + struct.pack("<I", 1) +
928+
int2byte(charset_id) + int2byte(0)*23)
925929

926930
next_packet = 1
927931

@@ -1018,8 +1022,9 @@ def _get_server_information(self):
10181022
i += 10
10191023

10201024
if len(data) >= i + salt_len:
1021-
self.salt += data[i:i+salt_len] # salt_len includes auth_plugin_data_part_1 and filler
1022-
#TODO: AUTH PLUGIN NAME may appeare here.
1025+
# salt_len includes auth_plugin_data_part_1 and filler
1026+
self.salt += data[i:i+salt_len]
1027+
# TODO: AUTH PLUGIN NAME may appeare here.
10231028

10241029
def get_server_info(self):
10251030
return self.server_version
@@ -1185,3 +1190,5 @@ def _get_descriptions(self):
11851190
eof_packet = self.connection._read_packet()
11861191
assert eof_packet.is_eof_packet(), 'Protocol error, expecting EOF'
11871192
self.description = tuple(description)
1193+
1194+
# g:khuno_ignore='E226,E301,E701'

Diff for: pymysql/constants/CLIENT.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
LONG_PASSWORD = 1
32
FOUND_ROWS = 1 << 1
43
LONG_FLAG = 1 << 2
@@ -12,9 +11,9 @@
1211
INTERACTIVE = 1 << 10
1312
SSL = 1 << 11
1413
IGNORE_SIGPIPE = 1 << 12
15-
TRANSACTIONS = 1 << 13
14+
TRANSACTIONS = 1 << 13
1615
SECURE_CONNECTION = 1 << 15
1716
MULTI_STATEMENTS = 1 << 16
1817
MULTI_RESULTS = 1 << 17
19-
CAPABILITIES = LONG_PASSWORD|LONG_FLAG|TRANSACTIONS| \
20-
PROTOCOL_41|SECURE_CONNECTION
18+
CAPABILITIES = (LONG_PASSWORD | LONG_FLAG | TRANSACTIONS |
19+
PROTOCOL_41 | SECURE_CONNECTION)

Diff for: pymysql/err.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from .constants import ER
44

5+
56
class MySQLError(Exception):
67
"""Exception related to operation with MySQL."""
78

@@ -10,6 +11,7 @@ class Warning(Warning, MySQLError):
1011
"""Exception raised for important warnings like data truncations
1112
while inserting, etc."""
1213

14+
1315
class Error(MySQLError):
1416
"""Exception that is the base class of all other error exceptions
1517
(not Warning)."""
@@ -102,15 +104,17 @@ def _get_error_info(data):
102104
# version 4.0
103105
return (errno, None, data[3:].decode("utf8", 'replace'))
104106

107+
105108
def _check_mysql_exception(errinfo):
106109
errno, sqlstate, errorvalue = errinfo
107110
errorclass = error_map.get(errno, None)
108111
if errorclass:
109-
raise errorclass(errno,errorvalue)
112+
raise errorclass(errno, errorvalue)
110113

111114
# couldn't find the right error number
112115
raise InternalError(errno, errorvalue)
113116

117+
114118
def raise_mysql_exception(data):
115119
errinfo = _get_error_info(data)
116120
_check_mysql_exception(errinfo)

Diff for: setup.cfg

+4
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
[wheel]
22
universal = 1
3+
4+
[flake8]
5+
ignore = E226,E301,E701
6+
exclude = tests,build

0 commit comments

Comments
 (0)