Skip to content

Commit f889038

Browse files
authored
Reformat with black (PyMySQL#920)
1 parent b93a87a commit f889038

38 files changed

+2296
-1626
lines changed

pymysql/__init__.py

+96-29
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,26 @@
2626
from .constants import FIELD_TYPE
2727
from .converters import escape_dict, escape_sequence, escape_string
2828
from .err import (
29-
Warning, Error, InterfaceError, DataError,
30-
DatabaseError, OperationalError, IntegrityError, InternalError,
31-
NotSupportedError, ProgrammingError, MySQLError)
29+
Warning,
30+
Error,
31+
InterfaceError,
32+
DataError,
33+
DatabaseError,
34+
OperationalError,
35+
IntegrityError,
36+
InternalError,
37+
NotSupportedError,
38+
ProgrammingError,
39+
MySQLError,
40+
)
3241
from .times import (
33-
Date, Time, Timestamp,
34-
DateFromTicks, TimeFromTicks, TimestampFromTicks)
42+
Date,
43+
Time,
44+
Timestamp,
45+
DateFromTicks,
46+
TimeFromTicks,
47+
TimestampFromTicks,
48+
)
3549

3650

3751
VERSION = (0, 10, 1, None)
@@ -45,7 +59,6 @@
4559

4660

4761
class DBAPISet(frozenset):
48-
4962
def __ne__(self, other):
5063
if isinstance(other, set):
5164
return frozenset.__ne__(self, other)
@@ -62,18 +75,32 @@ def __hash__(self):
6275
return frozenset.__hash__(self)
6376

6477

65-
STRING = DBAPISet([FIELD_TYPE.ENUM, FIELD_TYPE.STRING,
66-
FIELD_TYPE.VAR_STRING])
67-
BINARY = DBAPISet([FIELD_TYPE.BLOB, FIELD_TYPE.LONG_BLOB,
68-
FIELD_TYPE.MEDIUM_BLOB, FIELD_TYPE.TINY_BLOB])
69-
NUMBER = DBAPISet([FIELD_TYPE.DECIMAL, FIELD_TYPE.DOUBLE, FIELD_TYPE.FLOAT,
70-
FIELD_TYPE.INT24, FIELD_TYPE.LONG, FIELD_TYPE.LONGLONG,
71-
FIELD_TYPE.TINY, FIELD_TYPE.YEAR])
72-
DATE = DBAPISet([FIELD_TYPE.DATE, FIELD_TYPE.NEWDATE])
73-
TIME = DBAPISet([FIELD_TYPE.TIME])
78+
STRING = DBAPISet([FIELD_TYPE.ENUM, FIELD_TYPE.STRING, FIELD_TYPE.VAR_STRING])
79+
BINARY = DBAPISet(
80+
[
81+
FIELD_TYPE.BLOB,
82+
FIELD_TYPE.LONG_BLOB,
83+
FIELD_TYPE.MEDIUM_BLOB,
84+
FIELD_TYPE.TINY_BLOB,
85+
]
86+
)
87+
NUMBER = DBAPISet(
88+
[
89+
FIELD_TYPE.DECIMAL,
90+
FIELD_TYPE.DOUBLE,
91+
FIELD_TYPE.FLOAT,
92+
FIELD_TYPE.INT24,
93+
FIELD_TYPE.LONG,
94+
FIELD_TYPE.LONGLONG,
95+
FIELD_TYPE.TINY,
96+
FIELD_TYPE.YEAR,
97+
]
98+
)
99+
DATE = DBAPISet([FIELD_TYPE.DATE, FIELD_TYPE.NEWDATE])
100+
TIME = DBAPISet([FIELD_TYPE.TIME])
74101
TIMESTAMP = DBAPISet([FIELD_TYPE.TIMESTAMP, FIELD_TYPE.DATETIME])
75-
DATETIME = TIMESTAMP
76-
ROWID = DBAPISet()
102+
DATETIME = TIMESTAMP
103+
ROWID = DBAPISet()
77104

78105

