Skip to content

Cleanup #921

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 2 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
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
8 changes: 3 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ The following examples make use of a simple table
`email` varchar(255) COLLATE utf8_bin NOT NULL,
`password` varchar(255) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8_bin
AUTO_INCREMENT=1 ;


Expand All @@ -103,10 +103,9 @@ The following examples make use of a simple table
user='user',
password='passwd',
db='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)

try:
with connection:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
Expand All @@ -122,8 +121,7 @@ The following examples make use of a simple table
cursor.execute(sql, ('webmaster@python.org',))
result = cursor.fetchone()
print(result)
finally:
connection.close()


This example will print:

Expand Down
25 changes: 7 additions & 18 deletions pymysql/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,6 @@

DEBUG = False

_py_version = sys.version_info[:2]


def _fast_surrogateescape(s):
return s.decode("ascii", "surrogateescape")


def _makefile(sock, mode):
return sock.makefile(mode)


TEXT_TYPES = {
FIELD_TYPE.BIT,
FIELD_TYPE.BLOB,
Expand All @@ -76,12 +65,12 @@ def _makefile(sock, mode):
MAX_PACKET_LEN = 2 ** 24 - 1


def pack_int24(n):
def _pack_int24(n):
return struct.pack("<I", n)[:3]


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

def _quote_bytes(self, s):
if self.server_status & SERVER_STATUS.SERVER_STATUS_NO_BACKSLASH_ESCAPES:
return "'%s'" % (_fast_surrogateescape(s.replace(b"'", b"''")),)
return "'%s'" % (s.replace(b"'", b"''").decode("ascii", "surrogateescape"),)
return converters.escape_bytes(s)

def cursor(self, cursor=None):
Expand Down Expand Up @@ -638,7 +627,7 @@ def connect(self, sock=None):
sock.settimeout(None)

self._sock = sock
self._rfile = _makefile(sock, "rb")
self._rfile = sock.makefile("rb")
self._next_seq_id = 0

self._get_server_information()
Expand Down Expand Up @@ -686,7 +675,7 @@ def write_packet(self, payload):
"""
# Internal note: when you build packet manually and calls _write_bytes()
# directly, you should set self._next_seq_id properly.
data = pack_int24(len(payload)) + int2byte(self._next_seq_id) + payload
data = _pack_int24(len(payload)) + int2byte(self._next_seq_id) + payload
if DEBUG:
dump_packet(data)
self._write_bytes(data)
Expand Down Expand Up @@ -859,7 +848,7 @@ def _request_authentication(self):
self.write_packet(data_init)

self._sock = self.ctx.wrap_socket(self._sock, server_hostname=self.host)
self._rfile = _makefile(self._sock, "rb")
self._rfile = self._sock.makefile("rb")
self._secure = True

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

if self.server_capabilities & CLIENT.PLUGIN_AUTH_LENENC_CLIENT_DATA:
data += lenenc_int(len(authresp)) + authresp
data += _lenenc_int(len(authresp)) + authresp
elif self.server_capabilities & CLIENT.SECURE_CONNECTION:
data += struct.pack("B", len(authresp)) + authresp
else: # pragma: no cover - not testing against servers without secure auth (>=5.0)
Expand Down
6 changes: 1 addition & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/usr/bin/env python
import io
from setuptools import setup, find_packages

version = "0.10.1"

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

setup(
Expand All @@ -23,10 +22,7 @@
},
classifiers=[
"Development Status :: 5 - Production/Stable",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
Expand Down