diff --git a/MySQLdb/compat.py b/MySQLdb/compat.py new file mode 100644 index 0000000..4d84afe --- /dev/null +++ b/MySQLdb/compat.py @@ -0,0 +1,8 @@ +import sys + +if sys.version_info[0] == 3: + unicode = str + unichr = chr +else: + unicode = unicode + unichr = unichr diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 13e55c4..a7adc99 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -7,10 +7,11 @@ override Connection.default_cursor with a non-standard Cursor class. """ from MySQLdb import cursors +from MySQLdb.compat import unicode from _mysql_exceptions import Warning, Error, InterfaceError, DataError, \ DatabaseError, OperationalError, IntegrityError, InternalError, \ NotSupportedError, ProgrammingError -import types, _mysql +import _mysql import re @@ -233,8 +234,8 @@ class Connection(_mysql.connection): self.converter[FIELD_TYPE.VARCHAR].append((None, string_decoder)) self.converter[FIELD_TYPE.BLOB].append((None, string_decoder)) - self.encoders[types.StringType] = string_literal - self.encoders[types.UnicodeType] = unicode_literal + self.encoders[bytes] = string_literal + self.encoders[unicode] = unicode_literal self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS if self._transactional: if autocommit is not None: diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index b45ddaf..c19f256 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -7,13 +7,8 @@ default, MySQLdb uses the Cursor class. import re import sys -try: - from types import ListType, TupleType, UnicodeType -except ImportError: - # Python 3 - ListType = list - TupleType = tuple - UnicodeType = str + +from MySQLdb.compat import unicode restr = r""" \s @@ -305,7 +300,7 @@ class BaseCursor(object): q = "CALL %s(%s)" % (procname, ','.join(['@_%s_%d' % (procname, i) for i in range(len(args))])) - if type(q) is UnicodeType: + if isinstance(q, unicode): q = q.encode(db.unicode_literal.charset) self._query(q) self._executed = q diff --git a/metadata.cfg b/metadata.cfg index 49841c1..ea31c01 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -44,6 +44,7 @@ classifiers: Topic :: Database :: Database Engines/Servers py_modules: _mysql_exceptions + MySQLdb.compat MySQLdb.converters MySQLdb.connections MySQLdb.cursors diff --git a/tests/capabilities.py b/tests/capabilities.py index 076361c..5b122cb 100644 --- a/tests/capabilities.py +++ b/tests/capabilities.py @@ -10,6 +10,8 @@ import array import unittest from configdb import connection_factory +from MySQLdb.compat import unichr + class DatabaseTest(unittest.TestCase):