79106
def Binary(x):
@@ -87,9 +114,12 @@ def Connect(*args, **kwargs):
87114
more information.
88115
"""
89116
from .connections import Connection
117+
90118
return Connection(*args, **kwargs)
91119

120+
92121
from . import connections as _orig_conn
122+
93123
if _orig_conn.Connection.__init__.__doc__ is not None:
94124
Connect.__doc__ = _orig_conn.Connection.__init__.__doc__
95125
del _orig_conn
@@ -99,7 +129,8 @@ def get_client_info(): # for MySQLdb compatibility
99129
version = VERSION
100130
if VERSION[3] is None:
101131
version = VERSION[:3]
102-
return '.'.join(map(str, version))
132+
return ".".join(map(str, version))
133+
103134

104135
connect = Connection = Connect
105136

@@ -110,9 +141,11 @@ def get_client_info(): # for MySQLdb compatibility
110141

111142
__version__ = get_client_info()
112143

144+
113145
def thread_safe():
114146
return True # match MySQLdb.thread_safe()
115147

148+
116149
def install_as_MySQLdb():
117150
"""
118151
After this function is called, any application that imports MySQLdb or
@@ -122,16 +155,50 @@ def install_as_MySQLdb():
122155

123156

124157
__all__ = [
125-
'BINARY', 'Binary', 'Connect', 'Connection', 'DATE', 'Date',
126-
'Time', 'Timestamp', 'DateFromTicks', 'TimeFromTicks', 'TimestampFromTicks',
127-
'DataError', 'DatabaseError', 'Error', 'FIELD_TYPE', 'IntegrityError',
128-
'InterfaceError', 'InternalError', 'MySQLError', 'NULL', 'NUMBER',
129-
'NotSupportedError', 'DBAPISet', 'OperationalError', 'ProgrammingError',
130-
'ROWID', 'STRING', 'TIME', 'TIMESTAMP', 'Warning', 'apilevel', 'connect',
131-
'connections', 'constants', 'converters', 'cursors',
132-
'escape_dict', 'escape_sequence', 'escape_string', 'get_client_info',
133-
'paramstyle', 'threadsafety', 'version_info',
134-
158+
"BINARY",
159+
"Binary",
160+
"Connect",
161+
"Connection",
162+
"DATE",
163+
"Date",
164+
"Time",
165+
"Timestamp",
166+
"DateFromTicks",
167+
"TimeFromTicks",
168+
"TimestampFromTicks",
169+
"DataError",
170+
"DatabaseError",
171+
"Error",
172+
"FIELD_TYPE",
173+
"IntegrityError",
174+
"InterfaceError",
175+
"InternalError",
176+
"MySQLError",
177+
"NULL",
178+
"NUMBER",
179+
"NotSupportedError",
180+
"DBAPISet",
181+
"OperationalError",
182+
"ProgrammingError",
183+
"ROWID",
184+
"STRING",
185+
"TIME",
186+
"TIMESTAMP",
187+
"Warning",
188+
"apilevel",
189+
"connect",
190+
"connections",
191+
"constants",
192+
"converters",
193+
"cursors",
194+
"escape_dict",
195+
"escape_sequence",
196+
"escape_string",
197+
"get_client_info",
198+
"paramstyle",
199+
"threadsafety",
200+
"version_info",
135201
"install_as_MySQLdb",
136-
"NULL", "__version__",
202+
"NULL",
203+
"__version__",
137204
]

pymysql/_auth.py

+26-19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from cryptography.hazmat.backends import default_backend
1010
from cryptography.hazmat.primitives import serialization, hashes
1111
from cryptography.hazmat.primitives.asymmetric import padding
12+
1213
_have_cryptography = True
1314
except ImportError:
1415
_have_cryptography = False
@@ -22,7 +23,7 @@
2223

2324
DEBUG = False
2425
SCRAMBLE_LENGTH = 20
25-
sha1_new = partial(hashlib.new, 'sha1')
26+
sha1_new = partial(hashlib.new, "sha1")
2627

2728

2829
# mysql_native_password
@@ -32,7 +33,7 @@
3233
def scramble_native_password(password, message):
3334
"""Scramble used for mysql_native_password"""
3435
if not password:
35-
return b''
36+
return b""
3637

