Skip to content

Remove py27 support from Python sources. #394

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 8 commits into from
Nov 28, 2019
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
2 changes: 1 addition & 1 deletion INSTALL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ MySQLdb Installation
Prerequisites
-------------

+ Python 2.7, 3.5 or higher
+ Python 3.5 or higher

+ setuptools

Expand Down
9 changes: 2 additions & 7 deletions MySQLdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
paramstyle = "format"

from ._mysql import *
from MySQLdb.compat import PY2
from MySQLdb.constants import FIELD_TYPE
from MySQLdb.times import Date, Time, Timestamp, \
DateFromTicks, TimeFromTicks, TimestampFromTicks
Expand Down Expand Up @@ -71,12 +70,8 @@ def test_DBAPISet_set_equality_membership():
def test_DBAPISet_set_inequality_membership():
assert FIELD_TYPE.DATE != STRING

if PY2:
def Binary(x):
return bytearray(x)
else:
def Binary(x):
return bytes(x)
def Binary(x):
return bytes(x)

def Connect(*args, **kwargs):
"""Factory function for connections.Connection."""
Expand Down
4 changes: 1 addition & 3 deletions MySQLdb/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

https://www.python.org/dev/peps/pep-0249/
"""
from .compat import StandardError


class MySQLError(StandardError):
class MySQLError(Exception):
"""Exception related to operation with MySQL."""


Expand Down
14 changes: 0 additions & 14 deletions MySQLdb/compat.py

This file was deleted.

27 changes: 7 additions & 20 deletions MySQLdb/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import sys

from MySQLdb import cursors, _mysql
from MySQLdb.compat import unicode, PY2
from MySQLdb._exceptions import (
Warning, Error, InterfaceError, DataError,
DatabaseError, OperationalError, IntegrityError, InternalError,
Expand Down Expand Up @@ -77,14 +76,11 @@ class object, used to create cursors (keyword only)
columns are returned as bytes. Unicode objects will always
be encoded to the connection's character set regardless of
this setting.
Default to False on Python 2 and True on Python 3
so that you can always get python `str` object by default.
Default to True.

:param str charset:
If supplied, the connection character set will be changed
to this character set.
On Python 2, this option changes default value of `use_unicode`
option from False to True.

:param str auth_plugin:
If supplied, the connection default authentication plugin will be
Expand Down Expand Up @@ -146,13 +142,7 @@ class object, used to create cursors (keyword only)

cursorclass = kwargs2.pop('cursorclass', self.default_cursor)
charset = kwargs2.get('charset', '')

if charset or not PY2:
use_unicode = True
else:
use_unicode = False

use_unicode = kwargs2.pop('use_unicode', use_unicode)
use_unicode = kwargs2.pop('use_unicode', True)
sql_mode = kwargs2.pop('sql_mode', '')
self._binary_prefix = kwargs2.pop('binary_prefix', False)

Expand Down Expand Up @@ -201,9 +191,9 @@ def unicode_literal(u, dummy=None):
self.converter[t] = _bytes_or_str
# Unlike other string/blob types, JSON is always text.
# MySQL may return JSON with charset==binary.
self.converter[FIELD_TYPE.JSON] = unicode
self.converter[FIELD_TYPE.JSON] = str

self.encoders[unicode] = unicode_literal
self.encoders[str] = unicode_literal
self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS
if self._transactional:
if autocommit is not None:
Expand Down Expand Up @@ -248,20 +238,17 @@ def literal(self, o):
Non-standard. For internal use; do not use this in your
applications.
"""
if isinstance(o, unicode):
if isinstance(o, str):
s = self.string_literal(o.encode(self.encoding))
elif isinstance(o, bytearray):
s = self._bytes_literal(o)
elif isinstance(o, bytes):
if PY2:
s = self.string_literal(o)
else:
s = self._bytes_literal(o)
s = self._bytes_literal(o)
elif isinstance(o, (tuple, list)):
s = self._tuple_literal(o)
else:
s = self.escape(o, self.encoders)
if isinstance(s, unicode):
if isinstance(s, str):
s = s.encode(self.encoding)
assert isinstance(s, bytes)
return s
Expand Down
4 changes: 1 addition & 3 deletions MySQLdb/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from MySQLdb._mysql import string_literal, escape
from MySQLdb.constants import FIELD_TYPE, FLAG
from MySQLdb.times import *
from MySQLdb.compat import PY2, long, unicode
from MySQLdb._exceptions import ProgrammingError

NoneType = type(None)
Expand Down Expand Up @@ -85,11 +84,10 @@ def array2Str(o, d):
return Thing2Literal(o.tostring(), d)

