Skip to content

Commit 9440a0d

Browse files
committed
Handle types in a Python 3 friendly way
1 parent 182011e commit 9440a0d

5 files changed

Lines changed: 18 additions & 11 deletions

File tree

MySQLdb/compat.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import sys
2+
3+
if sys.version_info[0] == 3:
4+
unicode = str
5+
unichr = chr
6+
else:
7+
unicode = unicode
8+
unichr = unichr

MySQLdb/connections.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
88
"""
99
from MySQLdb import cursors
10+
from MySQLdb.compat import unicode
1011
from _mysql_exceptions import Warning, Error, InterfaceError, DataError, \
1112
DatabaseError, OperationalError, IntegrityError, InternalError, \
1213
NotSupportedError, ProgrammingError
13-
import types, _mysql
14+
import _mysql
1415
import re
1516

1617

@@ -233,8 +234,8 @@ def string_decoder(s):
233234
self.converter[FIELD_TYPE.VARCHAR].append((None, string_decoder))
234235
self.converter[FIELD_TYPE.BLOB].append((None, string_decoder))
235236

236-
self.encoders[types.StringType] = string_literal
237-
self.encoders[types.UnicodeType] = unicode_literal
237+
self.encoders[bytes] = string_literal
238+
self.encoders[unicode] = unicode_literal
238239
self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS
239240
if self._transactional:
240241
if autocommit is not None:

MySQLdb/cursors.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@
77

88
import re
99
import sys
10-
try:
11-
from types import ListType, TupleType, UnicodeType
12-
except ImportError:
13-
# Python 3
14-
ListType = list
15-
TupleType = tuple
16-
UnicodeType = str
10+
11+
from MySQLdb.compat import unicode
1712

1813
restr = r"""
1914
\s
@@ -305,7 +300,7 @@ def callproc(self, procname, args=()):
305300
q = "CALL %s(%s)" % (procname,
306301
','.join(['@_%s_%d' % (procname, i)
307302
for i in range(len(args))]))
308-
if type(q) is UnicodeType:
303+
if isinstance(q, unicode):
309304
q = q.encode(db.unicode_literal.charset)
310305
self._query(q)
311306
self._executed = q

metadata.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ classifiers:
4444
Topic :: Database :: Database Engines/Servers
4545
py_modules:
4646
_mysql_exceptions
47+
MySQLdb.compat
4748
MySQLdb.converters
4849
MySQLdb.connections
4950
MySQLdb.cursors

tests/capabilities.py

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

13+
from MySQLdb.compat import unichr
14+
1315

1416
class DatabaseTest(unittest.TestCase):
1517

0 commit comments

Comments
 (0)