3738
stage1 = sha1_new(password).digest()
3839
stage2 = sha1_new(stage1).digest()
@@ -59,7 +60,6 @@ def _my_crypt(message1, message2):
5960

6061

6162
class RandStruct_323:
62-
6363
def __init__(self, seed1, seed2):
6464
self.max_value = 0x3FFFFFFF
6565
self.seed1 = seed1 % self.max_value
@@ -73,8 +73,10 @@ def my_rnd(self):
7373

7474
def scramble_old_password(password, message):
7575
"""Scramble for old_password"""
76-
warnings.warn("old password (for MySQL <4.1) is used. Upgrade your password with newer auth method.\n"
77-
"old password support will be removed in future PyMySQL version")
76+
warnings.warn(
77+
"old password (for MySQL <4.1) is used. Upgrade your password with newer auth method.\n"
78+
"old password support will be removed in future PyMySQL version"
79+
)
7880
hash_pass = _hash_password_323(password)
7981
hash_message = _hash_password_323(message[:SCRAMBLE_LENGTH_323])
8082
hash_pass_n = struct.unpack(">LL", hash_pass)
@@ -100,7 +102,7 @@ def _hash_password_323(password):
100102
nr2 = 0x12345671
101103

102104
# x in py3 is numbers, p27 is chars
103-
for c in [byte2int(x) for x in password if x not in (' ', '\t', 32, 9)]:
105+
for c in [byte2int(x) for x in password if x not in (" ", "\t", 32, 9)]:
104106
nr ^= (((nr & 63) + add) * c) + (nr << 8) & 0xFFFFFFFF
105107
nr2 = (nr2 + ((nr2 << 8) ^ nr)) & 0xFFFFFFFF
106108
add = (add + c) & 0xFFFFFFFF
@@ -120,9 +122,12 @@ def _init_nacl():
120122
global _nacl_bindings
121123
try:
122124
from nacl import bindings
125+
123126
_nacl_bindings = bindings
124127
except ImportError:
125-
raise RuntimeError("'pynacl' package is required for ed25519_password auth method")
128+
raise RuntimeError(
129+
"'pynacl' package is required for ed25519_password auth method"
130+
)
126131

127132

128133
def _scalar_clamp(s32):
@@ -185,7 +190,7 @@ def _xor_password(password, salt):
185190
# See https://github.com/mysql/mysql-server/blob/7d10c82196c8e45554f27c00681474a9fb86d137/sql/auth/sha2_password.cc#L939-L945
186191
salt = salt[:SCRAMBLE_LENGTH]
187192
password_bytes = bytearray(password)
188-
#salt = bytearray(salt) # for PY2 compat.
193+
# salt = bytearray(salt) # for PY2 compat.
189194
salt_len = len(salt)
190195
for i in range(len(password_bytes)):
191196
password_bytes[i] ^= salt[i % salt_len]
@@ -198,8 +203,10 @@ def sha2_rsa_encrypt(password, salt, public_key):
198203
Used for sha256_password and caching_sha2_password.
199204
"""
200205
if not _have_cryptography:
201-
raise RuntimeError("'cryptography' package is required for sha256_password or caching_sha2_password auth methods")
202-
message = _xor_password(password + b'\0', salt)
206+
raise RuntimeError(
207+
"'cryptography' package is required for sha256_password or caching_sha2_password auth methods"
208+
)
209+
message = _xor_password(password + b"\0", salt)
203210
rsa_key = serialization.load_pem_public_key(public_key, default_backend())
204211
return rsa_key.encrypt(
205212
message,
@@ -215,7 +222,7 @@ def sha256_password_auth(conn, pkt):
215222
if conn._secure:
216223
if DEBUG:
217224
print("sha256: Sending plain password")
218-
data = conn.password + b'\0'
225+
data = conn.password + b"\0"
219226
return _roundtrip(conn, data)
220227

221228
if pkt.is_auth_switch_request():
@@ -224,20 +231,20 @@ def sha256_password_auth(conn, pkt):
224231
# Request server public key
225232
if DEBUG:
226233
print("sha256: Requesting server public key")
227-
pkt = _roundtrip(conn, b'\1')
234+
pkt = _roundtrip(conn, b"\1")
228235

229236
if pkt.is_extra_auth_data():
230237
conn.server_public_key = pkt._data[1:]
231238
if DEBUG:
232-
print("Received public key:\n", conn.server_public_key.decode('ascii'))
239+
print("Received public key:\n", conn.server_public_key.decode("ascii"))
233240

234241
if conn.password:
235242
if not conn.server_public_key:
236243
raise OperationalError("Couldn't receive server's public key")
237244

238245
data = sha2_rsa_encrypt(conn.password, conn.salt, conn.server_public_key)
239246
else:
240-
data = b''
247+
data = b""
241248

242249
return _roundtrip(conn, data)
243250

@@ -249,7 +256,7 @@ def scramble_caching_sha2(password, nonce):
249256
XOR(SHA256(password), SHA256(SHA256(SHA256(password)), nonce))
250257
"""
251258
if not password:
252-
return b''
259+
return b""
253260

