Skip to content

Remove Python 2.7 and 3.5 support. #915

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 4 commits into from
Jan 2, 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
6 changes: 1 addition & 5 deletions pymysql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"""
import sys

from ._compat import PY2
from .constants import FIELD_TYPE
from .converters import escape_dict, escape_sequence, escape_string
from .err import (
Expand Down Expand Up @@ -79,10 +78,7 @@ def __hash__(self):

def Binary(x):
"""Return x as a binary type."""
if PY2:
return bytearray(x)
else:
return bytes(x)
return bytes(x)


def Connect(*args, **kwargs):
Expand Down
9 changes: 2 additions & 7 deletions pymysql/_auth.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Implements auth methods
"""
from ._compat import PY2
from .err import OperationalError
from .util import byte2int, int2byte

Expand Down Expand Up @@ -46,8 +45,6 @@ def scramble_native_password(password, message):

def _my_crypt(message1, message2):
result = bytearray(message1)
if PY2:
message2 = bytearray(message2)

for i in range(len(result)):
result[i] ^= message2[i]
Expand All @@ -61,7 +58,7 @@ def _my_crypt(message1, message2):
SCRAMBLE_LENGTH_323 = 8


class RandStruct_323(object):
class RandStruct_323:

def __init__(self, seed1, seed2):
self.max_value = 0x3FFFFFFF
Expand Down Expand Up @@ -188,7 +185,7 @@ def _xor_password(password, salt):
# See https://github.com/mysql/mysql-server/blob/7d10c82196c8e45554f27c00681474a9fb86d137/sql/auth/sha2_password.cc#L939-L945
salt = salt[:SCRAMBLE_LENGTH]
password_bytes = bytearray(password)
salt = bytearray(salt) # for PY2 compat.
#salt = bytearray(salt) # for PY2 compat.
salt_len = len(salt)
for i in range(len(password_bytes)):
password_bytes[i] ^= salt[i % salt_len]
Expand Down Expand Up @@ -259,8 +256,6 @@ def scramble_caching_sha2(password, nonce):
p3 = hashlib.sha256(p2 + nonce).digest()

res = bytearray(p1)
if PY2:
p3 = bytearray(p3)
for i in range(len(p3)):
res[i] ^= p3[i]

Expand Down
21 changes: 0 additions & 21 deletions pymysql/_compat.py

This file was deleted.

2 changes: 1 addition & 1 deletion pymysql/charset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}


class Charset(object):
class Charset:
def __init__(self, id, name, collation, is_default):
self.id, self.name, self.collation = id, name, collation
self.is_default = is_default == 'Yes'
Expand Down
60 changes: 16 additions & 44 deletions pymysql/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
# http://dev.mysql.com/doc/internals/en/client-server-protocol.html
# Error codes:
# https://dev.mysql.com/doc/refman/5.5/en/error-handling.html
from __future__ import print_function
from ._compat import PY2, range_type, text_type, str_type, JYTHON, IRONPYTHON

import errno
import io
import os
import socket
import struct
Expand Down Expand Up @@ -47,32 +43,11 @@

_py_version = sys.version_info[:2]

if PY2:
pass
elif _py_version < (3, 6):
# See http://bugs.python.org/issue24870
_surrogateescape_table = [chr(i) if i < 0x80 else chr(i + 0xdc00) for i in range(256)]

def _fast_surrogateescape(s):
return s.decode('latin1').translate(_surrogateescape_table)
else:
def _fast_surrogateescape(s):
return s.decode('ascii', 'surrogateescape')

# socket.makefile() in Python 2 is not usable because very inefficient and
# bad behavior about timeout.
# XXX: ._socketio doesn't work under IronPython.
if PY2 and not IRONPYTHON:
# read method of file-like returned by sock.makefile() is very slow.
# So we copy io-based one from Python 3.
from ._socketio import SocketIO

def _makefile(sock, mode):
return io.BufferedReader(SocketIO(sock, mode))
else:
# socket.makefile in Python 3 is nice.
def _makefile(sock, mode):
return sock.makefile(mode)
def _fast_surrogateescape(s):
return s.decode('ascii', 'surrogateescape')

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