# bytes or str regarding to BINARY_FLAG.
_bytes_or_str = ((FLAG.BINARY, bytes), (None, unicode))
_bytes_or_str = ((FLAG.BINARY, bytes), (None, str))

conversions = {
int: Thing2Str,
long: Thing2Str,
float: Float2Str,
NoneType: None2NULL,
ArrayType: array2Str,
Expand Down
16 changes: 7 additions & 9 deletions MySQLdb/cursors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
This module implements Cursors of various types for MySQLdb. By
default, MySQLdb uses the Cursor class.
"""
from __future__ import print_function, absolute_import
from functools import partial
import re
import sys

from .compat import unicode
from ._exceptions import (
Warning, Error, InterfaceError, DataError,
DatabaseError, OperationalError, IntegrityError, InternalError,
Expand Down Expand Up @@ -101,7 +99,7 @@ def _escape_args(self, args, conn):
literal = conn.literal

def ensure_bytes(x):
if isinstance(x, unicode):
if isinstance(x, str):
return x.encode(encoding)
elif isinstance(x, tuple):
return tuple(map(ensure_bytes, x))
Expand Down Expand Up @@ -187,14 +185,14 @@ def execute(self, query, args=None):
pass
db = self._get_db()

if isinstance(query, unicode):
if isinstance(query, str):
query = query.encode(db.encoding)

if args is not None:
if isinstance(args, dict):
nargs = {}
for key, item in args.items():
if isinstance(key, unicode):
if isinstance(key, str):
key = key.encode(db.encoding)
nargs[key] = db.literal(item)
args = nargs
Expand Down Expand Up @@ -242,11 +240,11 @@ def executemany(self, query, args):
def _do_execute_many(self, prefix, values, postfix, args, max_stmt_length, encoding):
conn = self._get_db()
escape = self._escape_args
if isinstance(prefix, unicode):
if isinstance(prefix, str):
prefix = prefix.encode(encoding)
if isinstance(values, unicode):
if isinstance(values, str):
values = values.encode(encoding)
if isinstance(postfix, unicode):
if isinstance(postfix, str):
postfix = postfix.encode(encoding)
sql = bytearray(prefix)
args = iter(args)
Expand Down Expand Up @@ -294,7 +292,7 @@ def callproc(self, procname, args=()):
disconnected.
"""
db = self._get_db()
if isinstance(procname, unicode):
if isinstance(procname, str):
procname = procname.encode(db.encoding)
if args:
fmt = b'@_' + procname + b'_%d=%s'
Expand Down
3 changes: 0 additions & 3 deletions metadata.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ classifiers:
Operating System :: Unix
Programming Language :: C
Programming Language :: Python
Programming Language :: Python :: 2
Programming Language :: Python :: 2.7
Programming Language :: Python :: 3
Programming Language :: Python :: 3.5
Programming Language :: Python :: 3.6
Expand All @@ -30,7 +28,6 @@ classifiers:
Topic :: Database :: Database Engines/Servers
py_modules:
MySQLdb._exceptions
MySQLdb.compat
MySQLdb.connections
MySQLdb.converters
MySQLdb.cursors
Expand Down
6 changes: 2 additions & 4 deletions tests/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import unittest
from configdb import connection_factory

from MySQLdb.compat import unichr


class DatabaseTest(unittest.TestCase):

Expand All @@ -27,8 +25,8 @@ def setUp(self):
db = connection_factory(**self.connect_kwargs)
self.connection = db
self.cursor = db.cursor()
self.BLOBUText = u''.join([unichr(i) for i in range(16384)])
self.BLOBBinary = self.db_module.Binary((u''.join([unichr(i) for i in range(256)] * 16)).encode('latin1'))
self.BLOBUText = u''.join([chr(i) for i in range(16384)])
self.BLOBBinary = self.db_module.Binary((u''.join([chr(i) for i in range(256)] * 16)).encode('latin1'))

leak_test = True

Expand Down
1 change: 0 additions & 1 deletion tests/test_MySQLdb_capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from contextlib import closing
import unittest
import MySQLdb
from MySQLdb.compat import unicode
from MySQLdb import cursors
from configdb import connection_factory
import warnings
Expand Down
4 changes: 2 additions & 2 deletions tests/test_MySQLdb_nonstandard.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ def test_client_flag(self):
use_unicode=True,
client_flag=MySQLdb.constants.CLIENT.FOUND_ROWS)

self.assertIsInstance(conn.client_flag, (int, MySQLdb.compat.long))
self.assertIsInstance(conn.client_flag, int)
self.assertTrue(conn.client_flag & MySQLdb.constants.CLIENT.FOUND_ROWS)
with self.assertRaises(TypeError if MySQLdb.compat.PY2 else AttributeError):
with self.assertRaises(AttributeError):
conn.client_flag = 0

conn.close()
Expand Down