mirror of
https://github.com/PyMySQL/mysqlclient.git
synced 2025-08-14 18:12:35 +08:00
Remove py27 support from Python sources. (#394)
This commit is contained in:

committed by
Inada Naoki

parent
5811678dea
commit
026e87f7b5
@ -8,7 +8,7 @@ MySQLdb Installation
|
|||||||
Prerequisites
|
Prerequisites
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
+ Python 2.7, 3.5 or higher
|
+ Python 3.5 or higher
|
||||||
|
|
||||||
+ setuptools
|
+ setuptools
|
||||||
|
|
||||||
|
@ -26,7 +26,6 @@ apilevel = "2.0"
|
|||||||
paramstyle = "format"
|
paramstyle = "format"
|
||||||
|
|
||||||
from ._mysql import *
|
from ._mysql import *
|
||||||
from MySQLdb.compat import PY2
|
|
||||||
from MySQLdb.constants import FIELD_TYPE
|
from MySQLdb.constants import FIELD_TYPE
|
||||||
from MySQLdb.times import Date, Time, Timestamp, \
|
from MySQLdb.times import Date, Time, Timestamp, \
|
||||||
DateFromTicks, TimeFromTicks, TimestampFromTicks
|
DateFromTicks, TimeFromTicks, TimestampFromTicks
|
||||||
@ -71,12 +70,8 @@ def test_DBAPISet_set_equality_membership():
|
|||||||
def test_DBAPISet_set_inequality_membership():
|
def test_DBAPISet_set_inequality_membership():
|
||||||
assert FIELD_TYPE.DATE != STRING
|
assert FIELD_TYPE.DATE != STRING
|
||||||
|
|
||||||
if PY2:
|
def Binary(x):
|
||||||
def Binary(x):
|
return bytes(x)
|
||||||
return bytearray(x)
|
|
||||||
else:
|
|
||||||
def Binary(x):
|
|
||||||
return bytes(x)
|
|
||||||
|
|
||||||
def Connect(*args, **kwargs):
|
def Connect(*args, **kwargs):
|
||||||
"""Factory function for connections.Connection."""
|
"""Factory function for connections.Connection."""
|
||||||
|
@ -4,10 +4,8 @@ These classes are dictated by the DB API v2.0:
|
|||||||
|
|
||||||
https://www.python.org/dev/peps/pep-0249/
|
https://www.python.org/dev/peps/pep-0249/
|
||||||
"""
|
"""
|
||||||
from .compat import StandardError
|
|
||||||
|
|
||||||
|
class MySQLError(Exception):
|
||||||
class MySQLError(StandardError):
|
|
||||||
"""Exception related to operation with MySQL."""
|
"""Exception related to operation with MySQL."""
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
import sys
|
|
||||||
|
|
||||||
if sys.version_info[0] == 2:
|
|
||||||
PY2 = True
|
|
||||||
unicode = unicode
|
|
||||||
unichr = unichr
|
|
||||||
long = long
|
|
||||||
StandardError = StandardError
|
|
||||||
else:
|
|
||||||
PY2 = False
|
|
||||||
unicode = str
|
|
||||||
unichr = chr
|
|
||||||
long = int
|
|
||||||
StandardError = Exception
|
|
@ -8,7 +8,6 @@ import re
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from MySQLdb import cursors, _mysql
|
from MySQLdb import cursors, _mysql
|
||||||
from MySQLdb.compat import unicode, PY2
|
|
||||||
from MySQLdb._exceptions import (
|
from MySQLdb._exceptions import (
|
||||||
Warning, Error, InterfaceError, DataError,
|
Warning, Error, InterfaceError, DataError,
|
||||||
DatabaseError, OperationalError, IntegrityError, InternalError,
|
DatabaseError, OperationalError, IntegrityError, InternalError,
|
||||||
@ -85,14 +84,11 @@ class Connection(_mysql.connection):
|
|||||||
columns are returned as bytes. Unicode objects will always
|
columns are returned as bytes. Unicode objects will always
|
||||||
be encoded to the connection's character set regardless of
|
be encoded to the connection's character set regardless of
|
||||||
this setting.
|
this setting.
|
||||||
Default to False on Python 2 and True on Python 3
|
Default to True.
|
||||||
so that you can always get python `str` object by default.
|
|
||||||
|
|
||||||
:param str charset:
|
:param str charset:
|
||||||
If supplied, the connection character set will be changed
|
If supplied, the connection character set will be changed
|
||||||
to this character set.
|
to this character set.
|
||||||
On Python 2, this option changes default value of `use_unicode`
|
|
||||||
option from False to True.
|
|
||||||
|
|
||||||
:param str auth_plugin:
|
:param str auth_plugin:
|
||||||
If supplied, the connection default authentication plugin will be
|
If supplied, the connection default authentication plugin will be
|
||||||
@ -154,13 +150,7 @@ class Connection(_mysql.connection):
|
|||||||
|
|
||||||
cursorclass = kwargs2.pop('cursorclass', self.default_cursor)
|
cursorclass = kwargs2.pop('cursorclass', self.default_cursor)
|
||||||
charset = kwargs2.get('charset', '')
|
charset = kwargs2.get('charset', '')
|
||||||
|
use_unicode = kwargs2.pop('use_unicode', True)
|
||||||
if charset or not PY2:
|
|
||||||
use_unicode = True
|
|
||||||
else:
|
|
||||||
use_unicode = False
|
|
||||||
|
|
||||||
use_unicode = kwargs2.pop('use_unicode', use_unicode)
|
|
||||||
sql_mode = kwargs2.pop('sql_mode', '')
|
sql_mode = kwargs2.pop('sql_mode', '')
|
||||||
self._binary_prefix = kwargs2.pop('binary_prefix', False)
|
self._binary_prefix = kwargs2.pop('binary_prefix', False)
|
||||||
|
|
||||||
@ -209,9 +199,9 @@ class Connection(_mysql.connection):
|
|||||||
self.converter[t] = _bytes_or_str
|
self.converter[t] = _bytes_or_str
|
||||||
# Unlike other string/blob types, JSON is always text.
|
# Unlike other string/blob types, JSON is always text.
|
||||||
# MySQL may return JSON with charset==binary.
|
# 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
|
self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS
|
||||||
if self._transactional:
|
if self._transactional:
|
||||||
if autocommit is not None:
|
if autocommit is not None:
|
||||||
@ -256,20 +246,17 @@ class Connection(_mysql.connection):
|
|||||||
Non-standard. For internal use; do not use this in your
|
Non-standard. For internal use; do not use this in your
|
||||||
applications.
|
applications.
|
||||||
"""
|
"""
|
||||||
if isinstance(o, unicode):
|
if isinstance(o, str):
|
||||||
s = self.string_literal(o.encode(self.encoding))
|
s = self.string_literal(o.encode(self.encoding))
|
||||||
elif isinstance(o, bytearray):
|
elif isinstance(o, bytearray):
|
||||||
s = self._bytes_literal(o)
|
s = self._bytes_literal(o)
|
||||||
elif isinstance(o, bytes):
|
elif isinstance(o, bytes):
|
||||||
if PY2:
|
s = self._bytes_literal(o)
|
||||||
s = self.string_literal(o)
|
|
||||||
else:
|
|
||||||
s = self._bytes_literal(o)
|
|
||||||
elif isinstance(o, (tuple, list)):
|
elif isinstance(o, (tuple, list)):
|
||||||
s = self._tuple_literal(o)
|
s = self._tuple_literal(o)
|
||||||
else:
|
else:
|
||||||
s = self.escape(o, self.encoders)
|
s = self.escape(o, self.encoders)
|
||||||
if isinstance(s, unicode):
|
if isinstance(s, str):
|
||||||
s = s.encode(self.encoding)
|
s = s.encode(self.encoding)
|
||||||
assert isinstance(s, bytes)
|
assert isinstance(s, bytes)
|
||||||
return s
|
return s
|
||||||
|
@ -35,7 +35,6 @@ from decimal import Decimal
|
|||||||
from MySQLdb._mysql import string_literal, escape
|
from MySQLdb._mysql import string_literal, escape
|
||||||
from MySQLdb.constants import FIELD_TYPE, FLAG
|
from MySQLdb.constants import FIELD_TYPE, FLAG
|
||||||
from MySQLdb.times import *
|
from MySQLdb.times import *
|
||||||
from MySQLdb.compat import PY2, long, unicode
|
|
||||||
from MySQLdb._exceptions import ProgrammingError
|
from MySQLdb._exceptions import ProgrammingError
|
||||||
|
|
||||||
NoneType = type(None)
|
NoneType = type(None)
|
||||||
@ -85,11 +84,10 @@ def array2Str(o, d):
|
|||||||
return Thing2Literal(o.tostring(), d)
|
return Thing2Literal(o.tostring(), d)
|
||||||
|
|
||||||
# bytes or str regarding to BINARY_FLAG.
|
# bytes or str regarding to BINARY_FLAG.
|
||||||
_bytes_or_str = ((FLAG.BINARY, bytes), (None, unicode))
|
_bytes_or_str = ((FLAG.BINARY, bytes), (None, str))
|
||||||
|
|
||||||
conversions = {
|
conversions = {
|
||||||
int: Thing2Str,
|
int: Thing2Str,
|
||||||
long: Thing2Str,
|
|
||||||
float: Float2Str,
|
float: Float2Str,
|
||||||
NoneType: None2NULL,
|
NoneType: None2NULL,
|
||||||
ArrayType: array2Str,
|
ArrayType: array2Str,
|
||||||
|
@ -3,12 +3,10 @@
|
|||||||
This module implements Cursors of various types for MySQLdb. By
|
This module implements Cursors of various types for MySQLdb. By
|
||||||
default, MySQLdb uses the Cursor class.
|
default, MySQLdb uses the Cursor class.
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function, absolute_import
|
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from .compat import unicode
|
|
||||||
from ._exceptions import (
|
from ._exceptions import (
|
||||||
Warning, Error, InterfaceError, DataError,
|
Warning, Error, InterfaceError, DataError,
|
||||||
DatabaseError, OperationalError, IntegrityError, InternalError,
|
DatabaseError, OperationalError, IntegrityError, InternalError,
|
||||||
@ -101,7 +99,7 @@ class BaseCursor(object):
|
|||||||
literal = conn.literal
|
literal = conn.literal
|
||||||
|
|
||||||
def ensure_bytes(x):
|
def ensure_bytes(x):
|
||||||
if isinstance(x, unicode):
|
if isinstance(x, str):
|
||||||
return x.encode(encoding)
|
return x.encode(encoding)
|
||||||
elif isinstance(x, tuple):
|
elif isinstance(x, tuple):
|
||||||
return tuple(map(ensure_bytes, x))
|
return tuple(map(ensure_bytes, x))
|
||||||
@ -187,14 +185,14 @@ class BaseCursor(object):
|
|||||||
pass
|
pass
|
||||||
db = self._get_db()
|
db = self._get_db()
|
||||||
|
|
||||||
if isinstance(query, unicode):
|
if isinstance(query, str):
|
||||||
query = query.encode(db.encoding)
|
query = query.encode(db.encoding)
|
||||||
|
|
||||||
if args is not None:
|
if args is not None:
|
||||||
if isinstance(args, dict):
|
if isinstance(args, dict):
|
||||||
nargs = {}
|
nargs = {}
|
||||||
for key, item in args.items():
|
for key, item in args.items():
|
||||||
if isinstance(key, unicode):
|
if isinstance(key, str):
|
||||||
key = key.encode(db.encoding)
|
key = key.encode(db.encoding)
|
||||||
nargs[key] = db.literal(item)
|
nargs[key] = db.literal(item)
|
||||||
args = nargs
|
args = nargs
|
||||||
@ -242,11 +240,11 @@ class BaseCursor(object):
|
|||||||
def _do_execute_many(self, prefix, values, postfix, args, max_stmt_length, encoding):
|
def _do_execute_many(self, prefix, values, postfix, args, max_stmt_length, encoding):
|
||||||
conn = self._get_db()
|
conn = self._get_db()
|
||||||
escape = self._escape_args
|
escape = self._escape_args
|
||||||
if isinstance(prefix, unicode):
|
if isinstance(prefix, str):
|
||||||
prefix = prefix.encode(encoding)
|
prefix = prefix.encode(encoding)
|
||||||
if isinstance(values, unicode):
|
if isinstance(values, str):
|
||||||
values = values.encode(encoding)
|
values = values.encode(encoding)
|
||||||
if isinstance(postfix, unicode):
|
if isinstance(postfix, str):
|
||||||
postfix = postfix.encode(encoding)
|
postfix = postfix.encode(encoding)
|
||||||
sql = bytearray(prefix)
|
sql = bytearray(prefix)
|
||||||
args = iter(args)
|
args = iter(args)
|
||||||
@ -294,7 +292,7 @@ class BaseCursor(object):
|
|||||||
disconnected.
|
disconnected.
|
||||||
"""
|
"""
|
||||||
db = self._get_db()
|
db = self._get_db()
|
||||||
if isinstance(procname, unicode):
|
if isinstance(procname, str):
|
||||||
procname = procname.encode(db.encoding)
|
procname = procname.encode(db.encoding)
|
||||||
if args:
|
if args:
|
||||||
fmt = b'@_' + procname + b'_%d=%s'
|
fmt = b'@_' + procname + b'_%d=%s'
|
||||||
|
@ -28,7 +28,6 @@ classifiers:
|
|||||||
Topic :: Database :: Database Engines/Servers
|
Topic :: Database :: Database Engines/Servers
|
||||||
py_modules:
|
py_modules:
|
||||||
MySQLdb._exceptions
|
MySQLdb._exceptions
|
||||||
MySQLdb.compat
|
|
||||||
MySQLdb.connections
|
MySQLdb.connections
|
||||||
MySQLdb.converters
|
MySQLdb.converters
|
||||||
MySQLdb.cursors
|
MySQLdb.cursors
|
||||||
|
@ -10,8 +10,6 @@ import array
|
|||||||
import unittest
|
import unittest
|
||||||
from configdb import connection_factory
|
from configdb import connection_factory
|
||||||
|
|
||||||
from MySQLdb.compat import unichr
|
|
||||||
|
|
||||||
|
|
||||||
class DatabaseTest(unittest.TestCase):
|
class DatabaseTest(unittest.TestCase):
|
||||||
|
|
||||||
@ -27,8 +25,8 @@ class DatabaseTest(unittest.TestCase):
|
|||||||
db = connection_factory(**self.connect_kwargs)
|
db = connection_factory(**self.connect_kwargs)
|
||||||
self.connection = db
|
self.connection = db
|
||||||
self.cursor = db.cursor()
|
self.cursor = db.cursor()
|
||||||
self.BLOBUText = u''.join([unichr(i) for i in range(16384)])
|
self.BLOBUText = u''.join([chr(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.BLOBBinary = self.db_module.Binary((u''.join([chr(i) for i in range(256)] * 16)).encode('latin1'))
|
||||||
|
|
||||||
leak_test = True
|
leak_test = True
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ from datetime import timedelta
|
|||||||
from contextlib import closing
|
from contextlib import closing
|
||||||
import unittest
|
import unittest
|
||||||
import MySQLdb
|
import MySQLdb
|
||||||
from MySQLdb.compat import unicode
|
|
||||||
from MySQLdb import cursors
|
from MySQLdb import cursors
|
||||||
from configdb import connection_factory
|
from configdb import connection_factory
|
||||||
import warnings
|
import warnings
|
||||||
|
@ -90,9 +90,9 @@ class CoreAPI(unittest.TestCase):
|
|||||||
use_unicode=True,
|
use_unicode=True,
|
||||||
client_flag=MySQLdb.constants.CLIENT.FOUND_ROWS)
|
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)
|
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.client_flag = 0
|
||||||
|
|
||||||
conn.close()
|
conn.close()
|
||||||
|
Reference in New Issue
Block a user