TEXT_TYPES = {
Expand Down Expand Up @@ -113,7 +88,7 @@ def lenenc_int(i):
raise ValueError("Encoding %x is larger than %x - no representation in LengthEncodedInteger" % (i, (1 << 64)))


class Connection(object):
class Connection:
"""
Representation of a socket with a mysql server.

Expand Down Expand Up @@ -258,7 +233,7 @@ def _config(key, arg):
raise ValueError("port should be of type int")
self.user = user or DEFAULT_USER
self.password = password or b""
if isinstance(self.password, text_type):
if isinstance(self.password, str):
self.password = self.password.encode('latin1')
self.db = database
self.unix_socket = unix_socket
Expand Down Expand Up @@ -459,7 +434,7 @@ def escape(self, obj, mapping=None):

Non-standard, for internal use; do not use this in your applications.
"""
if isinstance(obj, str_type):
if isinstance(obj, str):
return "'" + self.escape_string(obj) + "'"
if isinstance(obj, (bytes, bytearray)):
ret = self._quote_bytes(obj)
Expand Down Expand Up @@ -503,11 +478,8 @@ def cursor(self, cursor=None):
def query(self, sql, unbuffered=False):
# if DEBUG:
# print("DEBUG: sending query:", sql)
if isinstance(sql, text_type) and not (JYTHON or IRONPYTHON):
if PY2:
sql = sql.encode(self.encoding)
else:
sql = sql.encode(self.encoding, 'surrogateescape')
if isinstance(sql, str):
sql = sql.encode(self.encoding, 'surrogateescape')
self._execute_command(COMMAND.COM_QUERY, sql)
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
return self._affected_rows
Expand Down Expand Up @@ -758,7 +730,7 @@ def _execute_command(self, command, sql):
self.next_result()
self._result = None

if isinstance(sql, text_type):
if isinstance(sql, str):
sql = sql.encode(self.encoding)

packet_size = min(MAX_PACKET_LEN, len(sql) + 1) # +1 is for command
Expand Down Expand Up @@ -791,7 +763,7 @@ def _request_authentication(self):
raise ValueError("Did not specify a username")

charset_id = charset_by_name(self.charset).id
if isinstance(self.user, text_type):
if isinstance(self.user, str):
self.user = self.user.encode(self.encoding)

data_init = struct.pack('<iIB23s', self.client_flag, MAX_PACKET_LEN, charset_id, b'')
Expand Down Expand Up @@ -840,7 +812,7 @@ def _request_authentication(self):
data += authresp + b'\0'

if self.db and self.server_capabilities & CLIENT.CONNECT_WITH_DB:
if isinstance(self.db, text_type):
if isinstance(self.db, str):
self.db = self.db.encode(self.encoding)
data += self.db + b'\0'

Expand Down Expand Up @@ -1049,7 +1021,7 @@ def get_server_info(self):
NotSupportedError = err.NotSupportedError


class MySQLResult(object):
class MySQLResult:

def __init__(self, connection):
"""
Expand Down Expand Up @@ -1219,7 +1191,7 @@ def _get_descriptions(self):
conn_encoding = self.connection.encoding
description = []

for i in range_type(self.field_count):
for i in range(self.field_count):
field = self.connection._read_packet(FieldDescriptorPacket)
self.fields.append(field)
description.append(field.description())
Expand Down Expand Up @@ -1254,7 +1226,7 @@ def _get_descriptions(self):
self.description = tuple(description)


class LoadLocalFile(object):
class LoadLocalFile:
def __init__(self, filename, connection):
self.filename = filename
self.connection = connection
Expand Down
68 changes: 12 additions & 56 deletions pymysql/converters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from ._compat import PY2, text_type, long_type, JYTHON, IRONPYTHON, unichr

import datetime
from decimal import Decimal
import re
Expand All @@ -17,7 +15,7 @@ def escape_item(val, charset, mapping=None):
# Fallback to default when no encoder found
if not encoder:
try:
encoder = mapping[text_type]
encoder = mapping[str]
except KeyError:
raise TypeError("no default type converter defined")

Expand Down Expand Up @@ -47,9 +45,6 @@ def escape_set(val, charset, mapping=None):
def escape_bool(value, mapping=None):
return str(int(value))

def escape_object(value, mapping=None):
return str(value)

def escape_int(value, mapping=None):
return str(value)

Expand All @@ -61,7 +56,7 @@ def escape_float(value, mapping=None):
s += 'e0'
return s

_escape_table = [unichr(x) for x in range(128)]
_escape_table = [chr(x) for x in range(128)]
_escape_table[0] = u'\\0'
_escape_table[ord('\\')] = u'\\\\'
_escape_table[ord('\n')] = u'\\n'
Expand All @@ -70,57 +65,21 @@ def escape_float(value, mapping=None):
_escape_table[ord('"')] = u'\\"'
_escape_table[ord("'")] = u"\\'"

def _escape_unicode(value, mapping=None):
def escape_string(value, mapping=None):
"""escapes *value* without adding quote.

Value should be unicode
"""
return value.translate(_escape_table)

if PY2:
def escape_string(value, mapping=None):
"""escape_string escapes *value* but not surround it with quotes.

Value should be bytes or unicode.
"""
if isinstance(value, unicode):
return _escape_unicode(value)
assert isinstance(value, (bytes, bytearray))
value = value.replace('\\', '\\\\')
value = value.replace('\0', '\\0')
value = value.replace('\n', '\\n')
value = value.replace('\r', '\\r')
value = value.replace('\032', '\\Z')
value = value.replace("'", "\\'")
value = value.replace('"', '\\"')
return value

def escape_bytes_prefixed(value, mapping=None):
assert isinstance(value, (bytes, bytearray))
return b"_binary'%s'" % escape_string(value)

def escape_bytes(value, mapping=None):
assert isinstance(value, (bytes, bytearray))
return b"'%s'" % escape_string(value)

else:
escape_string = _escape_unicode

# On Python ~3.5, str.decode('ascii', 'surrogateescape') is slow.
# (fixed in Python 3.6, http://bugs.python.org/issue24870)
# Workaround is str.decode('latin1') then translate 0x80-0xff into 0udc80-0udcff.
# We can escape special chars and surrogateescape at once.
_escape_bytes_table = _escape_table + [chr(i) for i in range(0xdc80, 0xdd00)]

def escape_bytes_prefixed(value, mapping=None):
return "_binary'%s'" % value.decode('latin1').translate(_escape_bytes_table)
def escape_bytes_prefixed(value, mapping=None):
return "_binary'%s'" % value.decode('ascii', 'surrogateescape')

def escape_bytes(value, mapping=None):
return "'%s'" % value.decode('latin1').translate(_escape_bytes_table)

def escape_bytes(value, mapping=None):
return "'%s'" % value.decode('ascii', 'surrogateescape')

def escape_unicode(value, mapping=None):
return u"'%s'" % _escape_unicode(value)

def escape_str(value, mapping=None):
return "'%s'" % escape_string(str(value), mapping)
Expand Down Expand Up @@ -190,7 +149,7 @@ def convert_datetime(obj):
True

"""
if not PY2 and isinstance(obj, (bytes, bytearray)):
if isinstance(obj, (bytes, bytearray)):
obj = obj.decode('ascii')

m = DATETIME_RE.match(obj)
Expand Down Expand Up @@ -224,7 +183,7 @@ def convert_timedelta(obj):
can accept values as (+|-)DD HH:MM:SS. The latter format will not
be parsed correctly by this function.
"""
if not PY2 and isinstance(obj, (bytes, bytearray)):
if isinstance(obj, (bytes, bytearray)):
obj = obj.decode('ascii')

m = TIMEDELTA_RE.match(obj)
Expand Down Expand Up @@ -272,7 +231,7 @@ def convert_time(obj):
to be treated as time-of-day and not a time offset, then you can
use set this function as the converter for FIELD_TYPE.TIME.
"""
if not PY2 and isinstance(obj, (bytes, bytearray)):
if isinstance(obj, (bytes, bytearray)):
obj = obj.decode('ascii')

m = TIME_RE.match(obj)
Expand Down Expand Up @@ -303,7 +262,7 @@ def convert_date(obj):
True

"""
if not PY2 and isinstance(obj, (bytes, bytearray)):
if isinstance(obj, (bytes, bytearray)):
obj = obj.decode('ascii')
try:
return datetime.date(*[ int(x) for x in obj.split('-', 2) ])
Expand All @@ -327,10 +286,9 @@ def through(x):
encoders = {
bool: escape_bool,
int: escape_int,
long_type: escape_int,
float: escape_float,
str: escape_str,
text_type: escape_unicode,
bytes: escape_bytes,
tuple: escape_sequence,
list: escape_sequence,
set: escape_sequence,
Expand All @@ -345,8 +303,6 @@ def through(x):
Decimal: Decimal2Literal,
}

if not PY2 or JYTHON or IRONPYTHON:
encoders[bytes] = escape_bytes

decoders = {
FIELD_TYPE.BIT: convert_bit,
Expand Down
Loading