Skip to content

Commit 3481889

Browse files
authored
Cleanup (#921)
* Cleanup * black
1 parent 58b331e commit 3481889

File tree

3 files changed

+11
-28
lines changed

3 files changed

+11
-28
lines changed

README.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ The following examples make use of a simple table
9090
`email` varchar(255) COLLATE utf8_bin NOT NULL,
9191
`password` varchar(255) COLLATE utf8_bin NOT NULL,
9292
PRIMARY KEY (`id`)
93-
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
93+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8_bin
9494
AUTO_INCREMENT=1 ;
9595
9696
@@ -103,10 +103,9 @@ The following examples make use of a simple table
103103
user='user',
104104
password='passwd',
105105
db='db',
106-
charset='utf8mb4',
107106
cursorclass=pymysql.cursors.DictCursor)
108107
109-
try:
108+
with connection:
110109
with connection.cursor() as cursor:
111110
# Create a new record
112111
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
@@ -122,8 +121,7 @@ The following examples make use of a simple table
122121
cursor.execute(sql, ('webmaster@python.org',))
123122
result = cursor.fetchone()
124123
print(result)
125-
finally:
126-
connection.close()
124+
127125
128126
This example will print:
129127

pymysql/connections.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,6 @@
4747

4848
DEBUG = False
4949

50-
_py_version = sys.version_info[:2]
51-
52-
53-
def _fast_surrogateescape(s):
54-
return s.decode("ascii", "surrogateescape")
55-
56-
57-
def _makefile(sock, mode):
58-
return sock.makefile(mode)
59-
60-
6150
TEXT_TYPES = {
6251
FIELD_TYPE.BIT,
6352
FIELD_TYPE.BLOB,
@@ -76,12 +65,12 @@ def _makefile(sock, mode):
7665
MAX_PACKET_LEN = 2 ** 24 - 1
7766

7867

79-
def pack_int24(n):
68+
def _pack_int24(n):
8069
return struct.pack("<I", n)[:3]
8170

8271

8372
# https://dev.mysql.com/doc/internals/en/integer.html#packet-Protocol::LengthEncodedInteger
84-
def lenenc_int(i):
73+
def _lenenc_int(i):
8574
if i < 0:
8675
raise ValueError(
8776
"Encoding %d is less than 0 - no representation in LengthEncodedInteger" % i
@@ -535,7 +524,7 @@ def escape_string(self, s):
535524

536525
def _quote_bytes(self, s):
537526
if self.server_status & SERVER_STATUS.SERVER_STATUS_NO_BACKSLASH_ESCAPES:
538-
return "'%s'" % (_fast_surrogateescape(s.replace(b"'", b"''")),)
527+
return "'%s'" % (s.replace(b"'", b"''").decode("ascii", "surrogateescape"),)
539528
return converters.escape_bytes(s)
540529

541530
def cursor(self, cursor=None):
@@ -638,7 +627,7 @@ def connect(self, sock=None):
638627
sock.settimeout(None)
639628

640629
self._sock = sock
641-
self._rfile = _makefile(sock, "rb")
630+
self._rfile = sock.makefile("rb")
642631
self._next_seq_id = 0
643632

644633
self._get_server_information()
@@ -686,7 +675,7 @@ def write_packet(self, payload):
686675
"""
687676
# Internal note: when you build packet manually and calls _write_bytes()
688677
# directly, you should set self._next_seq_id properly.
689-
data = pack_int24(len(payload)) + int2byte(self._next_seq_id) + payload
678+
data = _pack_int24(len(payload)) + int2byte(self._next_seq_id) + payload
690679
if DEBUG:
691680
dump_packet(data)
692681
self._write_bytes(data)
@@ -859,7 +848,7 @@ def _request_authentication(self):
859848
self.write_packet(data_init)
860849

861850
self._sock = self.ctx.wrap_socket(self._sock, server_hostname=self.host)
862-
self._rfile = _makefile(self._sock, "rb")
851+
self._rfile = self._sock.makefile("rb")
863852
self._secure = True
864853

865854
data = data_init + self.user + b"\0"
@@ -892,7 +881,7 @@ def _request_authentication(self):
892881
authresp = b"\0" # empty password
893882

894883
if self.server_capabilities & CLIENT.PLUGIN_AUTH_LENENC_CLIENT_DATA:
895-
data += lenenc_int(len(authresp)) + authresp
884+
data += _lenenc_int(len(authresp)) + authresp
896885
elif self.server_capabilities & CLIENT.SECURE_CONNECTION:
897886
data += struct.pack("B", len(authresp)) + authresp
898887
else: # pragma: no cover - not testing against servers without secure auth (>=5.0)

setup.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#!/usr/bin/env python
2-
import io
32
from setuptools import setup, find_packages
43

54
version = "0.10.1"
65

7-
with io.open("./README.rst", encoding="utf-8") as f:
6+
with open("./README.rst", encoding="utf-8") as f:
87
readme = f.read()
98

109
setup(
@@ -23,10 +22,7 @@
2322
},
2423
classifiers=[
2524
"Development Status :: 5 - Production/Stable",
26-
"Programming Language :: Python :: 2",
27-
"Programming Language :: Python :: 2.7",
2825
"Programming Language :: Python :: 3",
29-
"Programming Language :: Python :: 3.5",
3026
"Programming Language :: Python :: 3.6",
3127
"Programming Language :: Python :: 3.7",
3228
"Programming Language :: Python :: 3.8",

0 commit comments

Comments
 (0)