Skip to content

Commit 026e87f

Browse files
Djaillamethane
authored andcommitted
Remove py27 support from Python sources. (#394)
1 parent 5811678 commit 026e87f

11 files changed

+23
-65
lines changed

INSTALL.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ MySQLdb Installation
88
Prerequisites
99
-------------
1010

11-
+ Python 2.7, 3.5 or higher
11+
+ Python 3.5 or higher
1212

1313
+ setuptools
1414

MySQLdb/__init__.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
paramstyle = "format"
2727

2828
from ._mysql import *
29-
from MySQLdb.compat import PY2
3029
from MySQLdb.constants import FIELD_TYPE
3130
from MySQLdb.times import Date, Time, Timestamp, \
3231
DateFromTicks, TimeFromTicks, TimestampFromTicks
@@ -71,12 +70,8 @@ def test_DBAPISet_set_equality_membership():
7170
def test_DBAPISet_set_inequality_membership():
7271
assert FIELD_TYPE.DATE != STRING
7372

74-
if PY2:
75-
def Binary(x):
76-
return bytearray(x)
77-
else:
78-
def Binary(x):
79-
return bytes(x)
73+
def Binary(x):
74+
return bytes(x)
8075

8176
def Connect(*args, **kwargs):
8277
"""Factory function for connections.Connection."""

MySQLdb/_exceptions.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
55
https://www.python.org/dev/peps/pep-0249/
66
"""
7-
from .compat import StandardError
87

9-
10-
class MySQLError(StandardError):
8+
class MySQLError(Exception):
119
"""Exception related to operation with MySQL."""
1210

1311

MySQLdb/compat.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

MySQLdb/connections.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import sys
99

1010
from MySQLdb import cursors, _mysql
11-
from MySQLdb.compat import unicode, PY2
1211
from MySQLdb._exceptions import (
1312
Warning, Error, InterfaceError, DataError,
1413
DatabaseError, OperationalError, IntegrityError, InternalError,
@@ -85,14 +84,11 @@ class object, used to create cursors (keyword only)
8584
columns are returned as bytes. Unicode objects will always
8685
be encoded to the connection's character set regardless of
8786
this setting.
88-
Default to False on Python 2 and True on Python 3
89-
so that you can always get python `str` object by default.
87+
Default to True.
9088
9189
:param str charset:
9290
If supplied, the connection character set will be changed
9391
to this character set.
94-
On Python 2, this option changes default value of `use_unicode`
95-
option from False to True.
9692
9793
:param str auth_plugin:
9894
If supplied, the connection default authentication plugin will be
@@ -154,13 +150,7 @@ class object, used to create cursors (keyword only)
154150

155151
cursorclass = kwargs2.pop('cursorclass', self.default_cursor)
156152
charset = kwargs2.get('charset', '')
157-
158-
if charset or not PY2:
159-
use_unicode = True
160-
else:
161-
use_unicode = False
162-
163-
use_unicode = kwargs2.pop('use_unicode', use_unicode)
153+
use_unicode = kwargs2.pop('use_unicode', True)
164154
sql_mode = kwargs2.pop('sql_mode', '')
165155
self._binary_prefix = kwargs2.pop('binary_prefix', False)
166156

@@ -209,9 +199,9 @@ def unicode_literal(u, dummy=None):
209199
self.converter[t] = _bytes_or_str
210200
# Unlike other string/blob types, JSON is always text.
211201
# MySQL may return JSON with charset==binary.
212-
self.converter[FIELD_TYPE.JSON] = unicode
202+
self.converter[FIELD_TYPE.JSON] = str
213203

214-
self.encoders[unicode] = unicode_literal
204+
self.encoders[str] = unicode_literal
215205
self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS
216206
if self._transactional:
217207
if autocommit is not None:
@@ -256,20 +246,17 @@ def literal(self, o):
256246
Non-standard. For internal use; do not use this in your
257247
applications.
258248
"""
259-
if isinstance(o, unicode):
249+
if isinstance(o, str):
260250
s = self.string_literal(o.encode(self.encoding))
261251
elif isinstance(o, bytearray):
262252
s = self._bytes_literal(o)
263253
elif isinstance(o, bytes):
264-
if PY2:
265-
s = self.string_literal(o)
266-
else:
267-
s = self._bytes_literal(o)
254+
s = self._bytes_literal(o)
268255
elif isinstance(o, (tuple, list)):
269256
s = self._tuple_literal(o)
270257
else:
271258
s = self.escape(o, self.encoders)
272-
if isinstance(s, unicode):
259+
if isinstance(s, str):
273260
s = s.encode(self.encoding)
274261
assert isinstance(s, bytes)
275262
return s

MySQLdb/converters.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from MySQLdb._mysql import string_literal, escape
3636
from MySQLdb.constants import FIELD_TYPE, FLAG
3737
from MySQLdb.times import *
38-
from MySQLdb.compat import PY2, long, unicode
3938
from MySQLdb._exceptions import ProgrammingError
4039

4140
NoneType = type(None)
@@ -85,11 +84,10 @@ def array2Str(o, d):
8584
return Thing2Literal(o.tostring(), d)
8685

8786
# bytes or str regarding to BINARY_FLAG.
88-
_bytes_or_str = ((FLAG.BINARY, bytes), (None, unicode))
87+
_bytes_or_str = ((FLAG.BINARY, bytes), (None, str))
8988

9089
conversions = {
9190
int: Thing2Str,
92-
long: Thing2Str,
9391
float: Float2Str,
9492
NoneType: None2NULL,
9593
ArrayType: array2Str,

MySQLdb/cursors.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
This module implements Cursors of various types for MySQLdb. By
44
default, MySQLdb uses the Cursor class.
55
"""
6-
from __future__ import print_function, absolute_import
76
from functools import partial
87
import re
98
import sys
109

11-
from .compat import unicode
1210
from ._exceptions import (
1311
Warning, Error, InterfaceError, DataError,
1412
DatabaseError, OperationalError, IntegrityError, InternalError,
@@ -101,7 +99,7 @@ def _escape_args(self, args, conn):
10199
literal = conn.literal
102100

103101
def ensure_bytes(x):
104-
if isinstance(x, unicode):
102+
if isinstance(x, str):
105103
return x.encode(encoding)
106104
elif isinstance(x, tuple):
107105
return tuple(map(ensure_bytes, x))
@@ -187,14 +185,14 @@ def execute(self, query, args=None):
187185
pass
188186
db = self._get_db()
189187

190-
if isinstance(query, unicode):
188+
if isinstance(query, str):
191189
query = query.encode(db.encoding)
192190

193191
if args is not None:
194192
if isinstance(args, dict):
195193
nargs = {}
196194
for key, item in args.items():
197-
if isinstance(key, unicode):
195+
if isinstance(key, str):
198196
key = key.encode(db.encoding)
199197
nargs[key] = db.literal(item)
200198
args = nargs
@@ -242,11 +240,11 @@ def executemany(self, query, args):
242240
def _do_execute_many(self, prefix, values, postfix, args, max_stmt_length, encoding):
243241
conn = self._get_db()
244242
escape = self._escape_args
245-
if isinstance(prefix, unicode):
243+
if isinstance(prefix, str):
246244
prefix = prefix.encode(encoding)
247-
if isinstance(values, unicode):
245+
if isinstance(values, str):
248246
values = values.encode(encoding)
249-
if isinstance(postfix, unicode):
247+
if isinstance(postfix, str):
250248
postfix = postfix.encode(encoding)
251249
sql = bytearray(prefix)
252250
args = iter(args)
@@ -294,7 +292,7 @@ def callproc(self, procname, args=()):
294292
disconnected.
295293
"""
296294
db = self._get_db()
297-
if isinstance(procname, unicode):
295+
if isinstance(procname, str):
298296
procname = procname.encode(db.encoding)
299297
if args:
300298
fmt = b'@_' + procname + b'_%d=%s'

metadata.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ classifiers:
2828
Topic :: Database :: Database Engines/Servers
2929
py_modules:
3030
MySQLdb._exceptions
31-
MySQLdb.compat
3231
MySQLdb.connections
3332
MySQLdb.converters
3433
MySQLdb.cursors

tests/capabilities.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import unittest
1111
from configdb import connection_factory
1212

13-
from MySQLdb.compat import unichr
14-
1513

1614
class DatabaseTest(unittest.TestCase):
1715

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

3331
leak_test = True
3432

tests/test_MySQLdb_capabilities.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from contextlib import closing
66
import unittest
77
import MySQLdb
8-
from MySQLdb.compat import unicode
98
from MySQLdb import cursors
109
from configdb import connection_factory
1110
import warnings

tests/test_MySQLdb_nonstandard.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ def test_client_flag(self):
9090
use_unicode=True,
9191
client_flag=MySQLdb.constants.CLIENT.FOUND_ROWS)
9292

93-
self.assertIsInstance(conn.client_flag, (int, MySQLdb.compat.long))
93+
self.assertIsInstance(conn.client_flag, int)
9494
self.assertTrue(conn.client_flag & MySQLdb.constants.CLIENT.FOUND_ROWS)
95-
with self.assertRaises(TypeError if MySQLdb.compat.PY2 else AttributeError):
95+
with self.assertRaises(AttributeError):
9696
conn.client_flag = 0
9797

9898
conn.close()

0 commit comments

Comments
 (0)