254261
p1 = hashlib.sha256(password).digest()
255262
p2 = hashlib.sha256(p1).digest()
@@ -265,7 +272,7 @@ def scramble_caching_sha2(password, nonce):
265272
def caching_sha2_password_auth(conn, pkt):
266273
# No password fast path
267274
if not conn.password:
268-
return _roundtrip(conn, b'')
275+
return _roundtrip(conn, b"")
269276

270277
if pkt.is_auth_switch_request():
271278
# Try from fast auth
@@ -305,18 +312,18 @@ def caching_sha2_password_auth(conn, pkt):
305312
if conn._secure:
306313
if DEBUG:
307314
print("caching sha2: Sending plain password via secure connection")
308-
return _roundtrip(conn, conn.password + b'\0')
315+
return _roundtrip(conn, conn.password + b"\0")
309316

310317
if not conn.server_public_key:
311-
pkt = _roundtrip(conn, b'\x02') # Request public key
318+
pkt = _roundtrip(conn, b"\x02") # Request public key
312319
if not pkt.is_extra_auth_data():
313320
raise OperationalError(
314321
"caching sha2: Unknown packet for public key: %s" % pkt._data[:1]
315322
)
316323

317324
conn.server_public_key = pkt._data[1:]
318325
if DEBUG:
319-
print(conn.server_public_key.decode('ascii'))
326+
print(conn.server_public_key.decode("ascii"))
320327

321328
data = sha2_rsa_encrypt(conn.password, conn.salt, conn.server_public_key)
322329
pkt = _roundtrip(conn, data)

pymysql/_socketio.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
import io
99
import errno
1010

11-
__all__ = ['SocketIO']
11+
__all__ = ["SocketIO"]
1212

1313
EINTR = errno.EINTR
1414
_blocking_errnos = (errno.EAGAIN, errno.EWOULDBLOCK)
1515

16+
1617
class SocketIO(io.RawIOBase):
1718

1819
"""Raw I/O implementation for stream sockets.
@@ -85,29 +86,25 @@ def write(self, b):
8586
raise
8687

8788
def readable(self):
88-
"""True if the SocketIO is open for reading.
89-
"""
89+
"""True if the SocketIO is open for reading."""
9090
if self.closed:
9191
raise ValueError("I/O operation on closed socket.")
9292
return self._reading
9393

9494
def writable(self):
95-
"""True if the SocketIO is open for writing.
96-
"""
95+
"""True if the SocketIO is open for writing."""
9796
if self.closed:
9897
raise ValueError("I/O operation on closed socket.")
9998
return self._writing
10099

101100
def seekable(self):
102-
"""True if the SocketIO is open for seeking.
103-
"""
101+
"""True if the SocketIO is open for seeking."""
104102
if self.closed:
105103
raise ValueError("I/O operation on closed socket.")
106104
return super().seekable()
107105

108106
def fileno(self):
109-
"""Return the file descriptor of the underlying socket.
110-
"""
107+
"""Return the file descriptor of the underlying socket."""
111108
self._checkClosed()
112109
return self._sock.fileno()
113110

@@ -131,4 +128,3 @@ def close(self):
131128
io.RawIOBase.close(self)
132129
self._sock._decref_socketios()
133130
self._sock = None
134-

0 commit comments

Comments
 (0)