Handle types in a Python 3 friendly way

This commit is contained in:
Marc Abramowitz
2014-04-16 14:13:06 -07:00
parent 182011eb18
commit 9440a0d4df
5 changed files with 18 additions and 11 deletions

8
MySQLdb/compat.py Normal file
View File

@@ -0,0 +1,8 @@
import sys
if sys.version_info[0] == 3:
unicode = str
unichr = chr
else:
unicode = unicode
unichr = unichr

View File

@@ -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:

View File

@@ -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

View File

@@ -44,6 +44,7 @@ classifiers:
Topic :: Database :: Database Engines/Servers
py_modules:
_mysql_exceptions
MySQLdb.compat
MySQLdb.converters
MySQLdb.connections
MySQLdb.cursors

View File

@@ -10,6 +10,8 @@ import array
import unittest
from configdb import connection_factory
from MySQLdb.compat import unichr
class DatabaseTest(unittest.TestCase):