From d16ced645f345017cbdfe789a4a69a6d3420869c Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 4 Jan 2017 21:34:02 +0900 Subject: [PATCH 001/177] Add test for PyMySQL/mysqlclient-python#137 --- tests/test__mysql.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 tests/test__mysql.py diff --git a/tests/test__mysql.py b/tests/test__mysql.py new file mode 100644 index 00000000..e2cb6088 --- /dev/null +++ b/tests/test__mysql.py @@ -0,0 +1,7 @@ +import pytest +import _mysql + + +def test_result_type(): + with pytest.raises(TypeError): + _mysql.result(b"xyz") From 10c6f9f767b5dca2b919285233ee9c12e84fea0b Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 4 Jan 2017 21:37:24 +0900 Subject: [PATCH 002/177] Add typecheck for _mysql.result() fixes #137 --- _mysql.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_mysql.c b/_mysql.c index 6917f4e7..e349e779 100644 --- a/_mysql.c +++ b/_mysql.c @@ -378,8 +378,8 @@ _mysql_ResultObject_Initialize( int n, i; MYSQL_FIELD *fields; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iO", kwlist, - &conn, &use, &conv)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|iO", kwlist, + &_mysql_ConnectionObject_Type, &conn, &use, &conv)) return -1; if (!conv) { if (!(conv = PyDict_New())) From 2feb5ed6850a3905edf0333e0cd11ea6218f0f4f Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 4 Jan 2017 21:43:42 +0900 Subject: [PATCH 003/177] reduce IS_PY3K usage --- _mysql.c | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/_mysql.c b/_mysql.c index e349e779..c3ebc16e 100644 --- a/_mysql.c +++ b/_mysql.c @@ -51,11 +51,7 @@ PERFORMANCE OF THIS SOFTWARE. #include "errmsg.h" #define MyAlloc(s,t) (s *) t.tp_alloc(&t,0) -#ifdef IS_PY3K -# define MyFree(o) Py_TYPE(o)->tp_free((PyObject*)o) -#else -# define MyFree(ob) ob->ob_type->tp_free((PyObject *)ob) -#endif +#define MyFree(o) Py_TYPE(o)->tp_free((PyObject*)o) static PyObject *_mysql_MySQLError; static PyObject *_mysql_Warning; @@ -2643,12 +2639,7 @@ _mysql_ResultObject_setattro( } PyTypeObject _mysql_ConnectionObject_Type = { -#ifdef IS_PY3K PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, -#endif "_mysql.connection", /* (char *)tp_name For printing */ sizeof(_mysql_ConnectionObject), 0, @@ -2715,12 +2706,7 @@ PyTypeObject _mysql_ConnectionObject_Type = { } ; PyTypeObject _mysql_ResultObject_Type = { -#ifdef IS_PY3K PyVarObject_HEAD_INIT(NULL, 0) -#else - PyObject_HEAD_INIT(NULL) - 0, -#endif "_mysql.result", sizeof(_mysql_ResultObject), 0, From 50a81b17835e66c7fb573a177fe0dcfa46eba851 Mon Sep 17 00:00:00 2001 From: Ardi Nusawan Date: Thu, 26 Jan 2017 15:37:19 +0700 Subject: [PATCH 004/177] adding requirement for Mac OS (#143) using homebrew --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 429770f8..60ec4ea5 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ On Windows, there are binary wheel you can install without MySQLConnector/C or M `sudo yum install python3-devel ` # Red Hat / CentOS +`brew install mysql-connector-c` # macOS (Homebrew) ### Install from PyPI From cba486e0438c2bbad9802667c7a1db0efc591b04 Mon Sep 17 00:00:00 2001 From: Vilnis Termanis Date: Fri, 10 Feb 2017 11:36:41 +0000 Subject: [PATCH 005/177] Use _binary prefix for bytes/bytearray parameters (#140) - Based on #106 but now disabled by default - Can be enabled via 'binary_prefix' connection parameter - Added unit tests to verify behaviour --- MySQLdb/__init__.py | 9 +++++++-- MySQLdb/connections.py | 24 ++++++++++++++++++++---- tests/test_MySQLdb_capabilities.py | 19 +++++++++++++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/MySQLdb/__init__.py b/MySQLdb/__init__.py index fc414810..9dc8dabc 100644 --- a/MySQLdb/__init__.py +++ b/MySQLdb/__init__.py @@ -27,6 +27,7 @@ paramstyle = "format" from _mysql import * +from MySQLdb.compat import PY2 from MySQLdb.constants import FIELD_TYPE from MySQLdb.times import Date, Time, Timestamp, \ DateFromTicks, TimeFromTicks, TimestampFromTicks @@ -72,8 +73,12 @@ def test_DBAPISet_set_equality_membership(): def test_DBAPISet_set_inequality_membership(): assert FIELD_TYPE.DATE != STRING -def Binary(x): - return bytes(x) +if PY2: + def Binary(x): + return bytearray(x) +else: + def Binary(x): + return bytes(x) def Connect(*args, **kwargs): """Factory function for connections.Connection.""" diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index d8406db8..8375041c 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -137,6 +137,10 @@ class object, used to create cursors (keyword only) If True, autocommit is enabled. If None, autocommit isn't set and server default is used. + :param bool binary_prefix: + If set, the '_binary' prefix will be used for raw byte query + arguments (e.g. Binary). This is disabled by default. + There are a number of undocumented, non-standard methods. See the documentation for the MySQL C API for some hints on what they do. """ @@ -174,6 +178,7 @@ class object, used to create cursors (keyword only) use_unicode = kwargs2.pop('use_unicode', use_unicode) sql_mode = kwargs2.pop('sql_mode', '') + binary_prefix = kwargs2.pop('binary_prefix', False) client_flag = kwargs.get('client_flag', 0) client_version = tuple([ numeric_part(n) for n in _mysql.get_client_info().split('.')[:2] ]) @@ -197,7 +202,7 @@ class object, used to create cursors (keyword only) db = proxy(self) def _get_string_literal(): - # Note: string_literal() is called for bytes object on Python 3. + # Note: string_literal() is called for bytes object on Python 3 (via bytes_literal) def string_literal(obj, dummy=None): return db.string_literal(obj) return string_literal @@ -206,13 +211,18 @@ def _get_unicode_literal(): if PY2: # unicode_literal is called for only unicode object. def unicode_literal(u, dummy=None): - return db.literal(u.encode(unicode_literal.charset)) + return db.string_literal(u.encode(unicode_literal.charset)) else: # unicode_literal() is called for arbitrary object. def unicode_literal(u, dummy=None): - return db.literal(str(u).encode(unicode_literal.charset)) + return db.string_literal(str(u).encode(unicode_literal.charset)) return unicode_literal + def _get_bytes_literal(): + def bytes_literal(obj, dummy=None): + return b'_binary' + db.string_literal(obj) + return bytes_literal + def _get_string_decoder(): def string_decoder(s): return s.decode(string_decoder.charset) @@ -220,6 +230,7 @@ def string_decoder(s): string_literal = _get_string_literal() self.unicode_literal = unicode_literal = _get_unicode_literal() + bytes_literal = _get_bytes_literal() self.string_decoder = string_decoder = _get_string_decoder() if not charset: charset = self.character_set_name() @@ -234,7 +245,12 @@ def string_decoder(s): self.converter[FIELD_TYPE.VARCHAR].append((None, string_decoder)) self.converter[FIELD_TYPE.BLOB].append((None, string_decoder)) - self.encoders[bytes] = string_literal + if binary_prefix: + self.encoders[bytes] = string_literal if PY2 else bytes_literal + self.encoders[bytearray] = bytes_literal + else: + self.encoders[bytes] = string_literal + self.encoders[unicode] = unicode_literal self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS if self._transactional: diff --git a/tests/test_MySQLdb_capabilities.py b/tests/test_MySQLdb_capabilities.py index 6f1fe27d..f1887575 100644 --- a/tests/test_MySQLdb_capabilities.py +++ b/tests/test_MySQLdb_capabilities.py @@ -2,10 +2,12 @@ # -*- coding: utf-8 -*- import capabilities from datetime import timedelta +from contextlib import closing import unittest import MySQLdb from MySQLdb.compat import unicode from MySQLdb import cursors +from configdb import connection_factory import warnings @@ -180,6 +182,23 @@ def test_warning_propagation(self): finally: c.close() + def test_binary_prefix(self): + # verify prefix behaviour when enabled, disabled and for default (disabled) + for binary_prefix in (True, False, None): + kwargs = self.connect_kwargs.copy() + # needs to be set to can guarantee CHARSET response for normal strings + kwargs['charset'] = 'utf8' + if binary_prefix != None: + kwargs['binary_prefix'] = binary_prefix + + with closing(connection_factory(**kwargs)) as conn: + with closing(conn.cursor()) as c: + c.execute('SELECT CHARSET(%s)', (MySQLdb.Binary(b'raw bytes'),)) + self.assertEqual(c.fetchall()[0][0], 'binary' if binary_prefix else 'utf8') + # normal strings should not get prefix + c.execute('SELECT CHARSET(%s)', ('str',)) + self.assertEqual(c.fetchall()[0][0], 'utf8') + if __name__ == '__main__': if test_MySQLdb.leak_test: From 97e18a1ea19180249fc38a50c556060fb437bf0f Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sun, 19 Feb 2017 20:11:49 +0900 Subject: [PATCH 006/177] stop using tox. --- .travis.yml | 34 ++++++++++++++++------------------ tox.ini | 12 ------------ 2 files changed, 16 insertions(+), 30 deletions(-) delete mode 100644 tox.ini diff --git a/.travis.yml b/.travis.yml index cb7f09d5..6600eeeb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,35 +1,33 @@ sudo: false +dist: trusty language: python -python: "3.5" +python: + - "nightly" + - "pypy-5.3.1" + - "3.6-dev" + - "3.6" + - "3.5" + - "3.4" + - "2.7" cache: pip -install: - - pip install tox - -matrix: - include: - - python: "3.6-dev" - env: - - TOX_ENV=py36 - - TESTDB=travis.cnf +install: + - pip install -U pip + - pip install -U mock coverage pytest pytest-cov env: - matrix: - - TOX_ENV=py26 - - TOX_ENV=py27 - - TOX_ENV=pypy - - TOX_ENV=py33 - - TOX_ENV=py34 - - TOX_ENV=py35 global: - TESTDB=travis.cnf + before_script: - "mysql --help" - "mysql --print-defaults" - "mysql -e 'create database mysqldb_test charset utf8mb4;'" -script: tox -e $TOX_ENV +script: + - pip install -e . + - py.test --cov ./MySQLdb # vim: sw=2 ts=2 sts=2 diff --git a/tox.ini b/tox.ini deleted file mode 100644 index 4c7fe5fd..00000000 --- a/tox.ini +++ /dev/null @@ -1,12 +0,0 @@ -[tox] -envlist = py26, py27, pypy, py33, py34, py35 - -[testenv] -passenv = TESTDB -commands = - py.test --cov {envsitepackagesdir}/MySQLdb -deps = - mock - coverage==3.7.1 - pytest - pytest-cov From cbc991ec2c08489ce7f58bc40fb98ace6f1f6d22 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sun, 19 Feb 2017 20:40:16 +0900 Subject: [PATCH 007/177] travis: fix erros * set mysql password * add mysql to services --- .travis.yml | 3 +++ tests/travis.cnf | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 6600eeeb..daeab25b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,9 @@ python: cache: pip +services: + - mysql + install: - pip install -U pip - pip install -U mock coverage pytest pytest-cov diff --git a/tests/travis.cnf b/tests/travis.cnf index 483b76f2..b03dfa50 100644 --- a/tests/travis.cnf +++ b/tests/travis.cnf @@ -7,5 +7,5 @@ host = 127.0.0.1 port = 3306 user = root database = mysqldb_test -#password = +password = travis default-character-set = utf8mb4 From 2c0485f2465e60ebcc69395eb7ef138e6eae979e Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sun, 19 Feb 2017 20:47:03 +0900 Subject: [PATCH 008/177] travis: use sudo-enabled trusty for now Random fail happens on container based image. --- .travis.yml | 2 +- tests/travis.cnf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index daeab25b..84f39ccb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ -sudo: false +sudo: required dist: trusty language: python python: diff --git a/tests/travis.cnf b/tests/travis.cnf index b03dfa50..05ff8039 100644 --- a/tests/travis.cnf +++ b/tests/travis.cnf @@ -7,5 +7,5 @@ host = 127.0.0.1 port = 3306 user = root database = mysqldb_test -password = travis +#password = travis default-character-set = utf8mb4 From d587e81a7f09e1a0baf66ba3a16e7a7c0a05455c Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sun, 19 Feb 2017 20:59:39 +0900 Subject: [PATCH 009/177] codecov --- .travis.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 84f39ccb..575f7e40 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,7 @@ services: install: - pip install -U pip - - pip install -U mock coverage pytest pytest-cov + - pip install -U mock coverage pytest pytest-cov codecov env: global: @@ -32,5 +32,8 @@ script: - pip install -e . - py.test --cov ./MySQLdb +after_succes: + - codecov + # vim: sw=2 ts=2 sts=2 From 118861f8cbe0463ea33f8b63cdf9d6ff41210e51 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 20 Feb 2017 16:47:56 +0900 Subject: [PATCH 010/177] deprecate Connection.__enter__ --- MySQLdb/connections.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 8375041c..d2a854f4 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -265,12 +265,10 @@ def autocommit(self, on): def cursor(self, cursorclass=None): """ - Create a cursor on which queries may be performed. The optional cursorclass parameter is used to create the Cursor. By default, self.cursorclass=cursors.Cursor is used. - """ return (cursorclass or self.cursorclass)(self) @@ -286,6 +284,9 @@ def query(self, query): _mysql.connection.query(self, query) def __enter__(self): + from warnings import warn + warn("context interface will be changed. Use explicit conn.commit() or conn.rollback().", + DeprecationWarning, 2) if self.get_autocommit(): self.query("BEGIN") return self.cursor() @@ -319,7 +320,7 @@ def begin(self): DEPRECATED: Will be removed in 1.3. Use an SQL BEGIN statement instead.""" from warnings import warn - warn("begin() is non-standard and will be removed in 1.3", + warn("begin() is non-standard and will be removed in 1.4", DeprecationWarning, 2) self.query("BEGIN") @@ -386,3 +387,5 @@ def show_warnings(self): NotSupportedError = NotSupportedError errorhandler = defaulterrorhandler + +# vim: colorcolumn=100 From 8fcd2c5b7d743db4e8c08a9f6e635e615d2cd08d Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 20 Feb 2017 17:04:48 +0900 Subject: [PATCH 011/177] Use surrogateescape normally on Python 3.6 --- MySQLdb/connections.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index d2a854f4..1511024f 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -4,21 +4,29 @@ want to make your own subclasses. In most cases, you will probably override Connection.default_cursor with a non-standard Cursor class. """ +import re +import sys + from MySQLdb import cursors from MySQLdb.compat import unicode, PY2 -from _mysql_exceptions import Warning, Error, InterfaceError, DataError, \ - DatabaseError, OperationalError, IntegrityError, InternalError, \ - NotSupportedError, ProgrammingError +from _mysql_exceptions import ( + Warning, Error, InterfaceError, DataError, + DatabaseError, OperationalError, IntegrityError, InternalError, + NotSupportedError, ProgrammingError, +) import _mysql -import re if not PY2: - # See http://bugs.python.org/issue24870 - _surrogateescape_table = [chr(i) if i < 0x80 else chr(i + 0xdc00) for i in range(256)] + if sys.version_info[:2] < (3, 6): + # See http://bugs.python.org/issue24870 + _surrogateescape_table = [chr(i) if i < 0x80 else chr(i + 0xdc00) for i in range(256)] - def _fast_surroundescape(s): - return s.decode('latin1').translate(_surrogateescape_table) + def _fast_surrogateescape(s): + return s.decode('latin1').translate(_surrogateescape_table) + else: + def _fast_surrogateescape(s): + return s.decode('ascii', 'surrogateescape') def defaulterrorhandler(connection, cursor, errorclass, errorvalue): @@ -312,7 +320,7 @@ def literal(self, o): # bytes to unicode and back again. # See http://python.org/dev/peps/pep-0383/ if not PY2 and isinstance(s, (bytes, bytearray)): - return _fast_surroundescape(s) + return _fast_surrogateescape(s) return s def begin(self): From 682dfa2b353323c77366bb8d0f1c90ca24e66e7e Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 20 Feb 2017 17:18:59 +0900 Subject: [PATCH 012/177] Add HISTORY for Python 1.3.10 --- HISTORY | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/HISTORY b/HISTORY index a420fd91..861e28e2 100644 --- a/HISTORY +++ b/HISTORY @@ -1,3 +1,17 @@ +====================== + What's new in 1.3.10 +====================== + +Added `binary_prefix` option (disabled by default) to support +`_binary` prefix again. (#134) + +Fix SEGV of `_mysql.result()` when argument's type is unexpected. (#138) + +Deprecate context interface of Connection object. (#149) + +Don't use workaround of `bytes.decode('ascii', 'surrogateescape')` on Python 3.6+. (#150) + + ===================== What's new in 1.3.9 ===================== From c56146d137b2c9d55f9d15a5e3b502dd5a8457a1 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 20 Feb 2017 17:20:34 +0900 Subject: [PATCH 013/177] Add suffix to HISTORY and INSTALL --- HISTORY => HISTORY.md | 0 INSTALL => INSTALL.rst | 0 MANIFEST.in | 26 +++++++++++++------------- 3 files changed, 13 insertions(+), 13 deletions(-) rename HISTORY => HISTORY.md (100%) rename INSTALL => INSTALL.rst (100%) diff --git a/HISTORY b/HISTORY.md similarity index 100% rename from HISTORY rename to HISTORY.md diff --git a/INSTALL b/INSTALL.rst similarity index 100% rename from INSTALL rename to INSTALL.rst diff --git a/MANIFEST.in b/MANIFEST.in index 4e03cc43..b6bc8b68 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,13 +1,13 @@ -recursive-include doc *.rst -recursive-include tests *.py -include doc/conf.py -include MANIFEST.in -include HISTORY -include INSTALL -include README.md -include GPL-2.0 -include metadata.cfg -include site.cfg -include setup_common.py -include setup_posix.py -include setup_windows.py +recursive-include doc *.rst +recursive-include tests *.py +include doc/conf.py +include MANIFEST.in +include HISTORY.md +include INSTALL.rst +include README.md +include GPL-2.0 +include metadata.cfg +include site.cfg +include setup_common.py +include setup_posix.py +include setup_windows.py From 31337c4b63976d28299e1602664afa984cb1b473 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 20 Feb 2017 17:44:12 +0900 Subject: [PATCH 014/177] nose is not used now --- setup.cfg | 3 --- 1 file changed, 3 deletions(-) diff --git a/setup.cfg b/setup.cfg index fea65ab0..eb5b5513 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,3 @@ -[test] -test_suite = nose.collector - [build_ext] ## Only uncomment/set these if the default configuration doesn't work ## Also see https://docs.python.org/distutils/configfile.html From 948def48bdba6d7f200648d68968afd60532470f Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 20 Feb 2017 17:49:47 +0900 Subject: [PATCH 015/177] 1.3.10 --- metadata.cfg | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/metadata.cfg b/metadata.cfg index 322ba993..b40c91e4 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,14 +1,14 @@ [metadata] -version: 1.3.9 -version_info: (1,3,9,'final',1) +version: 1.3.10 +version_info: (1,3,10,'final',0) description: Python interface to MySQL long_description: ========================= Python interface to MySQL ========================= \n - mysqlclient is a fork of MySQL-python. It adds Python 3.3~ support - and merges some pull requests. + mysqlclient is a fork of MySQL-python. It adds Python 3 support + and fixed many bugs. \n MySQLdb is an interface to the popular MySQL_ database server for Python. The design goals are: @@ -17,8 +17,8 @@ long_description: - Thread-safety - Thread-friendliness (threads will not block each other) \n - MySQL-4.1 through 5.5 and Python-2.7, 3.3-3.5 are currently - supported. PyPy is supported. + MySQL-5.1 through 5.7 and Python 2.7, 3.3+ are currently + supported. PyPy is supported too. \n MySQLdb is `Free Software`_. \n @@ -50,20 +50,20 @@ classifiers: Programming Language :: Python :: 3.3 Programming Language :: Python :: 3.4 Programming Language :: Python :: 3.5 + Programming Language :: Python :: 3.6 Topic :: Database Topic :: Database :: Database Engines/Servers py_modules: _mysql_exceptions MySQLdb.compat - MySQLdb.converters MySQLdb.connections + MySQLdb.converters MySQLdb.cursors MySQLdb.release MySQLdb.times + MySQLdb.constants.CLIENT MySQLdb.constants.CR - MySQLdb.constants.FIELD_TYPE MySQLdb.constants.ER + MySQLdb.constants.FIELD_TYPE MySQLdb.constants.FLAG MySQLdb.constants.REFRESH - MySQLdb.constants.CLIENT - From 2b6123e236bb9112d511e57f40da4133cac4121f Mon Sep 17 00:00:00 2001 From: Guangyang Li Date: Tue, 28 Feb 2017 14:27:16 -0500 Subject: [PATCH 016/177] Allow semicolon at the end of bulk insert query --- MySQLdb/cursors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 9ebf0a14..d5f38285 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -28,7 +28,7 @@ RE_INSERT_VALUES = re.compile( r"\s*((?:INSERT|REPLACE)\s.+\sVALUES?\s+)" + r"(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))" + - r"(\s*(?:ON DUPLICATE.*)?)\Z", + r"(\s*(?:ON DUPLICATE.*)?);?\s*\Z", re.IGNORECASE | re.DOTALL) From bb07f701f5cdb35ebbf36085bfaecd2f7a71e7dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Dor=C5=88=C3=A1k?= Date: Tue, 7 Mar 2017 10:48:28 +0100 Subject: [PATCH 017/177] include LICENSE in source tarball GPL-2.0 was removed in b6601fef9efff4786671bb2ecb99ee40d716d1aa --- MANIFEST.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MANIFEST.in b/MANIFEST.in index b6bc8b68..4f853e82 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -5,7 +5,7 @@ include MANIFEST.in include HISTORY.md include INSTALL.rst include README.md -include GPL-2.0 +include LICENSE include metadata.cfg include site.cfg include setup_common.py From f96cb44ef18007a33d1400ff98ce9b887cd983d3 Mon Sep 17 00:00:00 2001 From: Brian Van Klaveren Date: Sat, 29 Apr 2017 10:24:09 -0700 Subject: [PATCH 018/177] Add NEWDECIMAL to the NUMBER DBAPISet (#167) --- MySQLdb/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MySQLdb/__init__.py b/MySQLdb/__init__.py index 9dc8dabc..038309af 100644 --- a/MySQLdb/__init__.py +++ b/MySQLdb/__init__.py @@ -54,7 +54,7 @@ def __eq__(self, other): FIELD_TYPE.MEDIUM_BLOB, FIELD_TYPE.TINY_BLOB]) NUMBER = DBAPISet([FIELD_TYPE.DECIMAL, FIELD_TYPE.DOUBLE, FIELD_TYPE.FLOAT, FIELD_TYPE.INT24, FIELD_TYPE.LONG, FIELD_TYPE.LONGLONG, - FIELD_TYPE.TINY, FIELD_TYPE.YEAR]) + FIELD_TYPE.TINY, FIELD_TYPE.YEAR, FIELD_TYPE.NEWDECIMAL]) DATE = DBAPISet([FIELD_TYPE.DATE, FIELD_TYPE.NEWDATE]) TIME = DBAPISet([FIELD_TYPE.TIME]) TIMESTAMP = DBAPISet([FIELD_TYPE.TIMESTAMP, FIELD_TYPE.DATETIME]) From 1693848c9f6ca863868d94d63499830f7f4f3a1f Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 7 Jun 2017 15:46:20 +0900 Subject: [PATCH 019/177] Fix compile error with MariaDB 10.2. (#177) Don't touch MYSQL.reconnect directly. --- _mysql.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/_mysql.c b/_mysql.c index c3ebc16e..8295f6ce 100644 --- a/_mysql.c +++ b/_mysql.c @@ -1908,7 +1908,10 @@ _mysql_ConnectionObject_ping( int r, reconnect = -1; if (!PyArg_ParseTuple(args, "|I", &reconnect)) return NULL; check_connection(self); - if ( reconnect != -1 ) self->connection.reconnect = reconnect; + if (reconnect != -1) { + my_bool recon = reconnect; + mysql_options(&self->connection, MYSQL_OPT_RECONNECT, &recon); + } Py_BEGIN_ALLOW_THREADS r = mysql_ping(&(self->connection)); Py_END_ALLOW_THREADS From 354dcb59e286b0db3b108c92760d3ec135a8bc23 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 15 Jun 2017 09:06:46 +0900 Subject: [PATCH 020/177] Allow bulk insert which no space around `VALUES` (#179) --- MySQLdb/cursors.py | 2 +- tests/test_cursor.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index d5f38285..dfbd736b 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -26,7 +26,7 @@ #: executemany only supports simple bulk insert. #: You can use it to load large dataset. RE_INSERT_VALUES = re.compile( - r"\s*((?:INSERT|REPLACE)\s.+\sVALUES?\s+)" + + r"\s*((?:INSERT|REPLACE)\b.+\bVALUES?\s*)" + r"(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))" + r"(\s*(?:ON DUPLICATE.*)?);?\s*\Z", re.IGNORECASE | re.DOTALL) diff --git a/tests/test_cursor.py b/tests/test_cursor.py index bfdcb33a..535afec3 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -49,6 +49,10 @@ def test_executemany(): assert m is not None, 'error parse %(id_name)s' assert m.group(3) == ' ON duplicate update', 'group 3 not ON duplicate update, bug in RE_INSERT_VALUES?' + # https://github.com/PyMySQL/mysqlclient-python/issues/178 + m = MySQLdb.cursors.RE_INSERT_VALUES.match("INSERT INTO bloup(foo, bar)VALUES(%s, %s)") + assert m is not None + # cursor._executed myst bee "insert into test (data) values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)" # list args data = range(10) From f41e651e280a625c0a782789eefd478fb112dea8 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Fri, 16 Jun 2017 16:58:59 +0900 Subject: [PATCH 021/177] fix leak of connection->converter. (#182) --- _mysql.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/_mysql.c b/_mysql.c index 8295f6ce..6c5e07ec 100644 --- a/_mysql.c +++ b/_mysql.c @@ -495,13 +495,10 @@ static int _mysql_ResultObject_traverse( return 0; } -static int _mysql_ResultObject_clear( - _mysql_ResultObject *self) +static int _mysql_ResultObject_clear(_mysql_ResultObject *self) { - Py_XDECREF(self->converter); - self->converter = NULL; - Py_XDECREF(self->conn); - self->conn = NULL; + Py_CLEAR(self->converter); + Py_CLEAR(self->conn); return 0; } @@ -796,8 +793,7 @@ _mysql_ConnectionObject_close( return NULL; } _mysql_ConnectionObject_clear(self); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static char _mysql_ConnectionObject_affected_rows__doc__ [] = @@ -2162,6 +2158,7 @@ _mysql_ConnectionObject_dealloc( mysql_close(&(self->connection)); self->open = 0; } + Py_CLEAR(self->converter); MyFree(self); } From 1a10d0e4fc7b95389fa2a7eb68171040135191a6 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 27 Jun 2017 12:10:11 +0900 Subject: [PATCH 022/177] Support error numbers > CR_MAX_ERROR (#188) fixes #187 --- _mysql.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/_mysql.c b/_mysql.c index 6c5e07ec..8a515511 100644 --- a/_mysql.c +++ b/_mysql.c @@ -119,16 +119,7 @@ _mysql_Exception(_mysql_ConnectionObject *c) return NULL; } merr = mysql_errno(&(c->connection)); - if (!merr) - e = _mysql_InterfaceError; - else if (merr > CR_MAX_ERROR) { - PyTuple_SET_ITEM(t, 0, PyInt_FromLong(-1L)); - PyTuple_SET_ITEM(t, 1, PyString_FromString("error totally whack")); - PyErr_SetObject(_mysql_InterfaceError, t); - Py_DECREF(t); - return NULL; - } - else switch (merr) { + switch (merr) { case CR_COMMANDS_OUT_OF_SYNC: case ER_DB_CREATE_EXISTS: case ER_SYNTAX_ERROR: From 32ae0403d6bf00dd6edf89cac12a2b0c26fe6186 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 6 Jul 2017 22:31:52 +0900 Subject: [PATCH 023/177] add ISSUE_TEMPLATE --- .github/ISSUE_TEMPLATE.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE.md diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 00000000..4b7cf394 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,13 @@ +**IF YOU HAVE SOME TROUBLE, IT'S MAY NOT ISSUE OF THIS PROJECT. GO STACKOVERFLOW!!!** + +If you failed to build, go Stackoverflow. +If you failed to install, go Stackoverflow. +If you failed to connect to database, go Stackoverflow. + +FYI, MySQL Connector/C 6.1.10 has bug. see https://github.com/PyMySQL/mysqlclient-python/issues/169#issuecomment-299778504 + +Only when If you're sure it's PyMySQL's issue, report the complete steps to reproduce, from creating database. + +I don't have time to investigate your issue from an incomplete code snippet. + +See also: https://medium.com/@methane/why-you-must-not-ask-questions-on-github-issues-51d741d83fde From ca41a647cbb00a8699cbe2bd08918fa6e6e29e39 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 28 Aug 2017 22:35:27 +0900 Subject: [PATCH 024/177] remove include "my_config.h". (#198) fixes #197 --- _mysql.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/_mysql.c b/_mysql.c index 8a515511..b0b8fdeb 100644 --- a/_mysql.c +++ b/_mysql.c @@ -26,17 +26,9 @@ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "my_config.h" #include "mysql.h" #include "mysqld_error.h" -#ifdef HAVE_WCSCOLL -#undef HAVE_WCSCOLL -#endif -#ifdef SIZEOF_SIZE_T -#undef SIZEOF_SIZE_T -#endif - #include "Python.h" #if PY_MAJOR_VERSION >= 3 #define IS_PY3K From 92706a7dc72582b3367c365b4eef9bf46d2a847a Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 29 Aug 2017 16:33:55 +0900 Subject: [PATCH 025/177] 1.3.11 --- HISTORY.md | 21 ++++++++++++++++++--- metadata.cfg | 7 +++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 861e28e2..f2c32136 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,18 @@ +====================== + What's new in 1.3.11 +====================== + +Support MariaDB 10.2 client library (#197, #177) + +Add NEWDECIMAL to the NUMBER DBAPISet (#167) + +Allow bulk insert which no space around `VALUES` (#179) + +fix leak of connection->converter. (#182) + +Support error numbers > CR_MAX_ERROR (#188) + + ====================== What's new in 1.3.10 ====================== @@ -16,7 +31,7 @@ Don't use workaround of `bytes.decode('ascii', 'surrogateescape')` on Python 3.6 What's new in 1.3.9 ===================== -Revert adding _binary prefix for bytes/bytearray parameter. It broke backward compatibility. +Revert adding `_binary` prefix for bytes/bytearray parameter. It broke backward compatibility. Fix Windows compile error on MSVC. @@ -27,7 +42,7 @@ Fix Windows compile error on MSVC. Update error constants (#113) -Use _binary prefix for bytes/bytearray parameters (#106) +Use `_binary` prefix for bytes/bytearray parameters (#106) Use mysql_real_escape_string_quote() if exists (#109) @@ -149,7 +164,7 @@ beta 5 Another internal fix for handling remapped character sets. -_mysql.c was broken for the case where read_timeout was *not* available. (Issue #6) +`_mysql.c` was broken for the case where read_timeout was *not* available. (Issue #6) Documentation was converted to sphinx but there is a lot of cleanup left to do. diff --git a/metadata.cfg b/metadata.cfg index b40c91e4..8c57197b 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.3.10 -version_info: (1,3,10,'final',0) +version: 1.3.11 +version_info: (1,3,11,'final',0) description: Python interface to MySQL long_description: ========================= @@ -17,7 +17,7 @@ long_description: - Thread-safety - Thread-friendliness (threads will not block each other) \n - MySQL-5.1 through 5.7 and Python 2.7, 3.3+ are currently + MySQL-5.5 through 5.7 and Python 2.7, 3.4+ are currently supported. PyPy is supported too. \n MySQLdb is `Free Software`_. @@ -47,7 +47,6 @@ classifiers: Programming Language :: Python :: 2 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 - Programming Language :: Python :: 3.3 Programming Language :: Python :: 3.4 Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 From e39df07f29735fb4fef3db14c6b2418184f5e3ba Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 29 Aug 2017 18:16:15 +0900 Subject: [PATCH 026/177] Fix docstring for use_unicode type --- MySQLdb/connections.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 1511024f..8d9f2dd5 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -110,7 +110,7 @@ def __init__(self, *args, **kwargs): :param type cursorclass: class object, used to create cursors (keyword only) - :param str use_unicode: + :param bool use_unicode: If True, text-like columns are returned as unicode objects using the connection's character set. Otherwise, text-like columns are returned as strings. columns are returned as From ca4317a6b5ffc4c78c711123b52dfda400d00eb5 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 30 Aug 2017 18:49:02 +0900 Subject: [PATCH 027/177] Fix encoding tuple argument (#155) Since Connections.encoders is broken by design. Tuple and list is escaped directly in `Connection.literal()`. Removed tuple and list from converters mapping. Fixes #145 --- MySQLdb/connections.py | 82 +++++++++++++++++++++--------------------- MySQLdb/converters.py | 8 ++--- MySQLdb/cursors.py | 10 +++--- _mysql.c | 2 ++ 4 files changed, 51 insertions(+), 51 deletions(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 8d9f2dd5..e5d9fa6f 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -186,7 +186,7 @@ class object, used to create cursors (keyword only) use_unicode = kwargs2.pop('use_unicode', use_unicode) sql_mode = kwargs2.pop('sql_mode', '') - binary_prefix = kwargs2.pop('binary_prefix', False) + self._binary_prefix = kwargs2.pop('binary_prefix', False) client_flag = kwargs.get('client_flag', 0) client_version = tuple([ numeric_part(n) for n in _mysql.get_client_info().split('.')[:2] ]) @@ -208,38 +208,28 @@ class object, used to create cursors (keyword only) self._server_version = tuple([ numeric_part(n) for n in self.get_server_info().split('.')[:2] ]) + self.encoding = 'ascii' # overriden in set_character_set() db = proxy(self) - def _get_string_literal(): - # Note: string_literal() is called for bytes object on Python 3 (via bytes_literal) - def string_literal(obj, dummy=None): - return db.string_literal(obj) - return string_literal - - def _get_unicode_literal(): - if PY2: - # unicode_literal is called for only unicode object. - def unicode_literal(u, dummy=None): - return db.string_literal(u.encode(unicode_literal.charset)) - else: - # unicode_literal() is called for arbitrary object. - def unicode_literal(u, dummy=None): - return db.string_literal(str(u).encode(unicode_literal.charset)) - return unicode_literal - - def _get_bytes_literal(): - def bytes_literal(obj, dummy=None): - return b'_binary' + db.string_literal(obj) - return bytes_literal - - def _get_string_decoder(): - def string_decoder(s): - return s.decode(string_decoder.charset) - return string_decoder - - string_literal = _get_string_literal() - self.unicode_literal = unicode_literal = _get_unicode_literal() - bytes_literal = _get_bytes_literal() - self.string_decoder = string_decoder = _get_string_decoder() + + # Note: string_literal() is called for bytes object on Python 3 (via bytes_literal) + def string_literal(obj, dummy=None): + return db.string_literal(obj) + + if PY2: + # unicode_literal is called for only unicode object. + def unicode_literal(u, dummy=None): + return db.string_literal(u.encode(db.encoding)) + else: + # unicode_literal() is called for arbitrary object. + def unicode_literal(u, dummy=None): + return db.string_literal(str(u).encode(db.encoding)) + + def bytes_literal(obj, dummy=None): + return b'_binary' + db.string_literal(obj) + + def string_decoder(s): + return s.decode(db.encoding) + if not charset: charset = self.character_set_name() self.set_character_set(charset) @@ -253,12 +243,7 @@ def string_decoder(s): self.converter[FIELD_TYPE.VARCHAR].append((None, string_decoder)) self.converter[FIELD_TYPE.BLOB].append((None, string_decoder)) - if binary_prefix: - self.encoders[bytes] = string_literal if PY2 else bytes_literal - self.encoders[bytearray] = bytes_literal - else: - self.encoders[bytes] = string_literal - + self.encoders[bytes] = string_literal self.encoders[unicode] = unicode_literal self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS if self._transactional: @@ -305,6 +290,16 @@ def __exit__(self, exc, value, tb): else: self.commit() + def _bytes_literal(self, bs): + assert isinstance(bs, (bytes, bytearray)) + x = self.string_literal(bs) # x is escaped and quoted bytes + if self._binary_prefix: + return b'_binary' + x + return x + + def _tuple_literal(self, t, d): + return "(%s)" % (','.join(map(self.literal, t))) + def literal(self, o): """If o is a single object, returns an SQL literal as a string. If o is a non-string sequence, the items of the sequence are @@ -313,7 +308,14 @@ def literal(self, o): Non-standard. For internal use; do not use this in your applications. """ - s = self.escape(o, self.encoders) + if isinstance(o, bytearray): + s = self._bytes_literal(o) + elif not PY2 and isinstance(o, bytes): + s = self._bytes_literal(o) + elif isinstance(o, (tuple, list)): + s = self._tuple_literal(o) + else: + s = self.escape(o, self.encoders) # Python 3(~3.4) doesn't support % operation for bytes object. # We should decode it before using %. # Decoding with ascii and surrogateescape allows convert arbitrary @@ -360,8 +362,6 @@ def set_character_set(self, charset): raise NotSupportedError("server is too old to set charset") self.query('SET NAMES %s' % charset) self.store_result() - self.string_decoder.charset = py_charset - self.unicode_literal.charset = py_charset self.encoding = py_charset def set_sql_mode(self, sql_mode): diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index 8a9908d4..505a4df0 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -29,10 +29,9 @@ Don't modify conversions if you can avoid it. Instead, make copies (with the copy() method), modify the copies, and then pass them to MySQL.connect(). - """ -from _mysql import string_literal, escape_sequence, escape_dict, escape, NULL +from _mysql import string_literal, escape, NULL from MySQLdb.constants import FIELD_TYPE, FLAG from MySQLdb.times import * from MySQLdb.compat import PY2, long @@ -53,6 +52,7 @@ def Str2Set(s): return set([ i for i in s.split(',') if i ]) def Set2Str(s, d): + # Only support ascii string. Not tested. return string_literal(','.join(s), d) def Thing2Str(s, d): @@ -97,9 +97,6 @@ def quote_tuple(t, d): long: Thing2Str, float: Float2Str, NoneType: None2NULL, - tuple: quote_tuple, - list: quote_tuple, - dict: escape_dict, ArrayType: array2Str, bool: Bool2Str, Date: Thing2Literal, @@ -107,6 +104,7 @@ def quote_tuple(t, d): DateTimeDeltaType: DateTimeDelta2literal, str: Thing2Literal, # default set: Set2Str, + FIELD_TYPE.TINY: int, FIELD_TYPE.SHORT: int, FIELD_TYPE.LONG: long, diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index dfbd736b..3769ab5e 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -225,7 +225,7 @@ def execute(self, query, args=None): # db.literal(obj) always returns str. if PY2 and isinstance(query, unicode): - query = query.encode(db.unicode_literal.charset) + query = query.encode(db.encoding) if args is not None: if isinstance(args, dict): @@ -233,14 +233,14 @@ def execute(self, query, args=None): else: args = tuple(map(db.literal, args)) if not PY2 and isinstance(query, (bytes, bytearray)): - query = query.decode(db.unicode_literal.charset) + query = query.decode(db.encoding) try: query = query % args except TypeError as m: self.errorhandler(self, ProgrammingError, str(m)) if isinstance(query, unicode): - query = query.encode(db.unicode_literal.charset, 'surrogateescape') + query = query.encode(db.encoding, 'surrogateescape') res = None try: @@ -353,7 +353,7 @@ def callproc(self, procname, args=()): q = "SET @_%s_%d=%s" % (procname, index, db.literal(arg)) if isinstance(q, unicode): - q = q.encode(db.unicode_literal.charset, 'surrogateescape') + q = q.encode(db.encoding, 'surrogateescape') self._query(q) self.nextset() @@ -361,7 +361,7 @@ def callproc(self, procname, args=()): ','.join(['@_%s_%d' % (procname, i) for i in range(len(args))])) if isinstance(q, unicode): - q = q.encode(db.unicode_literal.charset, 'surrogateescape') + q = q.encode(db.encoding, 'surrogateescape') self._query(q) self._executed = q if not self._defer_warnings: diff --git a/_mysql.c b/_mysql.c index b0b8fdeb..36672373 100644 --- a/_mysql.c +++ b/_mysql.c @@ -2777,12 +2777,14 @@ _mysql_methods[] = { _mysql_escape__doc__ }, { + // deprecated. "escape_sequence", (PyCFunction)_mysql_escape_sequence, METH_VARARGS, _mysql_escape_sequence__doc__ }, { + // deprecated. "escape_dict", (PyCFunction)_mysql_escape_dict, METH_VARARGS, From bfefd34bc17430046ddf50fb2a37ced810f62467 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 30 Aug 2017 18:49:49 +0900 Subject: [PATCH 028/177] update HISTORY --- HISTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index f2c32136..74e20f1e 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -12,6 +12,8 @@ fix leak of connection->converter. (#182) Support error numbers > CR_MAX_ERROR (#188) +Fix tuple argument support (#145) + ====================== What's new in 1.3.10 From 823e56a4b295abc35f5b28b836c1750288b115d8 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 30 Aug 2017 18:51:07 +0900 Subject: [PATCH 029/177] 1.3.11c1 --- metadata.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metadata.cfg b/metadata.cfg index 8c57197b..f53ba760 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.3.11 -version_info: (1,3,11,'final',0) +version: 1.3.11c1 +version_info: (1,3,11,'c',1) description: Python interface to MySQL long_description: ========================= From 6058715077d7b63599eefd6eb8460548cba7c951 Mon Sep 17 00:00:00 2001 From: Ramiro Aparicio Date: Thu, 31 Aug 2017 03:53:55 +0200 Subject: [PATCH 030/177] Fix compile with MariaDB 10.2.8 (#200) --- _mysql.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_mysql.c b/_mysql.c index 36672373..2134057e 100644 --- a/_mysql.c +++ b/_mysql.c @@ -1039,7 +1039,7 @@ _mysql_escape_string( if (self && PyModule_Check((PyObject*)self)) self = NULL; if (self && self->open) { -#if MYSQL_VERSION_ID >= 50707 && !defined(MARIADB_BASE_VERSION) +#if MYSQL_VERSION_ID >= 50707 && !defined(MARIADB_BASE_VERSION) && !defined(MARIADB_VERSION_ID) len = mysql_real_escape_string_quote(&(self->connection), out, in, size, '\''); #else len = mysql_real_escape_string(&(self->connection), out, in, size); @@ -1097,7 +1097,7 @@ _mysql_string_literal( out = PyBytes_AS_STRING(str); check_server_init(NULL); if (self && self->open) { -#if MYSQL_VERSION_ID >= 50707 && !defined(MARIADB_BASE_VERSION) +#if MYSQL_VERSION_ID >= 50707 && !defined(MARIADB_BASE_VERSION) && !defined(MARIADB_VERSION_ID) len = mysql_real_escape_string_quote(&(self->connection), out+1, in, size, '\''); #else len = mysql_real_escape_string(&(self->connection), out+1, in, size); From 71e15b1b3be2c685eee27459f1c8ad9a4350e8fc Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Fri, 1 Sep 2017 10:54:36 +0900 Subject: [PATCH 031/177] 1.3.11 --- HISTORY.md | 6 +++--- metadata.cfg | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 74e20f1e..3f043ee3 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,15 +2,15 @@ What's new in 1.3.11 ====================== -Support MariaDB 10.2 client library (#197, #177) +Support MariaDB 10.2 client library (#197, #177, #200) Add NEWDECIMAL to the NUMBER DBAPISet (#167) Allow bulk insert which no space around `VALUES` (#179) -fix leak of connection->converter. (#182) +Fix leak of `connection->converter`. (#182) -Support error numbers > CR_MAX_ERROR (#188) +Support error `numbers > CR_MAX_ERROR` (#188) Fix tuple argument support (#145) diff --git a/metadata.cfg b/metadata.cfg index f53ba760..94f11f83 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.3.11c1 -version_info: (1,3,11,'c',1) +version: 1.3.11 +version_info: (1,3,11,'final',0) description: Python interface to MySQL long_description: ========================= @@ -17,8 +17,8 @@ long_description: - Thread-safety - Thread-friendliness (threads will not block each other) \n - MySQL-5.5 through 5.7 and Python 2.7, 3.4+ are currently - supported. PyPy is supported too. + MySQL-5.5 through 5.7 and Python 2.7, 3.4+ are currently supported. + PyPy is supported too. \n MySQLdb is `Free Software`_. \n @@ -47,6 +47,7 @@ classifiers: Programming Language :: Python :: 2 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 + Programming Language :: Python :: 3.3 Programming Language :: Python :: 3.4 Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 From bab17ed5049afc4247601f44ed3c52768c1b2fe4 Mon Sep 17 00:00:00 2001 From: NestorTejero Date: Fri, 1 Sep 2017 18:39:41 +0200 Subject: [PATCH 032/177] remove extra parameter (#201) This produced the error `TypeError: _tuple_literal() missing 1 required positional argument: 'd'` on line 316. --- MySQLdb/connections.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index e5d9fa6f..c03e128d 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -297,7 +297,7 @@ def _bytes_literal(self, bs): return b'_binary' + x return x - def _tuple_literal(self, t, d): + def _tuple_literal(self, t): return "(%s)" % (','.join(map(self.literal, t))) def literal(self, o): From 1a9874b340a3d0dda1470509c924ad9fc8284df7 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sat, 2 Sep 2017 01:50:16 +0900 Subject: [PATCH 033/177] raise InterfaceError when mysql_errno() returns 0 (#203) --- _mysql.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/_mysql.c b/_mysql.c index 2134057e..49c8d9b1 100644 --- a/_mysql.c +++ b/_mysql.c @@ -112,6 +112,9 @@ _mysql_Exception(_mysql_ConnectionObject *c) } merr = mysql_errno(&(c->connection)); switch (merr) { + case 0: + e = _mysql_InterfaceError; + break; case CR_COMMANDS_OUT_OF_SYNC: case ER_DB_CREATE_EXISTS: case ER_SYNTAX_ERROR: From 46e55812386ab9aab169b6a5f8daa0619c50792d Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sat, 2 Sep 2017 02:39:45 +0900 Subject: [PATCH 034/177] Update HISTORY --- HISTORY.md => HISTORY.rst | 8 ++++++++ MANIFEST.in | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) rename HISTORY.md => HISTORY.rst (97%) diff --git a/HISTORY.md b/HISTORY.rst similarity index 97% rename from HISTORY.md rename to HISTORY.rst index 3f043ee3..b6aa8c2b 100644 --- a/HISTORY.md +++ b/HISTORY.rst @@ -1,3 +1,11 @@ +====================== + What's new in 1.3.12 +====================== + +Fix tuple argument again (#201) + +InterfaceError is raised when Connection.query() is called for closed connection (#202) + ====================== What's new in 1.3.11 ====================== diff --git a/MANIFEST.in b/MANIFEST.in index 4f853e82..415a995a 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -2,7 +2,7 @@ recursive-include doc *.rst recursive-include tests *.py include doc/conf.py include MANIFEST.in -include HISTORY.md +include HISTORY.rst include INSTALL.rst include README.md include LICENSE From 2996f9bebc90b8bb1c51a3c12285e59e1fb2fcf0 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sat, 2 Sep 2017 02:40:15 +0900 Subject: [PATCH 035/177] 1.3.12 --- metadata.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metadata.cfg b/metadata.cfg index 94f11f83..cf40b262 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.3.11 -version_info: (1,3,11,'final',0) +version: 1.3.12 +version_info: (1,3,12,'final',0) description: Python interface to MySQL long_description: ========================= From 73ccdcdddf2dd8e193e8af4a2362727c601b40ac Mon Sep 17 00:00:00 2001 From: kilroy42 Date: Thu, 14 Dec 2017 09:30:29 +0100 Subject: [PATCH 036/177] Fix decoding tiny/medium/long blobs (#215) --- MySQLdb/connections.py | 7 +++---- tests/test_MySQLdb_capabilities.py | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index c03e128d..6d5f402e 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -238,10 +238,9 @@ def string_decoder(s): self.set_sql_mode(sql_mode) if use_unicode: - self.converter[FIELD_TYPE.STRING].append((None, string_decoder)) - self.converter[FIELD_TYPE.VAR_STRING].append((None, string_decoder)) - self.converter[FIELD_TYPE.VARCHAR].append((None, string_decoder)) - self.converter[FIELD_TYPE.BLOB].append((None, string_decoder)) + for t in (FIELD_TYPE.STRING, FIELD_TYPE.VAR_STRING, FIELD_TYPE.VARCHAR, FIELD_TYPE.TINY_BLOB, + FIELD_TYPE.MEDIUM_BLOB, FIELD_TYPE.LONG_BLOB, FIELD_TYPE.BLOB): + self.converter[t].append((None, string_decoder)) self.encoders[bytes] = string_literal self.encoders[unicode] = unicode_literal diff --git a/tests/test_MySQLdb_capabilities.py b/tests/test_MySQLdb_capabilities.py index f1887575..0427ed74 100644 --- a/tests/test_MySQLdb_capabilities.py +++ b/tests/test_MySQLdb_capabilities.py @@ -116,7 +116,7 @@ def test_MULTIPOLYGON(self): c.execute("SELECT id, AsText(border) FROM test_MULTIPOLYGON") row = c.fetchone() self.assertEqual(row[0], 1) - self.assertEqual(row[1], b'MULTIPOLYGON(((1 1,1 -1,-1 -1,-1 1,1 1)),((1 1,3 1,3 3,1 3,1 1)))') + self.assertEqual(row[1], 'MULTIPOLYGON(((1 1,1 -1,-1 -1,-1 1,1 1)),((1 1,3 1,3 3,1 3,1 1)))') c.execute("SELECT id, AsWKB(border) FROM test_MULTIPOLYGON") row = c.fetchone() From b948553229f6d25fb9a586185b8fec21b5bf4b30 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Thu, 21 Dec 2017 18:18:57 +0900 Subject: [PATCH 037/177] simplify memory management of the conv dictionary In ResultObject_Initialize, always borrow the conv dictionary from the caller. This simplifies the code, removes an allocation, and fixes ref leaks in error cases. --- _mysql.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/_mysql.c b/_mysql.c index 49c8d9b1..a3c53796 100644 --- a/_mysql.c +++ b/_mysql.c @@ -363,12 +363,6 @@ _mysql_ResultObject_Initialize( if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|iO", kwlist, &_mysql_ConnectionObject_Type, &conn, &use, &conv)) return -1; - if (!conv) { - if (!(conv = PyDict_New())) - return -1; - } - else - Py_INCREF(conv); self->conn = (PyObject *) conn; Py_INCREF(conn); @@ -387,13 +381,11 @@ _mysql_ResultObject_Initialize( return -1; } self->converter = PyTuple_New(0); - Py_DECREF(conv); return 0; } n = mysql_num_fields(result); self->nfields = n; if (!(self->converter = PyTuple_New(n))) { - Py_DECREF(conv); return -1; } fields = mysql_fetch_fields(result); @@ -401,15 +393,13 @@ _mysql_ResultObject_Initialize( PyObject *tmp, *fun; tmp = PyInt_FromLong((long) fields[i].type); if (!tmp) { - Py_DECREF(conv); return -1; } - fun = PyObject_GetItem(conv, tmp); + fun = conv ? PyObject_GetItem(conv, tmp) : NULL; Py_DECREF(tmp); if (!fun) { if (PyErr_Occurred()) { if (!PyErr_ExceptionMatches(PyExc_KeyError)) { - Py_DECREF(conv); return -1; } PyErr_Clear(); @@ -428,7 +418,6 @@ _mysql_ResultObject_Initialize( PyObject *t = PySequence_GetItem(fun, j); if (!t) { Py_DECREF(fun); - Py_DECREF(conv); return -1; } if (PyTuple_Check(t) && PyTuple_GET_SIZE(t) == 2) { @@ -463,7 +452,6 @@ _mysql_ResultObject_Initialize( PyTuple_SET_ITEM(self->converter, i, fun); } - Py_DECREF(conv); return 0; } From d65e10e356035f73584c59b381fbd0a01033dfd2 Mon Sep 17 00:00:00 2001 From: Graham Voysey Date: Thu, 21 Dec 2017 04:41:31 -0500 Subject: [PATCH 038/177] Add note about annoying MySQL Connector bug. (#212) --- README.md | 35 ++++++++++++++++++++++++++++++----- setup_posix.py | 2 ++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 60ec4ea5..2262e9d5 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,11 @@ I hope this fork is merged back to MySQLdb1 like distribute was merged back to s You may need to install the Python and MySQL development headers and libraries like so: -`sudo apt-get install python-dev libmysqlclient-dev` # Debian / Ubuntu +* `sudo apt-get install python-dev libmysqlclient-dev` # Debian / Ubuntu +* `sudo yum install python-devel mysql-devel` # Red Hat / CentOS +* `brew install mysql-connector-c` # macOS (Homebrew) (Currently, it has bug. See below) -`sudo yum install python-devel mysql-devel` # Red Hat / CentOS - -On Windows, there are binary wheel you can install without MySQLConnector/C or MSVC. +On Windows, there are binary wheels you can install without MySQLConnector/C or MSVC. #### Note on Python 3 : if you are using python3 then you need to install python3-dev using the following command : @@ -25,7 +25,32 @@ On Windows, there are binary wheel you can install without MySQLConnector/C or M `sudo yum install python3-devel ` # Red Hat / CentOS -`brew install mysql-connector-c` # macOS (Homebrew) +#### **Note about bug of MySQL Connector/C on macOS** + +See also: https://bugs.mysql.com/bug.php?id=86971 + +Versions of MySQL Connector/C may have incorrect default configuration options that cause compilation errors when `mysqlclient-python` is installed. (As of November 2017, this is known to be true for homebrew's `mysql-connector-c` and [official package](https://dev.mysql.com/downloads/connector/c/)) + +Modification of `mysql_config` resolves these issues as follows. + +Change + +``` +# on macOS, on or about line 112: +# Create options +libs="-L$pkglibdir" +libs="$libs -l " +``` + +to + +``` +# Create options +libs="-L$pkglibdir" +libs="$libs -lmysqlclient -lssl -lcrypto" +``` + +An improper ssl configuration may also create issues; see, e.g, `brew info openssl` for details on macOS. ### Install from PyPI diff --git a/setup_posix.py b/setup_posix.py index 320e9e99..14cffcad 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -9,6 +9,8 @@ # of mysql_config def dequote(s): + if not s: + raise Exception("Wrong MySQL configuration: maybe https://bugs.mysql.com/bug.php?id=86971 ?") if s[0] in "\"'" and s[0] == s[-1]: s = s[1:-1] return s From b10ecd0b46b8acec72c618826b4a61137b42cf1a Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 21 Dec 2017 18:48:50 +0900 Subject: [PATCH 039/177] Remove broken row_seek() and row_tell() (#220) They are broken from long ago and making them safe is difficult. So remove them rather than fix. --- _mysql.c | 52 ---------------------------------------------------- 1 file changed, 52 deletions(-) diff --git a/_mysql.c b/_mysql.c index a3c53796..5216fa5b 100644 --- a/_mysql.c +++ b/_mysql.c @@ -2166,46 +2166,6 @@ _mysql_ResultObject_data_seek( return Py_None; } -static char _mysql_ResultObject_row_seek__doc__[] = -"row_seek(n) -- seek by offset n rows of result set"; -static PyObject * -_mysql_ResultObject_row_seek( - _mysql_ResultObject *self, - PyObject *args) -{ - int offset; - MYSQL_ROW_OFFSET r; - if (!PyArg_ParseTuple(args, "i:row_seek", &offset)) return NULL; - check_result_connection(self); - if (self->use) { - PyErr_SetString(_mysql_ProgrammingError, - "cannot be used with connection.use_result()"); - return NULL; - } - r = mysql_row_tell(self->result); - mysql_row_seek(self->result, r+offset); - Py_INCREF(Py_None); - return Py_None; -} - -static char _mysql_ResultObject_row_tell__doc__[] = -"row_tell() -- return the current row number of the result set."; -static PyObject * -_mysql_ResultObject_row_tell( - _mysql_ResultObject *self, - PyObject *noargs) -{ - MYSQL_ROW_OFFSET r; - check_result_connection(self); - if (self->use) { - PyErr_SetString(_mysql_ProgrammingError, - "cannot be used with connection.use_result()"); - return NULL; - } - r = mysql_row_tell(self->result); - return PyInt_FromLong(r-self->result->data->data); -} - static void _mysql_ResultObject_dealloc( _mysql_ResultObject *self) @@ -2504,18 +2464,6 @@ static PyMethodDef _mysql_ResultObject_methods[] = { METH_VARARGS, _mysql_ResultObject_data_seek__doc__ }, - { - "row_seek", - (PyCFunction)_mysql_ResultObject_row_seek, - METH_VARARGS, - _mysql_ResultObject_row_seek__doc__ - }, - { - "row_tell", - (PyCFunction)_mysql_ResultObject_row_tell, - METH_NOARGS, - _mysql_ResultObject_row_tell__doc__ - }, { "describe", (PyCFunction)_mysql_ResultObject_describe, From 1ea5d01db84d68e45c2c25427fc70d8c28dbdc8c Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 21 Dec 2017 19:16:24 +0900 Subject: [PATCH 040/177] my_bool is removed from MySQL 8 Fixed #218 --- _mysql.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_mysql.c b/_mysql.c index 5216fa5b..014c2bda 100644 --- a/_mysql.c +++ b/_mysql.c @@ -1879,7 +1879,7 @@ _mysql_ConnectionObject_ping( if (!PyArg_ParseTuple(args, "|I", &reconnect)) return NULL; check_connection(self); if (reconnect != -1) { - my_bool recon = reconnect; + char recon = (char)reconnect; mysql_options(&self->connection, MYSQL_OPT_RECONNECT, &recon); } Py_BEGIN_ALLOW_THREADS From a2ebbd207375f545a8e97797b0c980313a58982f Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 21 Dec 2017 20:09:58 +0900 Subject: [PATCH 041/177] Use _Bool only for MySQL 8 --- _mysql.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/_mysql.c b/_mysql.c index 014c2bda..24eacb33 100644 --- a/_mysql.c +++ b/_mysql.c @@ -29,6 +29,11 @@ PERFORMANCE OF THIS SOFTWARE. #include "mysql.h" #include "mysqld_error.h" +#if MYSQL_VERSION_ID >= 80000 +// https://github.com/mysql/mysql-server/commit/eb821c023cedc029ca0b06456dfae365106bee84 +#define my_bool _Bool +#endif + #include "Python.h" #if PY_MAJOR_VERSION >= 3 #define IS_PY3K @@ -1879,7 +1884,7 @@ _mysql_ConnectionObject_ping( if (!PyArg_ParseTuple(args, "|I", &reconnect)) return NULL; check_connection(self); if (reconnect != -1) { - char recon = (char)reconnect; + my_bool recon = (my_bool)reconnect; mysql_options(&self->connection, MYSQL_OPT_RECONNECT, &recon); } Py_BEGIN_ALLOW_THREADS From 1f8bd5361e068bc81a522ae405bb632d74d86be4 Mon Sep 17 00:00:00 2001 From: Vilnis Termanis Date: Sat, 20 Jan 2018 17:40:38 +0000 Subject: [PATCH 042/177] Reduce callproc roundtrip time - Make only one single call to SET variables used as procedure parameters --- MySQLdb/cursors.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 3769ab5e..b3632933 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -349,9 +349,10 @@ def callproc(self, procname, args=()): """ db = self._get_db() - for index, arg in enumerate(args): - q = "SET @_%s_%d=%s" % (procname, index, - db.literal(arg)) + if args: + argFmt = '@_{0}_%d=%s'.format(procname) + q = 'SET %s' % ','.join(argFmt % (index, db.literal(arg)) + for index, arg in enumerate(args)) if isinstance(q, unicode): q = q.encode(db.encoding, 'surrogateescape') self._query(q) From f2f94dcddeddbcd0b458873f6166b5d1dea23ba0 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 15 Mar 2018 18:44:22 +0900 Subject: [PATCH 043/177] Update cursors.py --- MySQLdb/cursors.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index b3632933..bc1334b7 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -350,9 +350,9 @@ def callproc(self, procname, args=()): db = self._get_db() if args: - argFmt = '@_{0}_%d=%s'.format(procname) - q = 'SET %s' % ','.join(argFmt % (index, db.literal(arg)) - for index, arg in enumerate(args)) + fmt = '@_{0}_%d=%s'.format(procname) + q = 'SET %s' % ','.join(fmt % (index, db.literal(arg)) + for index, arg in enumerate(args)) if isinstance(q, unicode): q = q.encode(db.encoding, 'surrogateescape') self._query(q) From f95cfc09f6f9f569f6a9db93c39e8b56069b0b0e Mon Sep 17 00:00:00 2001 From: Lee Gaines Date: Thu, 22 Mar 2018 09:29:27 -0700 Subject: [PATCH 044/177] typo/grammar fix Changes "cursor execute" to "cursor executes" on line 388. --- doc/user_guide.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/user_guide.rst b/doc/user_guide.rst index 0ce88aee..86a8b17a 100644 --- a/doc/user_guide.rst +++ b/doc/user_guide.rst @@ -385,7 +385,7 @@ threadsafety SSCursor (which uses ``mysql_use_result()``; with the latter you must ensure all the rows have been read before another query can be executed. It is further complicated by the addition of - transactions, since transactions start when a cursor execute a + transactions, since transactions start when a cursor executes a query, but end when ``COMMIT`` or ``ROLLBACK`` is executed by the Connection object. Two threads simply cannot share a connection while a transaction is in progress, in addition to From 0ce8ed205d4d7c345f42f9c7e59d699bc4e2fc68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 15 May 2018 22:00:54 +0200 Subject: [PATCH 045/177] Use pytest instead of py.test per upstream recommendation, #dropthedot http://blog.pytest.org/2016/whats-new-in-pytest-30/ https://twitter.com/hashtag/dropthedot --- .travis.yml | 2 +- tests/test_cursor.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 575f7e40..e1909524 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,7 +30,7 @@ before_script: script: - pip install -e . - - py.test --cov ./MySQLdb + - pytest --cov ./MySQLdb after_succes: - codecov diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 535afec3..7919b561 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -1,4 +1,4 @@ -import py.test +import pytest import MySQLdb.cursors from configdb import connection_factory From 07f4739d97396976cec3e29a306eba7c30f6a39a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 15 May 2018 22:04:10 +0200 Subject: [PATCH 046/177] Spelling and grammar fixes --- MySQLdb/connections.py | 2 +- doc/user_guide.rst | 2 +- tests/dbapi20.py | 2 +- tests/test_MySQLdb_dbapi20.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 6d5f402e..8375cd0a 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -208,7 +208,7 @@ class object, used to create cursors (keyword only) self._server_version = tuple([ numeric_part(n) for n in self.get_server_info().split('.')[:2] ]) - self.encoding = 'ascii' # overriden in set_character_set() + self.encoding = 'ascii' # overridden in set_character_set() db = proxy(self) # Note: string_literal() is called for bytes object on Python 3 (via bytes_literal) diff --git a/doc/user_guide.rst b/doc/user_guide.rst index 86a8b17a..a0b752c3 100644 --- a/doc/user_guide.rst +++ b/doc/user_guide.rst @@ -8,7 +8,7 @@ MySQLdb User's Guide Introduction ------------ -MySQLdb is an thread-compatible interface to the popular MySQL +MySQLdb is a thread-compatible interface to the popular MySQL database server that provides the Python database API. Installation diff --git a/tests/dbapi20.py b/tests/dbapi20.py index 19109f4d..e28d5d1f 100644 --- a/tests/dbapi20.py +++ b/tests/dbapi20.py @@ -633,7 +633,7 @@ def test_fetchall(self): cur.execute(sql) # cursor.fetchall should raise an Error if called - # after executing a a statement that cannot return rows + # after executing a statement that cannot return rows self.assertRaises(self.driver.Error,cur.fetchall) cur.execute('select name from %sbooze' % self.table_prefix) diff --git a/tests/test_MySQLdb_dbapi20.py b/tests/test_MySQLdb_dbapi20.py index 2274c454..88eaaef8 100644 --- a/tests/test_MySQLdb_dbapi20.py +++ b/tests/test_MySQLdb_dbapi20.py @@ -35,7 +35,7 @@ def test_fetchall(self): cur.execute(sql) # cursor.fetchall should raise an Error if called - # after executing a a statement that cannot return rows + # after executing a statement that cannot return rows #self.assertRaises(self.driver.Error,cur.fetchall) cur.execute('select name from %sbooze' % self.table_prefix) From 46805facbc099a7c4887e2acf7bbea4fe0218459 Mon Sep 17 00:00:00 2001 From: "e. ravyn deadbunny" Date: Tue, 15 May 2018 17:37:58 -0700 Subject: [PATCH 047/177] Change default build option to use static linking for the MySQL client library, and support perconaserverclient as a valid link target. --- setup_posix.py | 14 ++++++++++---- site.cfg | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/setup_posix.py b/setup_posix.py index 14cffcad..216347e0 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -41,16 +41,12 @@ def get_config(): static = enabled(options, 'static') if enabled(options, 'embedded'): libs = mysql_config("libmysqld-libs") - client = "mysqld" elif enabled(options, 'threadsafe'): libs = mysql_config("libs_r") - client = "mysqlclient_r" if not libs: libs = mysql_config("libs") - client = "mysqlclient" else: libs = mysql_config("libs") - client = "mysqlclient" library_dirs = [dequote(i[2:]) for i in libs if i.startswith('-L')] libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')] @@ -68,6 +64,16 @@ def get_config(): include_dirs = [dequote(i[2:]) for i in mysql_config('include') if i.startswith('-I')] + + # properly handle mysql client libraries that are not called libmysqlclient + client = None + CLIENT_LIST = ['mysqlclient', 'mysqlclient_r', 'mysqld', + 'perconaserverclient', 'perconaserverclient_r'] + for c in CLIENT_LIST: + if c in libraries: + client = c + break + if static: extra_objects.append(os.path.join(library_dirs[0], 'lib%s.a' % client)) if client in libraries: diff --git a/site.cfg b/site.cfg index 8e2ab991..8afdef77 100644 --- a/site.cfg +++ b/site.cfg @@ -5,7 +5,7 @@ embedded = False threadsafe = True -static = False +static = True # The path to mysql_config. # Only use this if mysql_config is not on your PATH, or you have some weird From cacd5d0398e2d2e5c1540221928a15e6ec80c1b2 Mon Sep 17 00:00:00 2001 From: "E. Souhrada" Date: Tue, 15 May 2018 18:19:26 -0700 Subject: [PATCH 048/177] revert back to non-static linking by default --- site.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site.cfg b/site.cfg index 8afdef77..8e2ab991 100644 --- a/site.cfg +++ b/site.cfg @@ -5,7 +5,7 @@ embedded = False threadsafe = True -static = True +static = False # The path to mysql_config. # Only use this if mysql_config is not on your PATH, or you have some weird From 50fa27df5c857743e083452b5640dbc675919eab Mon Sep 17 00:00:00 2001 From: "E. Souhrada" Date: Tue, 15 May 2018 22:35:53 -0700 Subject: [PATCH 049/177] Add optional --static flag to customize _mysql.so at install time --- setup_posix.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/setup_posix.py b/setup_posix.py index 216347e0..6630a115 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -39,6 +39,14 @@ def get_config(): extra_objects = [] static = enabled(options, 'static') + + # allow a command-line option to override the base config file to permit + # a static build to be created via requirements.txt + # + if '--static' in sys.argv: + static = True + sys.argv.remove('--static') + if enabled(options, 'embedded'): libs = mysql_config("libmysqld-libs") elif enabled(options, 'threadsafe'): From 92cd53289376dbd1eff90a94b2810c4641205d5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kukr=C3=A1l?= Date: Thu, 31 May 2018 10:38:54 +0200 Subject: [PATCH 050/177] use metapackage for libmysqlclient-dev (#245) libmysqliclient-dev package isn't available in Debian (Stretch) anymore. It's better to use metapackage for installation because it is available in Ubuntu and Debian. --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2262e9d5..cd91383d 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,11 @@ I hope this fork is merged back to MySQLdb1 like distribute was merged back to s You may need to install the Python and MySQL development headers and libraries like so: -* `sudo apt-get install python-dev libmysqlclient-dev` # Debian / Ubuntu +* `sudo apt-get install python-dev default-libmysqlclient-dev` # Debian / Ubuntu * `sudo yum install python-devel mysql-devel` # Red Hat / CentOS * `brew install mysql-connector-c` # macOS (Homebrew) (Currently, it has bug. See below) -On Windows, there are binary wheels you can install without MySQLConnector/C or MSVC. +On Windows, there are binary wheels you can install without MySQLConnector/C or MSVC. #### Note on Python 3 : if you are using python3 then you need to install python3-dev using the following command : @@ -31,21 +31,21 @@ See also: https://bugs.mysql.com/bug.php?id=86971 Versions of MySQL Connector/C may have incorrect default configuration options that cause compilation errors when `mysqlclient-python` is installed. (As of November 2017, this is known to be true for homebrew's `mysql-connector-c` and [official package](https://dev.mysql.com/downloads/connector/c/)) -Modification of `mysql_config` resolves these issues as follows. +Modification of `mysql_config` resolves these issues as follows. Change ``` # on macOS, on or about line 112: -# Create options +# Create options libs="-L$pkglibdir" libs="$libs -l " ``` -to +to ``` -# Create options +# Create options libs="-L$pkglibdir" libs="$libs -lmysqlclient -lssl -lcrypto" ``` From 99c1eea5782e5319aa512f567255c76d417c63e9 Mon Sep 17 00:00:00 2001 From: "E. Souhrada" Date: Wed, 27 Jun 2018 03:47:03 -0700 Subject: [PATCH 051/177] Use c++ for static link (#243) --- setup_posix.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/setup_posix.py b/setup_posix.py index 6630a115..6e409609 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -108,6 +108,11 @@ def get_config(): extra_objects = extra_objects, define_macros = define_macros, ) + + # newer versions of gcc require libstdc++ if doing a static build + if static: + ext_options['language'] = 'c++' + return metadata, ext_options if __name__ == "__main__": From 3de469db63c5da47d61ac79bba5dadc0d22bde5c Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 27 Jun 2018 20:27:54 +0900 Subject: [PATCH 052/177] 1.3.13 --- HISTORY.rst | 13 +++++++++++++ MySQLdb/__init__.py | 8 ++------ metadata.cfg | 28 ++-------------------------- setup.py | 13 +++++++------ 4 files changed, 24 insertions(+), 38 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index b6aa8c2b..e37380b9 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,3 +1,16 @@ +====================== + What's new in 1.3.13 +====================== + +Support build with MySQL 8 + +Fix decoding tiny/medium/long blobs (#215) + +Remove broken row_seek() and row_tell() APIs (#220) + +Reduce callproc roundtrip time (#223) + + ====================== What's new in 1.3.12 ====================== diff --git a/MySQLdb/__init__.py b/MySQLdb/__init__.py index 038309af..a4a9da94 100644 --- a/MySQLdb/__init__.py +++ b/MySQLdb/__init__.py @@ -1,4 +1,5 @@ -"""MySQLdb - A DB API v2.0 compatible interface to MySQL. +""" +MySQLdb - A DB API v2.0 compatible interface to MySQL. This package is a wrapper around _mysql, which mostly implements the MySQL C API. @@ -10,10 +11,8 @@ For information on how MySQLdb handles type conversion, see the MySQLdb.converters module. - """ -__revision__ = """$Revision$"""[11:-2] from MySQLdb.release import __version__, version_info, __author__ import _mysql @@ -98,6 +97,3 @@ def Connect(*args, **kwargs): 'escape_sequence', 'escape_string', 'get_client_info', 'paramstyle', 'string_literal', 'threadsafety', 'version_info'] - - - diff --git a/metadata.cfg b/metadata.cfg index cf40b262..4dee9616 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,30 +1,7 @@ [metadata] -version: 1.3.12 -version_info: (1,3,12,'final',0) +version: 1.3.13 +version_info: (1,3,13,'final',0) description: Python interface to MySQL -long_description: - ========================= - Python interface to MySQL - ========================= - \n - mysqlclient is a fork of MySQL-python. It adds Python 3 support - and fixed many bugs. - \n - MySQLdb is an interface to the popular MySQL_ database server for - Python. The design goals are: - \n - - Compliance with Python database API version 2.0 [PEP-0249]_ - - Thread-safety - - Thread-friendliness (threads will not block each other) - \n - MySQL-5.5 through 5.7 and Python 2.7, 3.4+ are currently supported. - PyPy is supported too. - \n - MySQLdb is `Free Software`_. - \n - .. _MySQL: http://www.mysql.com/ - .. _`Free Software`: http://www.gnu.org/ - .. [PEP-0249] https://www.python.org/dev/peps/pep-0249/ author: Andy Dustman author_email: farcepest@gmail.com maintainer: INADA Naoki @@ -47,7 +24,6 @@ classifiers: Programming Language :: Python :: 2 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 - Programming Language :: Python :: 3.3 Programming Language :: Python :: 3.4 Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 diff --git a/setup.py b/setup.py index f88a3819..4d2e1b43 100644 --- a/setup.py +++ b/setup.py @@ -1,21 +1,22 @@ #!/usr/bin/env python import os +import io import sys import distutils.errors import setuptools -if not hasattr(sys, "hexversion") or sys.hexversion < 0x02060000: - raise distutils.errors.DistutilsError("Python 2.6 or newer is required") - if os.name == "posix": from setup_posix import get_config else: # assume windows from setup_windows import get_config +with io.open('README.md', encoding='utf-8') as f: + readme = f.read() + metadata, options = get_config() -metadata['ext_modules'] = [ - setuptools.Extension(sources=['_mysql.c'], **options)] -metadata['long_description'] = metadata['long_description'].replace(r'\n', '') +metadata['ext_modules'] = [setuptools.Extension(sources=['_mysql.c'], **options)] +metadata['long_description'] = readme +metadata['long_description_content_type'] = "text/markdown" setuptools.setup(**metadata) From cfff0175bad7d63abca8a88b2fff811c2947c8d0 Mon Sep 17 00:00:00 2001 From: Ed Date: Tue, 2 Oct 2018 06:04:51 +0100 Subject: [PATCH 053/177] Handle mariadb static library called mariadbclient.a (#265) --- setup_posix.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/setup_posix.py b/setup_posix.py index 6e409609..aa4b260b 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -72,17 +72,21 @@ def get_config(): include_dirs = [dequote(i[2:]) for i in mysql_config('include') if i.startswith('-I')] - - # properly handle mysql client libraries that are not called libmysqlclient - client = None - CLIENT_LIST = ['mysqlclient', 'mysqlclient_r', 'mysqld', - 'perconaserverclient', 'perconaserverclient_r'] - for c in CLIENT_LIST: - if c in libraries: - client = c - break - if static: + # properly handle mysql client libraries that are not called libmysqlclient + client = None + CLIENT_LIST = ['mysqlclient', 'mysqlclient_r', 'mysqld', 'mariadb', + 'perconaserverclient', 'perconaserverclient_r'] + for c in CLIENT_LIST: + if c in libraries: + client = c + break + + if client == 'mariadb': + client = 'mariadbclient' + if client is None: + raise ValueError("Couldn't identify mysql client library") + extra_objects.append(os.path.join(library_dirs[0], 'lib%s.a' % client)) if client in libraries: libraries.remove(client) From 3d4d286a10d264907a52347b58419050a785a27b Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Fri, 19 Oct 2018 15:50:23 +0500 Subject: [PATCH 054/177] Fixed float converter (#268) Use exponential notation always to MySQL treat it as float, not decimal. Use enough precision for round-trip value. --- MySQLdb/converters.py | 2 +- tests/capabilities.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index 505a4df0..586f16e0 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -66,7 +66,7 @@ def Unicode2Str(s, d): return s.encode() def Float2Str(o, d): - return '%.15g' % o + return '%.16e' % o def None2NULL(o, d): """Convert None to NULL.""" diff --git a/tests/capabilities.py b/tests/capabilities.py index 31aa398e..4e02f173 100644 --- a/tests/capabilities.py +++ b/tests/capabilities.py @@ -279,3 +279,9 @@ def generator(row,col): ('col1 INT','col2 BLOB'), generator) + def test_DOUBLE(self): + for val in (18014398509481982.0, 0.1): + self.cursor.execute('SELECT %s', (val,)); + result = self.cursor.fetchone()[0] + self.assertEqual(result, val) + self.assertIsInstance(result, float) From 68b9662918577fc05be9610ef4824a00f2b051b0 Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Fri, 19 Oct 2018 15:51:36 +0500 Subject: [PATCH 055/177] Add Decimal converter (#267) --- MySQLdb/converters.py | 1 + tests/capabilities.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index 586f16e0..b16aea50 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -139,5 +139,6 @@ def quote_tuple(t, d): from decimal import Decimal conversions[FIELD_TYPE.DECIMAL] = Decimal conversions[FIELD_TYPE.NEWDECIMAL] = Decimal + conversions[Decimal] = Thing2Str except ImportError: pass diff --git a/tests/capabilities.py b/tests/capabilities.py index 4e02f173..d4635b56 100644 --- a/tests/capabilities.py +++ b/tests/capabilities.py @@ -195,13 +195,18 @@ def generator(row,col): def test_DECIMAL(self): # DECIMAL + from decimal import Decimal def generator(row,col): - from decimal import Decimal return Decimal("%d.%02d" % (row, col)) self.check_data_integrity( ('col1 DECIMAL(5,2)',), generator) + self.cursor.execute('SELECT %s + %s', (Decimal('0.1'), Decimal('0.2'))) + result = self.cursor.fetchone()[0] + self.assertEqual(result, Decimal('0.3')) + self.assertIsInstance(result, Decimal) + def test_DATE(self): ticks = time() def generator(row,col): From 54c69436f45f2c78b3ace754780a1a265e60d430 Mon Sep 17 00:00:00 2001 From: Christian Ferrari <1506336+tiian@users.noreply.github.com> Date: Sat, 20 Oct 2018 05:20:44 +0200 Subject: [PATCH 056/177] Add Connection._get_native_connection Private API for XTA [1] project. [1]: http://www.tiian.org/lixa/XTA.html --- _mysql.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/_mysql.c b/_mysql.c index 24eacb33..a4f6976f 100644 --- a/_mysql.c +++ b/_mysql.c @@ -1686,6 +1686,28 @@ _mysql_ConnectionObject_get_character_set_info( } #endif +static char _mysql_ConnectionObject_get_native_connection__doc__[] = +"Return the internal MYSQL* wrapped in a PyCapsule object.\n\ +NOTE: this is a private API introduced ONLY for XTA integration,\n\ + don't use it for different use cases.\n\ + This method is supported only for XTA integration and support must\n\ + be asked to LIXA project: http://www.tiian.org/lixa/\n\ + Please DO NOT ask support to PyMySQL/mysqlclient-python project.\n\ +"; + +static PyObject * +_mysql_ConnectionObject_get_native_connection( + _mysql_ConnectionObject *self, + PyObject *noargs) +{ + PyObject *result; + check_connection(self); + result = PyCapsule_New(&(self->connection), + "_mysql.connection.native_connection", NULL); + return result; +} + + static char _mysql_get_client_info__doc__[] = "get_client_info() -- Returns a string that represents\n\ the client library version."; @@ -2271,6 +2293,12 @@ static PyMethodDef _mysql_ConnectionObject_methods[] = { _mysql_ConnectionObject_get_character_set_info__doc__ }, #endif + { + "_get_native_connection", + (PyCFunction)_mysql_ConnectionObject_get_native_connection, + METH_NOARGS, + _mysql_ConnectionObject_get_native_connection__doc__ + }, { "close", (PyCFunction)_mysql_ConnectionObject_close, From 4a4978d6b10e6bc7983ad41f92277a635133f80f Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 22 Oct 2018 21:26:57 +0900 Subject: [PATCH 057/177] Add missing checks for connection before calling mysql APIs (#272) Fixes #270 --- _mysql.c | 102 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 55 insertions(+), 47 deletions(-) diff --git a/_mysql.c b/_mysql.c index a4f6976f..cea98203 100644 --- a/_mysql.c +++ b/_mysql.c @@ -69,9 +69,14 @@ typedef struct { PyObject *converter; } _mysql_ConnectionObject; -#define check_connection(c) if (!(c->open)) return _mysql_Exception(c) +#define check_connection(c, func) \ + if (!(c->open)) { \ + PyErr_SetString(_mysql_ProgrammingError, func "() is called for closed connection"); \ + return NULL; \ + }; + #define result_connection(r) ((_mysql_ConnectionObject *)r->conn) -#define check_result_connection(r) check_connection(result_connection(r)) +#define check_result_connection(r, func) check_connection(result_connection(r), func) extern PyTypeObject _mysql_ConnectionObject_Type; @@ -750,6 +755,7 @@ static PyObject * _mysql_ConnectionObject_fileno( _mysql_ConnectionObject *self) { + check_connection(self, "fileno"); return PyInt_FromLong(self->connection.net.fd); } @@ -761,16 +767,11 @@ _mysql_ConnectionObject_close( _mysql_ConnectionObject *self, PyObject *noargs) { - if (self->open) { - Py_BEGIN_ALLOW_THREADS - mysql_close(&(self->connection)); - Py_END_ALLOW_THREADS - self->open = 0; - } else { - PyErr_SetString(_mysql_ProgrammingError, - "closing a closed connection"); - return NULL; - } + check_connection(self, "close"); + Py_BEGIN_ALLOW_THREADS + mysql_close(&(self->connection)); + Py_END_ALLOW_THREADS + self->open = 0; _mysql_ConnectionObject_clear(self); Py_RETURN_NONE; } @@ -786,7 +787,7 @@ _mysql_ConnectionObject_affected_rows( PyObject *noargs) { my_ulonglong ret; - check_connection(self); + check_connection(self, "affected_rows"); ret = mysql_affected_rows(&(self->connection)); if (ret == (my_ulonglong)-1) return PyInt_FromLong(-1); @@ -823,7 +824,7 @@ _mysql_ConnectionObject_dump_debug_info( PyObject *noargs) { int err; - check_connection(self); + check_connection(self, "dump_debug_info"); Py_BEGIN_ALLOW_THREADS err = mysql_dump_debug_info(&(self->connection)); Py_END_ALLOW_THREADS @@ -842,6 +843,7 @@ _mysql_ConnectionObject_autocommit( { int flag, err; if (!PyArg_ParseTuple(args, "i", &flag)) return NULL; + check_connection(self, "autocommit"); Py_BEGIN_ALLOW_THREADS err = mysql_autocommit(&(self->connection), flag); Py_END_ALLOW_THREADS @@ -858,6 +860,7 @@ _mysql_ConnectionObject_get_autocommit( _mysql_ConnectionObject *self, PyObject *args) { + check_connection(self, "get_autocommit"); if (self->connection.server_status & SERVER_STATUS_AUTOCOMMIT) { Py_RETURN_TRUE; } @@ -873,6 +876,7 @@ _mysql_ConnectionObject_commit( PyObject *noargs) { int err; + check_connection(self, "commit"); Py_BEGIN_ALLOW_THREADS err = mysql_commit(&(self->connection)); Py_END_ALLOW_THREADS @@ -890,13 +894,13 @@ _mysql_ConnectionObject_rollback( PyObject *noargs) { int err; + check_connection(self, "rollback"); Py_BEGIN_ALLOW_THREADS err = mysql_rollback(&(self->connection)); Py_END_ALLOW_THREADS if (err) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; -} + Py_RETURN_NONE; +} static char _mysql_ConnectionObject_next_result__doc__[] = "If more query results exist, next_result() reads the next query\n\ @@ -917,6 +921,7 @@ _mysql_ConnectionObject_next_result( PyObject *noargs) { int err; + check_connection(self, "next_result"); Py_BEGIN_ALLOW_THREADS err = mysql_next_result(&(self->connection)); Py_END_ALLOW_THREADS @@ -939,6 +944,7 @@ _mysql_ConnectionObject_set_server_option( int err, flags=0; if (!PyArg_ParseTuple(args, "i", &flags)) return NULL; + check_connection(self, "set_server_option"); Py_BEGIN_ALLOW_THREADS err = mysql_set_server_option(&(self->connection), flags); Py_END_ALLOW_THREADS @@ -963,6 +969,7 @@ _mysql_ConnectionObject_sqlstate( _mysql_ConnectionObject *self, PyObject *noargs) { + check_connection(self, "sqlstate"); return PyString_FromString(mysql_sqlstate(&(self->connection))); } @@ -977,6 +984,7 @@ _mysql_ConnectionObject_warning_count( _mysql_ConnectionObject *self, PyObject *noargs) { + check_connection(self, "warning_count"); return PyInt_FromLong(mysql_warning_count(&(self->connection))); } @@ -991,7 +999,7 @@ _mysql_ConnectionObject_errno( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self); + check_connection(self, "errno"); return PyInt_FromLong((long)mysql_errno(&(self->connection))); } @@ -1006,7 +1014,7 @@ _mysql_ConnectionObject_error( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self); + check_connection(self, "error"); return PyString_FromString(mysql_error(&(self->connection))); } @@ -1249,7 +1257,7 @@ _mysql_ResultObject_describe( PyObject *d; MYSQL_FIELD *fields; unsigned int i, n; - check_result_connection(self); + check_result_connection(self, "describe"); n = mysql_num_fields(self->result); fields = mysql_fetch_fields(self->result); if (!(d = PyTuple_New(n))) return NULL; @@ -1284,7 +1292,7 @@ _mysql_ResultObject_field_flags( PyObject *d; MYSQL_FIELD *fields; unsigned int i, n; - check_result_connection(self); + check_result_connection(self, "field_flags"); n = mysql_num_fields(self->result); fields = mysql_fetch_fields(self->result); if (!(d = PyTuple_New(n))) return NULL; @@ -1523,7 +1531,7 @@ _mysql_ResultObject_fetch_row( if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii:fetch_row", kwlist, &maxrows, &how)) return NULL; - check_result_connection(self); + check_result_connection(self, "fetch_row"); if (how >= (int)sizeof(row_converters)) { PyErr_SetString(PyExc_ValueError, "how out of range"); return NULL; @@ -1592,7 +1600,7 @@ _mysql_ConnectionObject_change_user( if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ss:change_user", kwlist, &user, &pwd, &db)) return NULL; - check_connection(self); + check_connection(self, "change_user"); Py_BEGIN_ALLOW_THREADS r = mysql_change_user(&(self->connection), user, pwd, db); Py_END_ALLOW_THREADS @@ -1612,7 +1620,7 @@ _mysql_ConnectionObject_character_set_name( PyObject *noargs) { const char *s; - check_connection(self); + check_connection(self, "character_set_name"); s = mysql_character_set_name(&(self->connection)); return PyString_FromString(s); } @@ -1630,7 +1638,7 @@ _mysql_ConnectionObject_set_character_set( const char *s; int err; if (!PyArg_ParseTuple(args, "s", &s)) return NULL; - check_connection(self); + check_connection(self, "set_character_set"); Py_BEGIN_ALLOW_THREADS err = mysql_set_character_set(&(self->connection), s); Py_END_ALLOW_THREADS @@ -1669,7 +1677,7 @@ _mysql_ConnectionObject_get_character_set_info( PyObject *result; MY_CHARSET_INFO cs; - check_connection(self); + check_connection(self, "get_character_set_info"); mysql_get_character_set_info(&(self->connection), &cs); if (!(result = PyDict_New())) return NULL; if (cs.csname) @@ -1701,7 +1709,7 @@ _mysql_ConnectionObject_get_native_connection( PyObject *noargs) { PyObject *result; - check_connection(self); + check_connection(self, "_get_native_connection"); result = PyCapsule_New(&(self->connection), "_mysql.connection.native_connection", NULL); return result; @@ -1730,7 +1738,7 @@ _mysql_ConnectionObject_get_host_info( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self); + check_connection(self, "get_host_info"); return PyString_FromString(mysql_get_host_info(&(self->connection))); } @@ -1744,7 +1752,7 @@ _mysql_ConnectionObject_get_proto_info( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self); + check_connection(self, "get_proto_info"); return PyInt_FromLong((long)mysql_get_proto_info(&(self->connection))); } @@ -1758,7 +1766,7 @@ _mysql_ConnectionObject_get_server_info( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self); + check_connection(self, "get_server_info"); return PyString_FromString(mysql_get_server_info(&(self->connection))); } @@ -1774,7 +1782,7 @@ _mysql_ConnectionObject_info( PyObject *noargs) { const char *s; - check_connection(self); + check_connection(self, "info"); s = mysql_info(&(self->connection)); if (s) return PyString_FromString(s); Py_INCREF(Py_None); @@ -1808,7 +1816,7 @@ _mysql_ConnectionObject_insert_id( PyObject *noargs) { my_ulonglong r; - check_connection(self); + check_connection(self, "insert_id"); Py_BEGIN_ALLOW_THREADS r = mysql_insert_id(&(self->connection)); Py_END_ALLOW_THREADS @@ -1827,7 +1835,7 @@ _mysql_ConnectionObject_kill( unsigned long pid; int r; if (!PyArg_ParseTuple(args, "k:kill", &pid)) return NULL; - check_connection(self); + check_connection(self, "kill"); Py_BEGIN_ALLOW_THREADS r = mysql_kill(&(self->connection), pid); Py_END_ALLOW_THREADS @@ -1847,7 +1855,7 @@ _mysql_ConnectionObject_field_count( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self); + check_connection(self, "field_count"); return PyInt_FromLong((long)mysql_field_count(&(self->connection))); } @@ -1859,7 +1867,7 @@ _mysql_ResultObject_num_fields( _mysql_ResultObject *self, PyObject *noargs) { - check_result_connection(self); + check_result_connection(self, "num_fields"); return PyInt_FromLong((long)mysql_num_fields(self->result)); } @@ -1874,7 +1882,7 @@ _mysql_ResultObject_num_rows( _mysql_ResultObject *self, PyObject *noargs) { - check_result_connection(self); + check_result_connection(self, "num_rows"); return PyLong_FromUnsignedLongLong(mysql_num_rows(self->result)); } @@ -1904,7 +1912,7 @@ _mysql_ConnectionObject_ping( { int r, reconnect = -1; if (!PyArg_ParseTuple(args, "|I", &reconnect)) return NULL; - check_connection(self); + check_connection(self, "ping"); if (reconnect != -1) { my_bool recon = (my_bool)reconnect; mysql_options(&self->connection, MYSQL_OPT_RECONNECT, &recon); @@ -1931,7 +1939,7 @@ _mysql_ConnectionObject_query( char *query; int len, r; if (!PyArg_ParseTuple(args, "s#:query", &query, &len)) return NULL; - check_connection(self); + check_connection(self, "query"); Py_BEGIN_ALLOW_THREADS r = mysql_real_query(&(self->connection), query, len); @@ -1955,7 +1963,7 @@ _mysql_ConnectionObject_send_query( int len, r; MYSQL *mysql = &(self->connection); if (!PyArg_ParseTuple(args, "s#:query", &query, &len)) return NULL; - check_connection(self); + check_connection(self, "send_query"); Py_BEGIN_ALLOW_THREADS r = mysql_send_query(mysql, query, len); @@ -1976,7 +1984,7 @@ _mysql_ConnectionObject_read_query_result( { int r; MYSQL *mysql = &(self->connection); - check_connection(self); + check_connection(self, "reqd_query_result"); Py_BEGIN_ALLOW_THREADS r = (int)mysql_read_query_result(mysql); @@ -2006,7 +2014,7 @@ _mysql_ConnectionObject_select_db( char *db; int r; if (!PyArg_ParseTuple(args, "s:select_db", &db)) return NULL; - check_connection(self); + check_connection(self, "select_db"); Py_BEGIN_ALLOW_THREADS r = mysql_select_db(&(self->connection), db); Py_END_ALLOW_THREADS @@ -2026,7 +2034,7 @@ _mysql_ConnectionObject_shutdown( PyObject *noargs) { int r; - check_connection(self); + check_connection(self, "shutdown"); Py_BEGIN_ALLOW_THREADS r = mysql_shutdown(&(self->connection), SHUTDOWN_DEFAULT); Py_END_ALLOW_THREADS @@ -2048,7 +2056,7 @@ _mysql_ConnectionObject_stat( PyObject *noargs) { const char *s; - check_connection(self); + check_connection(self, "stat"); Py_BEGIN_ALLOW_THREADS s = mysql_stat(&(self->connection)); Py_END_ALLOW_THREADS @@ -2070,7 +2078,7 @@ _mysql_ConnectionObject_store_result( PyObject *arglist=NULL, *kwarglist=NULL, *result=NULL; _mysql_ResultObject *r=NULL; - check_connection(self); + check_connection(self, "store_result"); arglist = Py_BuildValue("(OiO)", self, 0, self->converter); if (!arglist) goto error; kwarglist = PyDict_New(); @@ -2108,7 +2116,7 @@ _mysql_ConnectionObject_thread_id( PyObject *noargs) { unsigned long pid; - check_connection(self); + check_connection(self, "thread_id"); Py_BEGIN_ALLOW_THREADS pid = mysql_thread_id(&(self->connection)); Py_END_ALLOW_THREADS @@ -2129,7 +2137,7 @@ _mysql_ConnectionObject_use_result( PyObject *arglist=NULL, *kwarglist=NULL, *result=NULL; _mysql_ResultObject *r=NULL; - check_connection(self); + check_connection(self, "use_result"); arglist = Py_BuildValue("(OiO)", self, 1, self->converter); if (!arglist) return NULL; kwarglist = PyDict_New(); @@ -2187,7 +2195,7 @@ _mysql_ResultObject_data_seek( { unsigned int row; if (!PyArg_ParseTuple(args, "i:data_seek", &row)) return NULL; - check_result_connection(self); + check_result_connection(self, "data_seek"); mysql_data_seek(self->result, row); Py_INCREF(Py_None); return Py_None; From 777b74f53ec2d8fa0022cd7a137d0acde69cd61c Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Tue, 23 Oct 2018 12:16:52 +0500 Subject: [PATCH 058/177] Fix formatting and typo in user_guide.rst (#274) --- doc/user_guide.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/user_guide.rst b/doc/user_guide.rst index a0b752c3..82efe698 100644 --- a/doc/user_guide.rst +++ b/doc/user_guide.rst @@ -434,7 +434,7 @@ conv can be either: * a callable object which takes a string argument (the MySQL - value),' returning a Python value + value), returning a Python value * a sequence of 2-tuples, where the first value is a combination of flags from ``MySQLdb.constants.FLAG``, and the second value @@ -653,7 +653,6 @@ CursorTupleRowsMixIn Causes the cursor to return rows as a tuple of the column values. CursorDictRowsMixIn - Causes the cursor to return rows as a dictionary, where the keys are column names and the values are column values. Note that if the column names are not unique, i.e., you are selecting from two From 09018914e1f271afc1aee2c2f233a59032f9effc Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Tue, 23 Oct 2018 14:38:50 +0500 Subject: [PATCH 059/177] Use shorter format for float converter (#273) --- MySQLdb/converters.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index b16aea50..04029cde 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -66,7 +66,10 @@ def Unicode2Str(s, d): return s.encode() def Float2Str(o, d): - return '%.16e' % o + s = repr(o) + if 'e' not in s: + s += 'e0' + return s def None2NULL(o, d): """Convert None to NULL.""" From 9fb618806bf638fa67f40b9cc3dafc4779237b63 Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Tue, 23 Oct 2018 15:04:18 +0500 Subject: [PATCH 060/177] Fix Connection.client_flag (#266) --- _mysql.c | 2 +- tests/test_MySQLdb_nonstandard.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/_mysql.c b/_mysql.c index cea98203..ca991021 100644 --- a/_mysql.c +++ b/_mysql.c @@ -2491,8 +2491,8 @@ static struct PyMemberDef _mysql_ConnectionObject_memberlist[] = { { "client_flag", T_UINT, - READONLY, offsetof(_mysql_ConnectionObject,connection.client_flag), + READONLY, "Client flags; refer to MySQLdb.constants.CLIENT" }, {NULL} /* Sentinel */ diff --git a/tests/test_MySQLdb_nonstandard.py b/tests/test_MySQLdb_nonstandard.py index 7efe265c..52f6a4fb 100644 --- a/tests/test_MySQLdb_nonstandard.py +++ b/tests/test_MySQLdb_nonstandard.py @@ -92,3 +92,14 @@ def test_server_info(self): self.assertTrue(isinstance(self.conn.get_server_info(), str), "Should return an str.") + def test_client_flag(self): + conn = connection_factory( + use_unicode=True, + client_flag=MySQLdb.constants.CLIENT.FOUND_ROWS) + + self.assertIsInstance(conn.client_flag, (int, MySQLdb.compat.long)) + self.assertTrue(conn.client_flag & MySQLdb.constants.CLIENT.FOUND_ROWS) + with self.assertRaises(TypeError if MySQLdb.compat.PY2 else AttributeError): + conn.client_flag = 0 + + conn.close() From abcc4681fbde0fa2dc46f0b9a53788aff41aaa20 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Fri, 2 Nov 2018 20:42:12 +0900 Subject: [PATCH 061/177] Update INSTALL.rst (#281) --- INSTALL.rst | 29 +++-------------------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/INSTALL.rst b/INSTALL.rst index 616b5902..e61af93c 100644 --- a/INSTALL.rst +++ b/INSTALL.rst @@ -8,40 +8,17 @@ MySQLdb Installation Prerequisites ------------- -+ Python 2.6, 2.7, 3.3 or higher - - * http://www.python.org/ ++ Python 2.7, 3.4 or higher + setuptools * http://pypi.python.org/pypi/setuptools -+ MySQL 5.0 or higher ++ MySQL 5.5 or higher * http://www.mysql.com/downloads/ - * MySQL-4.0 and MySQL-4.1 may work, but not supported. - - * MySQL-5.0 is supported and tested, including stored procedures. - - * MySQL-5.1 is supported (currently a release candidate) but untested. - It should work. - - * Red Hat Linux packages: - - - mysql-devel to compile - - - mysql and/or mysql-devel to run - - * MySQL.com RPM packages: - - - MySQL-devel to compile - - - MySQL-shared if you want to use their shared - library. Otherwise you'll get a statically-linked module, - which may or may not be what you want. - - - MySQL-shared to run if you compiled with MySQL-shared installed + * MySQL-5.0 may work, but not supported. + C compiler From e7fddaea248ed540ae9d19fc33aa75dbf2d37c50 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 5 Nov 2018 20:48:43 +0900 Subject: [PATCH 062/177] Clear cursor._query before query() (#283) --- MySQLdb/cursors.py | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index bc1334b7..9181f237 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -75,6 +75,7 @@ def __init__(self, connection): self._result = None self._warnings = None self.rownumber = None + self._rows = None def close(self): """Close the cursor. No further queries will be possible.""" @@ -172,23 +173,27 @@ def nextset(self): nr = db.next_result() if nr == -1: return None - self._do_get_result() + self._do_get_result(db) self._post_get_result() self._warning_check() return 1 - def _post_get_result(self): pass + def _do_get_result(self, db): + self._result = result = self._get_result() + if result is None: + self.description = self.description_flags = None + else: + self.description = result.describe() + self.description_flags = result.field_flags() - def _do_get_result(self): - db = self._get_db() - self._result = self._get_result() self.rowcount = db.affected_rows() self.rownumber = 0 - self.description = self._result and self._result.describe() or None - self.description_flags = self._result and self._result.field_flags() or None self.lastrowid = db.insert_id() self._warnings = None + def _post_get_result(self): + pass + def setinputsizes(self, *args): """Does nothing, required by DB API.""" @@ -248,7 +253,6 @@ def execute(self, query, args=None): except Exception: exc, value = sys.exc_info()[:2] self.errorhandler(self, exc, value) - self._executed = query if not self._defer_warnings: self._warning_check() return res @@ -364,21 +368,19 @@ def callproc(self, procname, args=()): if isinstance(q, unicode): q = q.encode(db.encoding, 'surrogateescape') self._query(q) - self._executed = q if not self._defer_warnings: self._warning_check() return args - def _do_query(self, q): + def _query(self, q): db = self._get_db() - self._last_executed = q + self._result = None db.query(q) - self._do_get_result() + self._do_get_result(db) + self._post_get_result() + self._executed = q return self.rowcount - def _query(self, q): - return self._do_query(q) - def _fetch_row(self, size=1): if not self._result: return () @@ -408,11 +410,6 @@ class CursorStoreResultMixIn(object): def _get_result(self): return self._get_db().store_result() - def _query(self, q): - rowcount = self._do_query(q) - self._post_get_result() - return rowcount - def _post_get_result(self): self._rows = self._fetch_row(0) self._result = None @@ -481,7 +478,8 @@ class CursorUseResultMixIn(object): _defer_warnings = True - def _get_result(self): return self._get_db().use_result() + def _get_result(self): + return self._get_db().use_result() def fetchone(self): """Fetches a single row from the cursor.""" From 413d3da22e099155c4221033ef84fe0ec73f2a10 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 5 Nov 2018 20:55:16 +0900 Subject: [PATCH 063/177] Update .travis.yml * Test with Python 3.7 * Use xenial --- .travis.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index e1909524..a3b30d31 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,12 @@ -sudo: required -dist: trusty +dist: xenial language: python + +# See aws s3 ls s3://travis-python-archives/binaries/ubuntu/16.04/x86_64/ python: - "nightly" - - "pypy-5.3.1" - - "3.6-dev" + - "pypy3.5" + - "pypy2.7-5.10.0" + - "3.7" - "3.6" - "3.5" - "3.4" @@ -35,5 +37,4 @@ script: after_succes: - codecov - # vim: sw=2 ts=2 sts=2 From 53764264945fb26d5a21dd9000c4ea5eefdc6e2d Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 5 Nov 2018 21:05:29 +0900 Subject: [PATCH 064/177] Return OperationalError for closed connection (#276) libmysqlclient returns CR_SERVER_GONE_ERROR(2006) when using closed connection. But libmariadbclient cause SEGV. For compatibility, emulate CR_SERVER_GONE_ERROR manually. Ref: GH-270 --- _mysql.c | 101 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 54 insertions(+), 47 deletions(-) diff --git a/_mysql.c b/_mysql.c index ca991021..52f3283a 100644 --- a/_mysql.c +++ b/_mysql.c @@ -69,14 +69,13 @@ typedef struct { PyObject *converter; } _mysql_ConnectionObject; -#define check_connection(c, func) \ +#define check_connection(c) \ if (!(c->open)) { \ - PyErr_SetString(_mysql_ProgrammingError, func "() is called for closed connection"); \ - return NULL; \ + return _mysql_Exception(c); \ }; #define result_connection(r) ((_mysql_ConnectionObject *)r->conn) -#define check_result_connection(r, func) check_connection(result_connection(r), func) +#define check_result_connection(r) check_connection(result_connection(r)) extern PyTypeObject _mysql_ConnectionObject_Type; @@ -120,7 +119,15 @@ _mysql_Exception(_mysql_ConnectionObject *c) Py_DECREF(t); return NULL; } - merr = mysql_errno(&(c->connection)); + if (!(c->open)) { + /* GH-270: When connection is closed, accessing the c->connection + * object may cause SEGV. + */ + merr = CR_SERVER_GONE_ERROR; + } + else { + merr = mysql_errno(&(c->connection)); + } switch (merr) { case 0: e = _mysql_InterfaceError; @@ -755,7 +762,7 @@ static PyObject * _mysql_ConnectionObject_fileno( _mysql_ConnectionObject *self) { - check_connection(self, "fileno"); + check_connection(self); return PyInt_FromLong(self->connection.net.fd); } @@ -767,7 +774,7 @@ _mysql_ConnectionObject_close( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self, "close"); + check_connection(self); Py_BEGIN_ALLOW_THREADS mysql_close(&(self->connection)); Py_END_ALLOW_THREADS @@ -787,7 +794,7 @@ _mysql_ConnectionObject_affected_rows( PyObject *noargs) { my_ulonglong ret; - check_connection(self, "affected_rows"); + check_connection(self); ret = mysql_affected_rows(&(self->connection)); if (ret == (my_ulonglong)-1) return PyInt_FromLong(-1); @@ -824,7 +831,7 @@ _mysql_ConnectionObject_dump_debug_info( PyObject *noargs) { int err; - check_connection(self, "dump_debug_info"); + check_connection(self); Py_BEGIN_ALLOW_THREADS err = mysql_dump_debug_info(&(self->connection)); Py_END_ALLOW_THREADS @@ -843,7 +850,7 @@ _mysql_ConnectionObject_autocommit( { int flag, err; if (!PyArg_ParseTuple(args, "i", &flag)) return NULL; - check_connection(self, "autocommit"); + check_connection(self); Py_BEGIN_ALLOW_THREADS err = mysql_autocommit(&(self->connection), flag); Py_END_ALLOW_THREADS @@ -860,7 +867,7 @@ _mysql_ConnectionObject_get_autocommit( _mysql_ConnectionObject *self, PyObject *args) { - check_connection(self, "get_autocommit"); + check_connection(self); if (self->connection.server_status & SERVER_STATUS_AUTOCOMMIT) { Py_RETURN_TRUE; } @@ -876,7 +883,7 @@ _mysql_ConnectionObject_commit( PyObject *noargs) { int err; - check_connection(self, "commit"); + check_connection(self); Py_BEGIN_ALLOW_THREADS err = mysql_commit(&(self->connection)); Py_END_ALLOW_THREADS @@ -894,7 +901,7 @@ _mysql_ConnectionObject_rollback( PyObject *noargs) { int err; - check_connection(self, "rollback"); + check_connection(self); Py_BEGIN_ALLOW_THREADS err = mysql_rollback(&(self->connection)); Py_END_ALLOW_THREADS @@ -921,7 +928,7 @@ _mysql_ConnectionObject_next_result( PyObject *noargs) { int err; - check_connection(self, "next_result"); + check_connection(self); Py_BEGIN_ALLOW_THREADS err = mysql_next_result(&(self->connection)); Py_END_ALLOW_THREADS @@ -944,7 +951,7 @@ _mysql_ConnectionObject_set_server_option( int err, flags=0; if (!PyArg_ParseTuple(args, "i", &flags)) return NULL; - check_connection(self, "set_server_option"); + check_connection(self); Py_BEGIN_ALLOW_THREADS err = mysql_set_server_option(&(self->connection), flags); Py_END_ALLOW_THREADS @@ -969,7 +976,7 @@ _mysql_ConnectionObject_sqlstate( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self, "sqlstate"); + check_connection(self); return PyString_FromString(mysql_sqlstate(&(self->connection))); } @@ -984,7 +991,7 @@ _mysql_ConnectionObject_warning_count( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self, "warning_count"); + check_connection(self); return PyInt_FromLong(mysql_warning_count(&(self->connection))); } @@ -999,7 +1006,7 @@ _mysql_ConnectionObject_errno( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self, "errno"); + check_connection(self); return PyInt_FromLong((long)mysql_errno(&(self->connection))); } @@ -1014,7 +1021,7 @@ _mysql_ConnectionObject_error( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self, "error"); + check_connection(self); return PyString_FromString(mysql_error(&(self->connection))); } @@ -1257,7 +1264,7 @@ _mysql_ResultObject_describe( PyObject *d; MYSQL_FIELD *fields; unsigned int i, n; - check_result_connection(self, "describe"); + check_result_connection(self); n = mysql_num_fields(self->result); fields = mysql_fetch_fields(self->result); if (!(d = PyTuple_New(n))) return NULL; @@ -1292,7 +1299,7 @@ _mysql_ResultObject_field_flags( PyObject *d; MYSQL_FIELD *fields; unsigned int i, n; - check_result_connection(self, "field_flags"); + check_result_connection(self); n = mysql_num_fields(self->result); fields = mysql_fetch_fields(self->result); if (!(d = PyTuple_New(n))) return NULL; @@ -1531,7 +1538,7 @@ _mysql_ResultObject_fetch_row( if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii:fetch_row", kwlist, &maxrows, &how)) return NULL; - check_result_connection(self, "fetch_row"); + check_result_connection(self); if (how >= (int)sizeof(row_converters)) { PyErr_SetString(PyExc_ValueError, "how out of range"); return NULL; @@ -1600,7 +1607,7 @@ _mysql_ConnectionObject_change_user( if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ss:change_user", kwlist, &user, &pwd, &db)) return NULL; - check_connection(self, "change_user"); + check_connection(self); Py_BEGIN_ALLOW_THREADS r = mysql_change_user(&(self->connection), user, pwd, db); Py_END_ALLOW_THREADS @@ -1620,7 +1627,7 @@ _mysql_ConnectionObject_character_set_name( PyObject *noargs) { const char *s; - check_connection(self, "character_set_name"); + check_connection(self); s = mysql_character_set_name(&(self->connection)); return PyString_FromString(s); } @@ -1638,7 +1645,7 @@ _mysql_ConnectionObject_set_character_set( const char *s; int err; if (!PyArg_ParseTuple(args, "s", &s)) return NULL; - check_connection(self, "set_character_set"); + check_connection(self); Py_BEGIN_ALLOW_THREADS err = mysql_set_character_set(&(self->connection), s); Py_END_ALLOW_THREADS @@ -1677,7 +1684,7 @@ _mysql_ConnectionObject_get_character_set_info( PyObject *result; MY_CHARSET_INFO cs; - check_connection(self, "get_character_set_info"); + check_connection(self); mysql_get_character_set_info(&(self->connection), &cs); if (!(result = PyDict_New())) return NULL; if (cs.csname) @@ -1709,7 +1716,7 @@ _mysql_ConnectionObject_get_native_connection( PyObject *noargs) { PyObject *result; - check_connection(self, "_get_native_connection"); + check_connection(self); result = PyCapsule_New(&(self->connection), "_mysql.connection.native_connection", NULL); return result; @@ -1738,7 +1745,7 @@ _mysql_ConnectionObject_get_host_info( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self, "get_host_info"); + check_connection(self); return PyString_FromString(mysql_get_host_info(&(self->connection))); } @@ -1752,7 +1759,7 @@ _mysql_ConnectionObject_get_proto_info( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self, "get_proto_info"); + check_connection(self); return PyInt_FromLong((long)mysql_get_proto_info(&(self->connection))); } @@ -1766,7 +1773,7 @@ _mysql_ConnectionObject_get_server_info( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self, "get_server_info"); + check_connection(self); return PyString_FromString(mysql_get_server_info(&(self->connection))); } @@ -1782,7 +1789,7 @@ _mysql_ConnectionObject_info( PyObject *noargs) { const char *s; - check_connection(self, "info"); + check_connection(self); s = mysql_info(&(self->connection)); if (s) return PyString_FromString(s); Py_INCREF(Py_None); @@ -1816,7 +1823,7 @@ _mysql_ConnectionObject_insert_id( PyObject *noargs) { my_ulonglong r; - check_connection(self, "insert_id"); + check_connection(self); Py_BEGIN_ALLOW_THREADS r = mysql_insert_id(&(self->connection)); Py_END_ALLOW_THREADS @@ -1835,7 +1842,7 @@ _mysql_ConnectionObject_kill( unsigned long pid; int r; if (!PyArg_ParseTuple(args, "k:kill", &pid)) return NULL; - check_connection(self, "kill"); + check_connection(self); Py_BEGIN_ALLOW_THREADS r = mysql_kill(&(self->connection), pid); Py_END_ALLOW_THREADS @@ -1855,7 +1862,7 @@ _mysql_ConnectionObject_field_count( _mysql_ConnectionObject *self, PyObject *noargs) { - check_connection(self, "field_count"); + check_connection(self); return PyInt_FromLong((long)mysql_field_count(&(self->connection))); } @@ -1867,7 +1874,7 @@ _mysql_ResultObject_num_fields( _mysql_ResultObject *self, PyObject *noargs) { - check_result_connection(self, "num_fields"); + check_result_connection(self); return PyInt_FromLong((long)mysql_num_fields(self->result)); } @@ -1882,7 +1889,7 @@ _mysql_ResultObject_num_rows( _mysql_ResultObject *self, PyObject *noargs) { - check_result_connection(self, "num_rows"); + check_result_connection(self); return PyLong_FromUnsignedLongLong(mysql_num_rows(self->result)); } @@ -1912,7 +1919,7 @@ _mysql_ConnectionObject_ping( { int r, reconnect = -1; if (!PyArg_ParseTuple(args, "|I", &reconnect)) return NULL; - check_connection(self, "ping"); + check_connection(self); if (reconnect != -1) { my_bool recon = (my_bool)reconnect; mysql_options(&self->connection, MYSQL_OPT_RECONNECT, &recon); @@ -1939,7 +1946,7 @@ _mysql_ConnectionObject_query( char *query; int len, r; if (!PyArg_ParseTuple(args, "s#:query", &query, &len)) return NULL; - check_connection(self, "query"); + check_connection(self); Py_BEGIN_ALLOW_THREADS r = mysql_real_query(&(self->connection), query, len); @@ -1963,7 +1970,7 @@ _mysql_ConnectionObject_send_query( int len, r; MYSQL *mysql = &(self->connection); if (!PyArg_ParseTuple(args, "s#:query", &query, &len)) return NULL; - check_connection(self, "send_query"); + check_connection(self); Py_BEGIN_ALLOW_THREADS r = mysql_send_query(mysql, query, len); @@ -1984,7 +1991,7 @@ _mysql_ConnectionObject_read_query_result( { int r; MYSQL *mysql = &(self->connection); - check_connection(self, "reqd_query_result"); + check_connection(self); Py_BEGIN_ALLOW_THREADS r = (int)mysql_read_query_result(mysql); @@ -2014,7 +2021,7 @@ _mysql_ConnectionObject_select_db( char *db; int r; if (!PyArg_ParseTuple(args, "s:select_db", &db)) return NULL; - check_connection(self, "select_db"); + check_connection(self); Py_BEGIN_ALLOW_THREADS r = mysql_select_db(&(self->connection), db); Py_END_ALLOW_THREADS @@ -2034,7 +2041,7 @@ _mysql_ConnectionObject_shutdown( PyObject *noargs) { int r; - check_connection(self, "shutdown"); + check_connection(self); Py_BEGIN_ALLOW_THREADS r = mysql_shutdown(&(self->connection), SHUTDOWN_DEFAULT); Py_END_ALLOW_THREADS @@ -2056,7 +2063,7 @@ _mysql_ConnectionObject_stat( PyObject *noargs) { const char *s; - check_connection(self, "stat"); + check_connection(self); Py_BEGIN_ALLOW_THREADS s = mysql_stat(&(self->connection)); Py_END_ALLOW_THREADS @@ -2078,7 +2085,7 @@ _mysql_ConnectionObject_store_result( PyObject *arglist=NULL, *kwarglist=NULL, *result=NULL; _mysql_ResultObject *r=NULL; - check_connection(self, "store_result"); + check_connection(self); arglist = Py_BuildValue("(OiO)", self, 0, self->converter); if (!arglist) goto error; kwarglist = PyDict_New(); @@ -2116,7 +2123,7 @@ _mysql_ConnectionObject_thread_id( PyObject *noargs) { unsigned long pid; - check_connection(self, "thread_id"); + check_connection(self); Py_BEGIN_ALLOW_THREADS pid = mysql_thread_id(&(self->connection)); Py_END_ALLOW_THREADS @@ -2137,7 +2144,7 @@ _mysql_ConnectionObject_use_result( PyObject *arglist=NULL, *kwarglist=NULL, *result=NULL; _mysql_ResultObject *r=NULL; - check_connection(self, "use_result"); + check_connection(self); arglist = Py_BuildValue("(OiO)", self, 1, self->converter); if (!arglist) return NULL; kwarglist = PyDict_New(); @@ -2195,7 +2202,7 @@ _mysql_ResultObject_data_seek( { unsigned int row; if (!PyArg_ParseTuple(args, "i:data_seek", &row)) return NULL; - check_result_connection(self, "data_seek"); + check_result_connection(self); mysql_data_seek(self->result, row); Py_INCREF(Py_None); return Py_None; From 21170a763941d6f9cd736f6060ccef94358a326d Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 5 Nov 2018 21:28:05 +0900 Subject: [PATCH 065/177] Deprecate waiter (#285) --- MySQLdb/connections.py | 14 ++++++++------ _mysql.c | 2 +- samples/waiter_gevent.py | 24 ------------------------ samples/waiter_meinheld.py | 19 ------------------- 4 files changed, 9 insertions(+), 50 deletions(-) delete mode 100644 samples/waiter_gevent.py delete mode 100644 samples/waiter_meinheld.py diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 8375cd0a..10a0bf77 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -200,6 +200,10 @@ class object, used to create cursors (keyword only) # PEP-249 requires autocommit to be initially off autocommit = kwargs2.pop('autocommit', False) self.waiter = kwargs2.pop('waiter', None) + if self.waiter: + from warnings import warn + warn("waiter is deprecated and will be removed in 1.4.", + DeprecationWarning, 2) super(Connection, self).__init__(*args, **kwargs2) self.cursorclass = cursorclass @@ -325,12 +329,10 @@ def literal(self, o): return s def begin(self): - """Explicitly begin a connection. Non-standard. - DEPRECATED: Will be removed in 1.3. - Use an SQL BEGIN statement instead.""" - from warnings import warn - warn("begin() is non-standard and will be removed in 1.4", - DeprecationWarning, 2) + """Explicitly begin a connection. + + This method is not used when autocommit=False (default). + """ self.query("BEGIN") if not hasattr(_mysql.connection, 'warning_count'): diff --git a/_mysql.c b/_mysql.c index 52f3283a..bf85704e 100644 --- a/_mysql.c +++ b/_mysql.c @@ -756,7 +756,7 @@ static int _mysql_ConnectionObject_clear( } static char _mysql_ConnectionObject_fileno__doc__[] = -"Return underlaying fd for connection"; +"Return underlaying fd for connection (deprecated)"; static PyObject * _mysql_ConnectionObject_fileno( diff --git a/samples/waiter_gevent.py b/samples/waiter_gevent.py deleted file mode 100644 index 698b9158..00000000 --- a/samples/waiter_gevent.py +++ /dev/null @@ -1,24 +0,0 @@ -from __future__ import print_function -"""Demo using Gevent with mysqlclient.""" - -import gevent.hub -import MySQLdb - - -def gevent_waiter(fd, hub=gevent.hub.get_hub()): - hub.wait(hub.loop.io(fd, 1)) - - -def f(n): - conn = MySQLdb.connect(user='root', waiter=gevent_waiter) - cur = conn.cursor() - cur.execute("SELECT SLEEP(%s)", (n,)) - cur.execute("SELECT 1+%s", (n,)) - print(cur.fetchall()[0]) - - -gevent.spawn(f, 1) -gevent.spawn(f, 2) -gevent.spawn(f, 3) -gevent.spawn(f, 4) -gevent.sleep(5) diff --git a/samples/waiter_meinheld.py b/samples/waiter_meinheld.py deleted file mode 100644 index 838c9153..00000000 --- a/samples/waiter_meinheld.py +++ /dev/null @@ -1,19 +0,0 @@ -import meinheld -import MySQLdb - - -def meinheld_waiter(fd): - meinheld.server.trampoline(fd, read=True, timeout=10) - - -def app(env, start): - cont = b"Hello, World\n" - conn = MySQLdb.connect(user="root", waiter=meinheld_waiter) - cur = conn.cursor() - cur.execute("SELECT SLEEP(2)") - start(b"200 OK", [('Content-Type', 'text/plain'), ('Content-Length', str(len(cont)))]) - return [cont] - - -meinheld.server.listen(("0.0.0.0", 8080)) -meinheld.server.run(app) From 9f71220b9271109a7ec0b8d858a210d6fe7d742a Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Wed, 7 Nov 2018 12:46:02 +0500 Subject: [PATCH 066/177] Fix conversion of decimals with exponent (#286) --- MySQLdb/converters.py | 4 +++- tests/capabilities.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index 04029cde..5e28005d 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -82,6 +82,8 @@ def Thing2Literal(o, d): that method when the connection is created.""" return string_literal(o, d) +def Decimal2Literal(o, d): + return format(o, 'f') def char_array(s): return array.array('c', s) @@ -142,6 +144,6 @@ def quote_tuple(t, d): from decimal import Decimal conversions[FIELD_TYPE.DECIMAL] = Decimal conversions[FIELD_TYPE.NEWDECIMAL] = Decimal - conversions[Decimal] = Thing2Str + conversions[Decimal] = Decimal2Literal except ImportError: pass diff --git a/tests/capabilities.py b/tests/capabilities.py index d4635b56..c341b3c9 100644 --- a/tests/capabilities.py +++ b/tests/capabilities.py @@ -202,6 +202,12 @@ def generator(row,col): ('col1 DECIMAL(5,2)',), generator) + val = Decimal('1.11111111111111119E-7') + self.cursor.execute('SELECT %s', (val,)) + result = self.cursor.fetchone()[0] + self.assertEqual(result, val) + self.assertIsInstance(result, Decimal) + self.cursor.execute('SELECT %s + %s', (Decimal('0.1'), Decimal('0.2'))) result = self.cursor.fetchone()[0] self.assertEqual(result, Decimal('0.3')) From 01f820991158a2f6c13c2fc88a7c55f21be6282f Mon Sep 17 00:00:00 2001 From: Gatsby Lee Date: Thu, 8 Nov 2018 23:12:45 -0800 Subject: [PATCH 067/177] Fix typo in _mysql.c (#287) --- _mysql.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_mysql.c b/_mysql.c index bf85704e..71f4509c 100644 --- a/_mysql.c +++ b/_mysql.c @@ -99,7 +99,7 @@ static int _mysql_server_init_done = 0; #endif /* According to https://dev.mysql.com/doc/refman/5.1/en/mysql-options.html - The MYSQL_OPT_READ_TIMEOUT apear in the version 5.1.12 */ + The MYSQL_OPT_READ_TIMEOUT appear in the version 5.1.12 */ #if MYSQL_VERSION_ID > 50112 #define HAVE_MYSQL_OPT_TIMEOUTS 1 #endif From 45bf8797d1bd90eb1ea2db577ba8c888b3c89099 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 4 Dec 2018 18:23:35 +0900 Subject: [PATCH 068/177] Remove HAVE_OPENSSL (#292) Assume ssl is always available. --- _mysql.c | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/_mysql.c b/_mysql.c index 71f4509c..3ee26c8b 100644 --- a/_mysql.c +++ b/_mysql.c @@ -94,10 +94,6 @@ extern PyTypeObject _mysql_ResultObject_Type; static int _mysql_server_init_done = 0; #define check_server_init(x) if (!_mysql_server_init_done) { if (mysql_server_init(0, NULL, NULL)) { _mysql_Exception(NULL); return x; } else { _mysql_server_init_done = 1;} } -#if MYSQL_VERSION_ID >= 50500 -#define HAVE_OPENSSL 1 -#endif - /* According to https://dev.mysql.com/doc/refman/5.1/en/mysql-options.html The MYSQL_OPT_READ_TIMEOUT appear in the version 5.1.12 */ #if MYSQL_VERSION_ID > 50112 @@ -502,12 +498,10 @@ _mysql_ConnectionObject_Initialize( MYSQL *conn = NULL; PyObject *conv = NULL; PyObject *ssl = NULL; -#if HAVE_OPENSSL char *key = NULL, *cert = NULL, *ca = NULL, *capath = NULL, *cipher = NULL; PyObject *ssl_keepref[5] = {NULL}; int n_ssl_keepref = 0; -#endif char *host = NULL, *user = NULL, *passwd = NULL, *db = NULL, *unix_socket = NULL; unsigned int port = 0; @@ -571,18 +565,12 @@ _mysql_ConnectionObject_Initialize( #endif if (ssl) { -#if HAVE_OPENSSL PyObject *value = NULL; _stringsuck(ca, value, ssl); _stringsuck(capath, value, ssl); _stringsuck(cert, value, ssl); _stringsuck(key, value, ssl); _stringsuck(cipher, value, ssl); -#else - PyErr_SetString(_mysql_NotSupportedError, - "client library does not have SSL support"); - return -1; -#endif } Py_BEGIN_ALLOW_THREADS ; @@ -620,18 +608,15 @@ _mysql_ConnectionObject_Initialize( if (local_infile != -1) mysql_options(&(self->connection), MYSQL_OPT_LOCAL_INFILE, (char *) &local_infile); -#if HAVE_OPENSSL if (ssl) { mysql_ssl_set(&(self->connection), key, cert, ca, capath, cipher); } -#endif conn = mysql_real_connect(&(self->connection), host, user, passwd, db, port, unix_socket, client_flag); Py_END_ALLOW_THREADS ; -#if HAVE_OPENSSL if (ssl) { int i; for (i=0; i Date: Tue, 4 Dec 2018 19:05:25 +0900 Subject: [PATCH 069/177] 1.3.14 --- HISTORY.rst | 24 ++++++++++++++++++++++++ metadata.cfg | 5 +++-- setup.py | 2 -- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index e37380b9..dd3bbaeb 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,3 +1,27 @@ +====================== + What's new in 1.3.14 +====================== + +Release: 2018-12-04 + +* Support static linking of MariaDB Connector/C (#265) + +* Better converter for Decimal and Float (#267, #268, #273, #286) + +* Add ``Connection._get_native_connection`` for XTA project (#269) + +* Fix SEGV on MariaDB Connector/C when ``Connection.close()`` is called + for closed connection. (#270, #272, #276) + +* Fix ``Connection.client_flag`` (#266) + +* Fix SSCursor may raise same exception twice (#282) + +* ``waiter`` option is now deprecated. (#285) + +* Fixed SSL support is not detected when built with MySQL < 5.1 (#291) + + ====================== What's new in 1.3.13 ====================== diff --git a/metadata.cfg b/metadata.cfg index 4dee9616..6e05d35d 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.3.13 -version_info: (1,3,13,'final',0) +version: 1.3.14 +version_info: (1,3,14,'final',0) description: Python interface to MySQL author: Andy Dustman author_email: farcepest@gmail.com @@ -27,6 +27,7 @@ classifiers: Programming Language :: Python :: 3.4 Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 Topic :: Database Topic :: Database :: Database Engines/Servers py_modules: diff --git a/setup.py b/setup.py index 4d2e1b43..ca4b362e 100644 --- a/setup.py +++ b/setup.py @@ -2,9 +2,7 @@ import os import io -import sys -import distutils.errors import setuptools if os.name == "posix": From 354b60145df8c4edfdc70af8b88080eabbf25d35 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 29 Oct 2018 19:33:43 +0900 Subject: [PATCH 070/177] Remove threadsafe and embedded build options --- INSTALL.rst | 23 ++----- _mysql.c | 149 +-------------------------------------------- doc/FAQ.rst | 7 --- doc/user_guide.rst | 27 -------- setup_posix.py | 23 +++---- site.cfg | 7 +-- 6 files changed, 15 insertions(+), 221 deletions(-) diff --git a/INSTALL.rst b/INSTALL.rst index e61af93c..2588b99e 100644 --- a/INSTALL.rst +++ b/INSTALL.rst @@ -42,20 +42,10 @@ Depending on which version of MySQL you have, you may have the option of using three different client libraries. To select the client library, edit the [options] section of site.cfg: - embedded - use embedded server library (libmysqld) if True; otherwise use - one of the client libraries (default). - - threadsafe - thread-safe client library (libmysqlclient_r) if True (default); - otherwise use non-thread-safe (libmysqlclient). You should - always use the thread-safe library if you have the option; - otherwise you *may* have problems. - static if True, try to link against a static library; otherwise link - against dynamic libraries (default). You may need static linking - to use the embedded server. + against dynamic libraries (default). You may need static linking + to use the embedded server. This option doesn't work for MySQL>5.6 since libmysqlclient requires libstdc++. If you want to use, add `-lstdc++` to mysql_config manually. @@ -130,7 +120,7 @@ Debian GNU/Linux Packaged as `python-mysqldb`_:: - # apt-get install python-mysqldb + # apt-get install python-mysqldb Or use Synaptic. @@ -148,9 +138,9 @@ Gentoo Linux Packaged as `mysql-python`_. :: - # emerge sync - # emerge mysql-python - # emerge zmysqlda # if you use Zope + # emerge sync + # emerge mysql-python + # emerge zmysqlda # if you use Zope .. _`mysql-python`: https://packages.gentoo.org/packages/search?q=mysql-python @@ -169,4 +159,3 @@ GPL or the original license based on Python 1.5.2's license. :Author: Andy Dustman -:Revision: $Id$ diff --git a/_mysql.c b/_mysql.c index 3ee26c8b..430b149b 100644 --- a/_mysql.c +++ b/_mysql.c @@ -91,8 +91,6 @@ typedef struct { extern PyTypeObject _mysql_ResultObject_Type; -static int _mysql_server_init_done = 0; -#define check_server_init(x) if (!_mysql_server_init_done) { if (mysql_server_init(0, NULL, NULL)) { _mysql_Exception(NULL); return x; } else { _mysql_server_init_done = 1;} } /* According to https://dev.mysql.com/doc/refman/5.1/en/mysql-options.html The MYSQL_OPT_READ_TIMEOUT appear in the version 5.1.12 */ @@ -107,14 +105,6 @@ _mysql_Exception(_mysql_ConnectionObject *c) int merr; if (!(t = PyTuple_New(2))) return NULL; - if (!_mysql_server_init_done) { - e = _mysql_InternalError; - PyTuple_SET_ITEM(t, 0, PyInt_FromLong(-1L)); - PyTuple_SET_ITEM(t, 1, PyString_FromString("server not initialized")); - PyErr_SetObject(e, t); - Py_DECREF(t); - return NULL; - } if (!(c->open)) { /* GH-270: When connection is closed, accessing the c->connection * object may cause SEGV. @@ -219,124 +209,6 @@ _mysql_Exception(_mysql_ConnectionObject *c) return NULL; } -static char _mysql_server_init__doc__[] = -"Initialize embedded server. If this client is not linked against\n\ -the embedded server library, this function does nothing.\n\ -\n\ -args -- sequence of command-line arguments\n\ -groups -- sequence of groups to use in defaults files\n\ -"; - -static PyObject *_mysql_server_init( - PyObject *self, - PyObject *args, - PyObject *kwargs) { - static char *kwlist[] = {"args", "groups", NULL}; - char **cmd_args_c=NULL, **groups_c=NULL, *s; - int cmd_argc=0, i, groupc; - PyObject *cmd_args=NULL, *groups=NULL, *ret=NULL, *item; - - if (_mysql_server_init_done) { - PyErr_SetString(_mysql_ProgrammingError, - "already initialized"); - return NULL; - } - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", kwlist, - &cmd_args, &groups)) - return NULL; - - if (cmd_args) { - if (!PySequence_Check(cmd_args)) { - PyErr_SetString(PyExc_TypeError, - "args must be a sequence"); - goto finish; - } - cmd_argc = PySequence_Size(cmd_args); - if (cmd_argc == -1) { - PyErr_SetString(PyExc_TypeError, - "args could not be sized"); - goto finish; - } - cmd_args_c = (char **) PyMem_Malloc(cmd_argc*sizeof(char *)); - for (i=0; i< cmd_argc; i++) { - item = PySequence_GetItem(cmd_args, i); -#ifdef IS_PY3K - s = PyUnicode_AsUTF8(item); -#else - s = PyString_AsString(item); -#endif - - Py_DECREF(item); - if (!s) { - PyErr_SetString(PyExc_TypeError, - "args must contain strings"); - goto finish; - } - cmd_args_c[i] = s; - } - } - if (groups) { - if (!PySequence_Check(groups)) { - PyErr_SetString(PyExc_TypeError, - "groups must be a sequence"); - goto finish; - } - groupc = PySequence_Size(groups); - if (groupc == -1) { - PyErr_SetString(PyExc_TypeError, - "groups could not be sized"); - goto finish; - } - groups_c = (char **) PyMem_Malloc((1+groupc)*sizeof(char *)); - for (i=0; i< groupc; i++) { - item = PySequence_GetItem(groups, i); -#ifdef IS_PY3K - s = PyUnicode_AsUTF8(item); -#else - s = PyString_AsString(item); -#endif - Py_DECREF(item); - if (!s) { - PyErr_SetString(PyExc_TypeError, - "groups must contain strings"); - goto finish; - } - groups_c[i] = s; - } - groups_c[groupc] = (char *)NULL; - } - /* even though this may block, don't give up the interpreter lock - so that the server can't be initialized multiple times. */ - if (mysql_server_init(cmd_argc, cmd_args_c, groups_c)) { - _mysql_Exception(NULL); - goto finish; - } - ret = Py_None; - Py_INCREF(Py_None); - _mysql_server_init_done = 1; - finish: - PyMem_Free(groups_c); - PyMem_Free(cmd_args_c); - return ret; -} - -static char _mysql_server_end__doc__[] = -"Shut down embedded server. If not using an embedded server, this\n\ -does nothing."; - -static PyObject *_mysql_server_end( - PyObject *self, - PyObject *args) { - if (_mysql_server_init_done) { - mysql_server_end(); - _mysql_server_init_done = 0; - Py_INCREF(Py_None); - return Py_None; - } - return _mysql_Exception(NULL); -} - static char _mysql_thread_safe__doc__[] = "Indicates whether the client is compiled as thread-safe."; @@ -344,10 +216,7 @@ static PyObject *_mysql_thread_safe( PyObject *self, PyObject *noargs) { - PyObject *flag; - check_server_init(NULL); - if (!(flag=PyInt_FromLong((long)mysql_thread_safe()))) return NULL; - return flag; + return PyInt_FromLong((long)mysql_thread_safe()); } static char _mysql_ResultObject__doc__[] = @@ -530,7 +399,6 @@ _mysql_ConnectionObject_Initialize( self->converter = NULL; self->open = 0; - check_server_init(-1); if (!PyArg_ParseTupleAndKeywords(args, kwargs, #ifdef HAVE_MYSQL_OPT_TIMEOUTS @@ -1029,7 +897,6 @@ _mysql_escape_string( str = PyBytes_FromStringAndSize((char *) NULL, size*2+1); if (!str) return PyErr_NoMemory(); out = PyBytes_AS_STRING(str); - check_server_init(NULL); if (self && PyModule_Check((PyObject*)self)) self = NULL; @@ -1090,7 +957,6 @@ _mysql_string_literal( return PyErr_NoMemory(); } out = PyBytes_AS_STRING(str); - check_server_init(NULL); if (self && self->open) { #if MYSQL_VERSION_ID >= 50707 && !defined(MARIADB_BASE_VERSION) && !defined(MARIADB_VERSION_ID) len = mysql_real_escape_string_quote(&(self->connection), out+1, in, size, '\''); @@ -1715,7 +1581,6 @@ _mysql_get_client_info( PyObject *self, PyObject *noargs) { - check_server_init(NULL); return PyString_FromString(mysql_get_client_info()); } @@ -2785,18 +2650,6 @@ _mysql_methods[] = { METH_NOARGS, _mysql_thread_safe__doc__ }, - { - "server_init", - (PyCFunction)_mysql_server_init, - METH_VARARGS | METH_KEYWORDS, - _mysql_server_init__doc__ - }, - { - "server_end", - (PyCFunction)_mysql_server_end, - METH_VARARGS, - _mysql_server_end__doc__ - }, {NULL, NULL} /* sentinel */ }; diff --git a/doc/FAQ.rst b/doc/FAQ.rst index 59c6b93d..21e00b9b 100644 --- a/doc/FAQ.rst +++ b/doc/FAQ.rst @@ -9,13 +9,6 @@ Build Errors ------------ - ld: fatal: library -lmysqlclient_r: not found - -mysqlclient_r is the thread-safe library. It's not available on -all platforms, or all installations, apparently. You'll need to -reconfigure site.cfg (in MySQLdb-1.2.1 and newer) to have -threadsafe = False. - mysql.h: No such file or directory This almost always mean you don't have development packages diff --git a/doc/user_guide.rst b/doc/user_guide.rst index 82efe698..d686b786 100644 --- a/doc/user_guide.rst +++ b/doc/user_guide.rst @@ -680,33 +680,6 @@ SSDictCursor Like ``SSCursor`` except it returns rows as dictionaries. -Embedded Server ---------------- - -Instead of connecting to a stand-alone server over the network, -the embedded server support lets you run a full server right in -your Python code or application server. - -If you have built MySQLdb with embedded server support, there -are two additional functions you will need to make use of: - - server_init(args, groups) - Initialize embedded server. If this client is not linked against - the embedded server library, this function does nothing. - - args - sequence of command-line arguments - groups - sequence of groups to use in defaults files - - server_end() - Shut down embedded server. If not using an embedded server, this - does nothing. - -See the MySQL documentation for more information on the embedded -server. - - :Title: MySQLdb: a Python interface for MySQL :Author: Andy Dustman diff --git a/setup_posix.py b/setup_posix.py index aa4b260b..7b86ec73 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -2,7 +2,6 @@ try: from ConfigParser import SafeConfigParser except ImportError: - # Probably running Python 3.x from configparser import ConfigParser as SafeConfigParser # This dequote() business is required for some older versions @@ -15,27 +14,29 @@ def dequote(s): s = s[1:-1] return s +_mysql_config_path = "mysql_config" + def mysql_config(what): from os import popen - f = popen("%s --%s" % (mysql_config.path, what)) + f = popen("%s --%s" % (_mysql_config_path, what)) data = f.read().strip().split() ret = f.close() if ret: if ret/256: data = [] if ret/256 > 1: - raise EnvironmentError("%s not found" % (mysql_config.path,)) + raise EnvironmentError("%s not found" % (_mysql_config_path,)) return data -mysql_config.path = "mysql_config" def get_config(): from setup_common import get_metadata_and_options, enabled, create_release_file + global _mysql_config_path metadata, options = get_metadata_and_options() if 'mysql_config' in options: - mysql_config.path = options['mysql_config'] + _mysql_config_path = options['mysql_config'] extra_objects = [] static = enabled(options, 'static') @@ -47,15 +48,7 @@ def get_config(): static = True sys.argv.remove('--static') - if enabled(options, 'embedded'): - libs = mysql_config("libmysqld-libs") - elif enabled(options, 'threadsafe'): - libs = mysql_config("libs_r") - if not libs: - libs = mysql_config("libs") - else: - libs = mysql_config("libs") - + libs = mysql_config("libs") library_dirs = [dequote(i[2:]) for i in libs if i.startswith('-L')] libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')] extra_link_args = [x for x in libs if not x.startswith(('-l', '-L'))] @@ -92,8 +85,6 @@ def get_config(): libraries.remove(client) name = "mysqlclient" - if enabled(options, 'embedded'): - name = name + "-embedded" metadata['name'] = name define_macros = [ diff --git a/site.cfg b/site.cfg index 8e2ab991..6b4596a4 100644 --- a/site.cfg +++ b/site.cfg @@ -1,10 +1,5 @@ [options] -# embedded: link against the embedded server library -# threadsafe: use the threadsafe client -# static: link against a static library (probably required for embedded) - -embedded = False -threadsafe = True +# static: link against a static library static = False # The path to mysql_config. From 67903f719fb29494491e1b3bbda25d6f359de043 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 4 Dec 2018 19:18:14 +0900 Subject: [PATCH 071/177] Replace TAB with 4 SPACEs --- _mysql.c | 3574 +++++++++++++++++++++++++++--------------------------- 1 file changed, 1787 insertions(+), 1787 deletions(-) diff --git a/_mysql.c b/_mysql.c index 430b149b..537e61bc 100644 --- a/_mysql.c +++ b/_mysql.c @@ -54,24 +54,24 @@ static PyObject *_mysql_MySQLError; static PyObject *_mysql_Warning; static PyObject *_mysql_Error; static PyObject *_mysql_DatabaseError; -static PyObject *_mysql_InterfaceError; +static PyObject *_mysql_InterfaceError; static PyObject *_mysql_DataError; -static PyObject *_mysql_OperationalError; -static PyObject *_mysql_IntegrityError; -static PyObject *_mysql_InternalError; +static PyObject *_mysql_OperationalError; +static PyObject *_mysql_IntegrityError; +static PyObject *_mysql_InternalError; static PyObject *_mysql_ProgrammingError; static PyObject *_mysql_NotSupportedError; typedef struct { - PyObject_HEAD - MYSQL connection; - int open; - PyObject *converter; + PyObject_HEAD + MYSQL connection; + int open; + PyObject *converter; } _mysql_ConnectionObject; #define check_connection(c) \ if (!(c->open)) { \ - return _mysql_Exception(c); \ + return _mysql_Exception(c); \ }; #define result_connection(r) ((_mysql_ConnectionObject *)r->conn) @@ -80,13 +80,13 @@ typedef struct { extern PyTypeObject _mysql_ConnectionObject_Type; typedef struct { - PyObject_HEAD - PyObject *conn; - MYSQL_RES *result; - int nfields; - int use; - char has_next; - PyObject *converter; + PyObject_HEAD + PyObject *conn; + MYSQL_RES *result; + int nfields; + int use; + char has_next; + PyObject *converter; } _mysql_ResultObject; extern PyTypeObject _mysql_ResultObject_Type; @@ -101,122 +101,122 @@ extern PyTypeObject _mysql_ResultObject_Type; PyObject * _mysql_Exception(_mysql_ConnectionObject *c) { - PyObject *t, *e; - int merr; - - if (!(t = PyTuple_New(2))) return NULL; - if (!(c->open)) { - /* GH-270: When connection is closed, accessing the c->connection - * object may cause SEGV. - */ - merr = CR_SERVER_GONE_ERROR; - } - else { - merr = mysql_errno(&(c->connection)); - } - switch (merr) { - case 0: - e = _mysql_InterfaceError; - break; - case CR_COMMANDS_OUT_OF_SYNC: - case ER_DB_CREATE_EXISTS: - case ER_SYNTAX_ERROR: - case ER_PARSE_ERROR: - case ER_NO_SUCH_TABLE: - case ER_WRONG_DB_NAME: - case ER_WRONG_TABLE_NAME: - case ER_FIELD_SPECIFIED_TWICE: - case ER_INVALID_GROUP_FUNC_USE: - case ER_UNSUPPORTED_EXTENSION: - case ER_TABLE_MUST_HAVE_COLUMNS: + PyObject *t, *e; + int merr; + + if (!(t = PyTuple_New(2))) return NULL; + if (!(c->open)) { + /* GH-270: When connection is closed, accessing the c->connection + * object may cause SEGV. + */ + merr = CR_SERVER_GONE_ERROR; + } + else { + merr = mysql_errno(&(c->connection)); + } + switch (merr) { + case 0: + e = _mysql_InterfaceError; + break; + case CR_COMMANDS_OUT_OF_SYNC: + case ER_DB_CREATE_EXISTS: + case ER_SYNTAX_ERROR: + case ER_PARSE_ERROR: + case ER_NO_SUCH_TABLE: + case ER_WRONG_DB_NAME: + case ER_WRONG_TABLE_NAME: + case ER_FIELD_SPECIFIED_TWICE: + case ER_INVALID_GROUP_FUNC_USE: + case ER_UNSUPPORTED_EXTENSION: + case ER_TABLE_MUST_HAVE_COLUMNS: #ifdef ER_CANT_DO_THIS_DURING_AN_TRANSACTION - case ER_CANT_DO_THIS_DURING_AN_TRANSACTION: + case ER_CANT_DO_THIS_DURING_AN_TRANSACTION: #endif - e = _mysql_ProgrammingError; - break; + e = _mysql_ProgrammingError; + break; #ifdef WARN_DATA_TRUNCATED - case WARN_DATA_TRUNCATED: + case WARN_DATA_TRUNCATED: #ifdef WARN_NULL_TO_NOTNULL - case WARN_NULL_TO_NOTNULL: + case WARN_NULL_TO_NOTNULL: #endif #ifdef ER_WARN_DATA_OUT_OF_RANGE - case ER_WARN_DATA_OUT_OF_RANGE: + case ER_WARN_DATA_OUT_OF_RANGE: #endif #ifdef ER_NO_DEFAULT - case ER_NO_DEFAULT: + case ER_NO_DEFAULT: #endif #ifdef ER_PRIMARY_CANT_HAVE_NULL - case ER_PRIMARY_CANT_HAVE_NULL: + case ER_PRIMARY_CANT_HAVE_NULL: #endif #ifdef ER_DATA_TOO_LONG - case ER_DATA_TOO_LONG: + case ER_DATA_TOO_LONG: #endif #ifdef ER_DATETIME_FUNCTION_OVERFLOW - case ER_DATETIME_FUNCTION_OVERFLOW: + case ER_DATETIME_FUNCTION_OVERFLOW: #endif - e = _mysql_DataError; - break; + e = _mysql_DataError; + break; #endif - case ER_DUP_ENTRY: + case ER_DUP_ENTRY: #ifdef ER_DUP_UNIQUE - case ER_DUP_UNIQUE: + case ER_DUP_UNIQUE: #endif #ifdef ER_NO_REFERENCED_ROW - case ER_NO_REFERENCED_ROW: + case ER_NO_REFERENCED_ROW: #endif #ifdef ER_NO_REFERENCED_ROW_2 - case ER_NO_REFERENCED_ROW_2: + case ER_NO_REFERENCED_ROW_2: #endif #ifdef ER_ROW_IS_REFERENCED - case ER_ROW_IS_REFERENCED: + case ER_ROW_IS_REFERENCED: #endif #ifdef ER_ROW_IS_REFERENCED_2 - case ER_ROW_IS_REFERENCED_2: + case ER_ROW_IS_REFERENCED_2: #endif #ifdef ER_CANNOT_ADD_FOREIGN - case ER_CANNOT_ADD_FOREIGN: + case ER_CANNOT_ADD_FOREIGN: #endif #ifdef ER_NO_DEFAULT_FOR_FIELD - case ER_NO_DEFAULT_FOR_FIELD: + case ER_NO_DEFAULT_FOR_FIELD: #endif - e = _mysql_IntegrityError; - break; + e = _mysql_IntegrityError; + break; #ifdef ER_WARNING_NOT_COMPLETE_ROLLBACK - case ER_WARNING_NOT_COMPLETE_ROLLBACK: + case ER_WARNING_NOT_COMPLETE_ROLLBACK: #endif #ifdef ER_NOT_SUPPORTED_YET - case ER_NOT_SUPPORTED_YET: + case ER_NOT_SUPPORTED_YET: #endif #ifdef ER_FEATURE_DISABLED - case ER_FEATURE_DISABLED: + case ER_FEATURE_DISABLED: #endif #ifdef ER_UNKNOWN_STORAGE_ENGINE - case ER_UNKNOWN_STORAGE_ENGINE: + case ER_UNKNOWN_STORAGE_ENGINE: #endif - e = _mysql_NotSupportedError; - break; - default: - if (merr < 1000) - e = _mysql_InternalError; - else - e = _mysql_OperationalError; - break; - } - PyTuple_SET_ITEM(t, 0, PyInt_FromLong((long)merr)); - PyTuple_SET_ITEM(t, 1, PyString_FromString(mysql_error(&(c->connection)))); - PyErr_SetObject(e, t); - Py_DECREF(t); - return NULL; + e = _mysql_NotSupportedError; + break; + default: + if (merr < 1000) + e = _mysql_InternalError; + else + e = _mysql_OperationalError; + break; + } + PyTuple_SET_ITEM(t, 0, PyInt_FromLong((long)merr)); + PyTuple_SET_ITEM(t, 1, PyString_FromString(mysql_error(&(c->connection)))); + PyErr_SetObject(e, t); + Py_DECREF(t); + return NULL; } static char _mysql_thread_safe__doc__[] = "Indicates whether the client is compiled as thread-safe."; static PyObject *_mysql_thread_safe( - PyObject *self, - PyObject *noargs) + PyObject *self, + PyObject *noargs) { - return PyInt_FromLong((long)mysql_thread_safe()); + return PyInt_FromLong((long)mysql_thread_safe()); } static char _mysql_ResultObject__doc__[] = @@ -230,197 +230,197 @@ Just forget you ever saw this. Forget... FOR-GET..."; static int _mysql_ResultObject_Initialize( - _mysql_ResultObject *self, - PyObject *args, - PyObject *kwargs) -{ - static char *kwlist[] = {"connection", "use", "converter", NULL}; - MYSQL_RES *result; - _mysql_ConnectionObject *conn=NULL; - int use=0; - PyObject *conv=NULL; - int n, i; - MYSQL_FIELD *fields; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|iO", kwlist, - &_mysql_ConnectionObject_Type, &conn, &use, &conv)) - return -1; - - self->conn = (PyObject *) conn; - Py_INCREF(conn); - self->use = use; - Py_BEGIN_ALLOW_THREADS ; - if (use) - result = mysql_use_result(&(conn->connection)); - else - result = mysql_store_result(&(conn->connection)); - self->result = result; - self->has_next = (char)mysql_more_results(&(conn->connection)); - Py_END_ALLOW_THREADS ; - if (!result) { - if (mysql_errno(&(conn->connection))) { - _mysql_Exception(conn); - return -1; - } - self->converter = PyTuple_New(0); - return 0; - } - n = mysql_num_fields(result); - self->nfields = n; - if (!(self->converter = PyTuple_New(n))) { - return -1; - } - fields = mysql_fetch_fields(result); - for (i=0; iconverter, i, fun); - } - - return 0; + _mysql_ResultObject *self, + PyObject *args, + PyObject *kwargs) +{ + static char *kwlist[] = {"connection", "use", "converter", NULL}; + MYSQL_RES *result; + _mysql_ConnectionObject *conn=NULL; + int use=0; + PyObject *conv=NULL; + int n, i; + MYSQL_FIELD *fields; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|iO", kwlist, + &_mysql_ConnectionObject_Type, &conn, &use, &conv)) + return -1; + + self->conn = (PyObject *) conn; + Py_INCREF(conn); + self->use = use; + Py_BEGIN_ALLOW_THREADS ; + if (use) + result = mysql_use_result(&(conn->connection)); + else + result = mysql_store_result(&(conn->connection)); + self->result = result; + self->has_next = (char)mysql_more_results(&(conn->connection)); + Py_END_ALLOW_THREADS ; + if (!result) { + if (mysql_errno(&(conn->connection))) { + _mysql_Exception(conn); + return -1; + } + self->converter = PyTuple_New(0); + return 0; + } + n = mysql_num_fields(result); + self->nfields = n; + if (!(self->converter = PyTuple_New(n))) { + return -1; + } + fields = mysql_fetch_fields(result); + for (i=0; iconverter, i, fun); + } + + return 0; } static int _mysql_ResultObject_traverse( - _mysql_ResultObject *self, - visitproc visit, - void *arg) + _mysql_ResultObject *self, + visitproc visit, + void *arg) { - int r; - if (self->converter) { - if (!(r = visit(self->converter, arg))) return r; - } - if (self->conn) - return visit(self->conn, arg); - return 0; + int r; + if (self->converter) { + if (!(r = visit(self->converter, arg))) return r; + } + if (self->conn) + return visit(self->conn, arg); + return 0; } static int _mysql_ResultObject_clear(_mysql_ResultObject *self) { - Py_CLEAR(self->converter); - Py_CLEAR(self->conn); - return 0; + Py_CLEAR(self->converter); + Py_CLEAR(self->conn); + return 0; } static int _mysql_ConnectionObject_Initialize( - _mysql_ConnectionObject *self, - PyObject *args, - PyObject *kwargs) -{ - MYSQL *conn = NULL; - PyObject *conv = NULL; - PyObject *ssl = NULL; - char *key = NULL, *cert = NULL, *ca = NULL, - *capath = NULL, *cipher = NULL; - PyObject *ssl_keepref[5] = {NULL}; - int n_ssl_keepref = 0; - char *host = NULL, *user = NULL, *passwd = NULL, - *db = NULL, *unix_socket = NULL; - unsigned int port = 0; - unsigned int client_flag = 0; - static char *kwlist[] = { "host", "user", "passwd", "db", "port", - "unix_socket", "conv", - "connect_timeout", "compress", - "named_pipe", "init_command", - "read_default_file", "read_default_group", - "client_flag", "ssl", - "local_infile", + _mysql_ConnectionObject *self, + PyObject *args, + PyObject *kwargs) +{ + MYSQL *conn = NULL; + PyObject *conv = NULL; + PyObject *ssl = NULL; + char *key = NULL, *cert = NULL, *ca = NULL, + *capath = NULL, *cipher = NULL; + PyObject *ssl_keepref[5] = {NULL}; + int n_ssl_keepref = 0; + char *host = NULL, *user = NULL, *passwd = NULL, + *db = NULL, *unix_socket = NULL; + unsigned int port = 0; + unsigned int client_flag = 0; + static char *kwlist[] = { "host", "user", "passwd", "db", "port", + "unix_socket", "conv", + "connect_timeout", "compress", + "named_pipe", "init_command", + "read_default_file", "read_default_group", + "client_flag", "ssl", + "local_infile", #ifdef HAVE_MYSQL_OPT_TIMEOUTS - "read_timeout", - "write_timeout", + "read_timeout", + "write_timeout", #endif - NULL } ; - int connect_timeout = 0; + NULL } ; + int connect_timeout = 0; #ifdef HAVE_MYSQL_OPT_TIMEOUTS - int read_timeout = 0; - int write_timeout = 0; + int read_timeout = 0; + int write_timeout = 0; #endif - int compress = -1, named_pipe = -1, local_infile = -1; - char *init_command=NULL, - *read_default_file=NULL, - *read_default_group=NULL; + int compress = -1, named_pipe = -1, local_infile = -1; + char *init_command=NULL, + *read_default_file=NULL, + *read_default_group=NULL; - self->converter = NULL; - self->open = 0; + self->converter = NULL; + self->open = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, + if (!PyArg_ParseTupleAndKeywords(args, kwargs, #ifdef HAVE_MYSQL_OPT_TIMEOUTS - "|ssssisOiiisssiOiii:connect", + "|ssssisOiiisssiOiii:connect", #else - "|ssssisOiiisssiOi:connect", + "|ssssisOiiisssiOi:connect", #endif - kwlist, - &host, &user, &passwd, &db, - &port, &unix_socket, &conv, - &connect_timeout, - &compress, &named_pipe, - &init_command, &read_default_file, - &read_default_group, - &client_flag, &ssl, - &local_infile + kwlist, + &host, &user, &passwd, &db, + &port, &unix_socket, &conv, + &connect_timeout, + &compress, &named_pipe, + &init_command, &read_default_file, + &read_default_group, + &client_flag, &ssl, + &local_infile #ifdef HAVE_MYSQL_OPT_TIMEOUTS - , &read_timeout - , &write_timeout + , &read_timeout + , &write_timeout #endif - )) - return -1; + )) + return -1; #ifdef IS_PY3K #define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\ @@ -432,90 +432,90 @@ _mysql_ConnectionObject_Initialize( PyErr_Clear();} #endif - if (ssl) { - PyObject *value = NULL; - _stringsuck(ca, value, ssl); - _stringsuck(capath, value, ssl); - _stringsuck(cert, value, ssl); - _stringsuck(key, value, ssl); - _stringsuck(cipher, value, ssl); - } - - Py_BEGIN_ALLOW_THREADS ; - conn = mysql_init(&(self->connection)); - if (connect_timeout) { - unsigned int timeout = connect_timeout; - mysql_options(&(self->connection), MYSQL_OPT_CONNECT_TIMEOUT, - (char *)&timeout); - } + if (ssl) { + PyObject *value = NULL; + _stringsuck(ca, value, ssl); + _stringsuck(capath, value, ssl); + _stringsuck(cert, value, ssl); + _stringsuck(key, value, ssl); + _stringsuck(cipher, value, ssl); + } + + Py_BEGIN_ALLOW_THREADS ; + conn = mysql_init(&(self->connection)); + if (connect_timeout) { + unsigned int timeout = connect_timeout; + mysql_options(&(self->connection), MYSQL_OPT_CONNECT_TIMEOUT, + (char *)&timeout); + } #ifdef HAVE_MYSQL_OPT_TIMEOUTS - if (read_timeout) { - unsigned int timeout = read_timeout; - mysql_options(&(self->connection), MYSQL_OPT_READ_TIMEOUT, - (char *)&timeout); - } - if (write_timeout) { - unsigned int timeout = write_timeout; - mysql_options(&(self->connection), MYSQL_OPT_WRITE_TIMEOUT, - (char *)&timeout); - } + if (read_timeout) { + unsigned int timeout = read_timeout; + mysql_options(&(self->connection), MYSQL_OPT_READ_TIMEOUT, + (char *)&timeout); + } + if (write_timeout) { + unsigned int timeout = write_timeout; + mysql_options(&(self->connection), MYSQL_OPT_WRITE_TIMEOUT, + (char *)&timeout); + } #endif - if (compress != -1) { - mysql_options(&(self->connection), MYSQL_OPT_COMPRESS, 0); - client_flag |= CLIENT_COMPRESS; - } - if (named_pipe != -1) - mysql_options(&(self->connection), MYSQL_OPT_NAMED_PIPE, 0); - if (init_command != NULL) - mysql_options(&(self->connection), MYSQL_INIT_COMMAND, init_command); - if (read_default_file != NULL) - mysql_options(&(self->connection), MYSQL_READ_DEFAULT_FILE, read_default_file); - if (read_default_group != NULL) - mysql_options(&(self->connection), MYSQL_READ_DEFAULT_GROUP, read_default_group); - - if (local_infile != -1) - mysql_options(&(self->connection), MYSQL_OPT_LOCAL_INFILE, (char *) &local_infile); - - if (ssl) { - mysql_ssl_set(&(self->connection), key, cert, ca, capath, cipher); - } - - conn = mysql_real_connect(&(self->connection), host, user, passwd, db, - port, unix_socket, client_flag); - - Py_END_ALLOW_THREADS ; - - if (ssl) { - int i; - for (i=0; iconverter = conv; - - /* - PyType_GenericAlloc() automatically sets up GC allocation and - tracking for GC objects, at least in 2.2.1, so it does not need to - be done here. tp_dealloc still needs to call PyObject_GC_UnTrack(), - however. - */ - self->open = 1; - return 0; + if (compress != -1) { + mysql_options(&(self->connection), MYSQL_OPT_COMPRESS, 0); + client_flag |= CLIENT_COMPRESS; + } + if (named_pipe != -1) + mysql_options(&(self->connection), MYSQL_OPT_NAMED_PIPE, 0); + if (init_command != NULL) + mysql_options(&(self->connection), MYSQL_INIT_COMMAND, init_command); + if (read_default_file != NULL) + mysql_options(&(self->connection), MYSQL_READ_DEFAULT_FILE, read_default_file); + if (read_default_group != NULL) + mysql_options(&(self->connection), MYSQL_READ_DEFAULT_GROUP, read_default_group); + + if (local_infile != -1) + mysql_options(&(self->connection), MYSQL_OPT_LOCAL_INFILE, (char *) &local_infile); + + if (ssl) { + mysql_ssl_set(&(self->connection), key, cert, ca, capath, cipher); + } + + conn = mysql_real_connect(&(self->connection), host, user, passwd, db, + port, unix_socket, client_flag); + + Py_END_ALLOW_THREADS ; + + if (ssl) { + int i; + for (i=0; iconverter = conv; + + /* + PyType_GenericAlloc() automatically sets up GC allocation and + tracking for GC objects, at least in 2.2.1, so it does not need to + be done here. tp_dealloc still needs to call PyObject_GC_UnTrack(), + however. + */ + self->open = 1; + return 0; } static char _mysql_connect__doc__[] = @@ -574,37 +574,37 @@ load_infile\n\ static PyObject * _mysql_connect( - PyObject *self, - PyObject *args, - PyObject *kwargs) + PyObject *self, + PyObject *args, + PyObject *kwargs) { - _mysql_ConnectionObject *c=NULL; + _mysql_ConnectionObject *c=NULL; - c = MyAlloc(_mysql_ConnectionObject, _mysql_ConnectionObject_Type); - if (c == NULL) return NULL; - if (_mysql_ConnectionObject_Initialize(c, args, kwargs)) { - Py_DECREF(c); - c = NULL; - } - return (PyObject *) c; + c = MyAlloc(_mysql_ConnectionObject, _mysql_ConnectionObject_Type); + if (c == NULL) return NULL; + if (_mysql_ConnectionObject_Initialize(c, args, kwargs)) { + Py_DECREF(c); + c = NULL; + } + return (PyObject *) c; } static int _mysql_ConnectionObject_traverse( - _mysql_ConnectionObject *self, - visitproc visit, - void *arg) + _mysql_ConnectionObject *self, + visitproc visit, + void *arg) { - if (self->converter) - return visit(self->converter, arg); - return 0; + if (self->converter) + return visit(self->converter, arg); + return 0; } static int _mysql_ConnectionObject_clear( - _mysql_ConnectionObject *self) + _mysql_ConnectionObject *self) { - Py_XDECREF(self->converter); - self->converter = NULL; - return 0; + Py_XDECREF(self->converter); + self->converter = NULL; + return 0; } static char _mysql_ConnectionObject_fileno__doc__[] = @@ -612,10 +612,10 @@ static char _mysql_ConnectionObject_fileno__doc__[] = static PyObject * _mysql_ConnectionObject_fileno( - _mysql_ConnectionObject *self) + _mysql_ConnectionObject *self) { check_connection(self); - return PyInt_FromLong(self->connection.net.fd); + return PyInt_FromLong(self->connection.net.fd); } static char _mysql_ConnectionObject_close__doc__[] = @@ -623,16 +623,16 @@ static char _mysql_ConnectionObject_close__doc__[] = static PyObject * _mysql_ConnectionObject_close( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { check_connection(self); Py_BEGIN_ALLOW_THREADS mysql_close(&(self->connection)); Py_END_ALLOW_THREADS self->open = 0; - _mysql_ConnectionObject_clear(self); - Py_RETURN_NONE; + _mysql_ConnectionObject_clear(self); + Py_RETURN_NONE; } static char _mysql_ConnectionObject_affected_rows__doc__ [] = @@ -642,15 +642,15 @@ Non-standard. Use Cursor.rowcount.\n\ static PyObject * _mysql_ConnectionObject_affected_rows( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - my_ulonglong ret; - check_connection(self); - ret = mysql_affected_rows(&(self->connection)); - if (ret == (my_ulonglong)-1) - return PyInt_FromLong(-1); - return PyLong_FromUnsignedLongLong(ret); + my_ulonglong ret; + check_connection(self); + ret = mysql_affected_rows(&(self->connection)); + if (ret == (my_ulonglong)-1) + return PyInt_FromLong(-1); + return PyLong_FromUnsignedLongLong(ret); } static char _mysql_debug__doc__[] = @@ -661,14 +661,14 @@ support debugging.\n\ "; static PyObject * _mysql_debug( - PyObject *self, - PyObject *args) + PyObject *self, + PyObject *args) { - char *debug; - if (!PyArg_ParseTuple(args, "s", &debug)) return NULL; - mysql_debug(debug); - Py_INCREF(Py_None); - return Py_None; + char *debug; + if (!PyArg_ParseTuple(args, "s", &debug)) return NULL; + mysql_debug(debug); + Py_INCREF(Py_None); + return Py_None; } static char _mysql_ConnectionObject_dump_debug_info__doc__[] = @@ -679,17 +679,17 @@ this to work. Non-standard.\n\ static PyObject * _mysql_ConnectionObject_dump_debug_info( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - int err; - check_connection(self); - Py_BEGIN_ALLOW_THREADS - err = mysql_dump_debug_info(&(self->connection)); - Py_END_ALLOW_THREADS - if (err) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + int err; + check_connection(self); + Py_BEGIN_ALLOW_THREADS + err = mysql_dump_debug_info(&(self->connection)); + Py_END_ALLOW_THREADS + if (err) return _mysql_Exception(self); + Py_INCREF(Py_None); + return Py_None; } static char _mysql_ConnectionObject_autocommit__doc__[] = @@ -697,18 +697,18 @@ static char _mysql_ConnectionObject_autocommit__doc__[] = "; static PyObject * _mysql_ConnectionObject_autocommit( - _mysql_ConnectionObject *self, - PyObject *args) + _mysql_ConnectionObject *self, + PyObject *args) { - int flag, err; - if (!PyArg_ParseTuple(args, "i", &flag)) return NULL; - check_connection(self); - Py_BEGIN_ALLOW_THREADS - err = mysql_autocommit(&(self->connection), flag); - Py_END_ALLOW_THREADS - if (err) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + int flag, err; + if (!PyArg_ParseTuple(args, "i", &flag)) return NULL; + check_connection(self); + Py_BEGIN_ALLOW_THREADS + err = mysql_autocommit(&(self->connection), flag); + Py_END_ALLOW_THREADS + if (err) return _mysql_Exception(self); + Py_INCREF(Py_None); + return Py_None; } static char _mysql_ConnectionObject_get_autocommit__doc__[] = @@ -716,14 +716,14 @@ static char _mysql_ConnectionObject_get_autocommit__doc__[] = static PyObject * _mysql_ConnectionObject_get_autocommit( - _mysql_ConnectionObject *self, - PyObject *args) + _mysql_ConnectionObject *self, + PyObject *args) { - check_connection(self); - if (self->connection.server_status & SERVER_STATUS_AUTOCOMMIT) { - Py_RETURN_TRUE; - } - Py_RETURN_FALSE; + check_connection(self); + if (self->connection.server_status & SERVER_STATUS_AUTOCOMMIT) { + Py_RETURN_TRUE; + } + Py_RETURN_FALSE; } static char _mysql_ConnectionObject_commit__doc__[] = @@ -731,17 +731,17 @@ static char _mysql_ConnectionObject_commit__doc__[] = "; static PyObject * _mysql_ConnectionObject_commit( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - int err; - check_connection(self); - Py_BEGIN_ALLOW_THREADS - err = mysql_commit(&(self->connection)); - Py_END_ALLOW_THREADS - if (err) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + int err; + check_connection(self); + Py_BEGIN_ALLOW_THREADS + err = mysql_commit(&(self->connection)); + Py_END_ALLOW_THREADS + if (err) return _mysql_Exception(self); + Py_INCREF(Py_None); + return Py_None; } static char _mysql_ConnectionObject_rollback__doc__[] = @@ -749,15 +749,15 @@ static char _mysql_ConnectionObject_rollback__doc__[] = "; static PyObject * _mysql_ConnectionObject_rollback( - _mysql_ConnectionObject *self, - PyObject *noargs) -{ - int err; - check_connection(self); - Py_BEGIN_ALLOW_THREADS - err = mysql_rollback(&(self->connection)); - Py_END_ALLOW_THREADS - if (err) return _mysql_Exception(self); + _mysql_ConnectionObject *self, + PyObject *noargs) +{ + int err; + check_connection(self); + Py_BEGIN_ALLOW_THREADS + err = mysql_rollback(&(self->connection)); + Py_END_ALLOW_THREADS + if (err) return _mysql_Exception(self); Py_RETURN_NONE; } @@ -776,17 +776,17 @@ Non-standard.\n\ "; static PyObject * _mysql_ConnectionObject_next_result( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - int err; - check_connection(self); - Py_BEGIN_ALLOW_THREADS - err = mysql_next_result(&(self->connection)); - Py_END_ALLOW_THREADS - if (err > 0) return _mysql_Exception(self); - return PyInt_FromLong(err); -} + int err; + check_connection(self); + Py_BEGIN_ALLOW_THREADS + err = mysql_next_result(&(self->connection)); + Py_END_ALLOW_THREADS + if (err > 0) return _mysql_Exception(self); + return PyInt_FromLong(err); +} static char _mysql_ConnectionObject_set_server_option__doc__[] = @@ -797,19 +797,19 @@ Non-standard.\n\ "; static PyObject * _mysql_ConnectionObject_set_server_option( - _mysql_ConnectionObject *self, - PyObject *args) -{ - int err, flags=0; - if (!PyArg_ParseTuple(args, "i", &flags)) - return NULL; - check_connection(self); - Py_BEGIN_ALLOW_THREADS - err = mysql_set_server_option(&(self->connection), flags); - Py_END_ALLOW_THREADS - if (err) return _mysql_Exception(self); - return PyInt_FromLong(err); -} + _mysql_ConnectionObject *self, + PyObject *args) +{ + int err, flags=0; + if (!PyArg_ParseTuple(args, "i", &flags)) + return NULL; + check_connection(self); + Py_BEGIN_ALLOW_THREADS + err = mysql_set_server_option(&(self->connection), flags); + Py_END_ALLOW_THREADS + if (err) return _mysql_Exception(self); + return PyInt_FromLong(err); +} static char _mysql_ConnectionObject_sqlstate__doc__[] = "Returns a string containing the SQLSTATE error code\n\ @@ -825,11 +825,11 @@ Non-standard.\n\ "; static PyObject * _mysql_ConnectionObject_sqlstate( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - check_connection(self); - return PyString_FromString(mysql_sqlstate(&(self->connection))); + check_connection(self); + return PyString_FromString(mysql_sqlstate(&(self->connection))); } static char _mysql_ConnectionObject_warning_count__doc__[] = @@ -840,12 +840,12 @@ Non-standard.\n\ "; static PyObject * _mysql_ConnectionObject_warning_count( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - check_connection(self); - return PyInt_FromLong(mysql_warning_count(&(self->connection))); -} + check_connection(self); + return PyInt_FromLong(mysql_warning_count(&(self->connection))); +} static char _mysql_ConnectionObject_errno__doc__[] = "Returns the error code for the most recently invoked API function\n\ @@ -855,11 +855,11 @@ occurred.\n\ static PyObject * _mysql_ConnectionObject_errno( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - check_connection(self); - return PyInt_FromLong((long)mysql_errno(&(self->connection))); + check_connection(self); + return PyInt_FromLong((long)mysql_errno(&(self->connection))); } static char _mysql_ConnectionObject_error__doc__[] = @@ -870,11 +870,11 @@ occurred.\n\ static PyObject * _mysql_ConnectionObject_error( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - check_connection(self); - return PyString_FromString(mysql_error(&(self->connection))); + check_connection(self); + return PyString_FromString(mysql_error(&(self->connection))); } static char _mysql_escape_string__doc__[] = @@ -887,30 +887,30 @@ it will escape entire sequences as well as strings."; static PyObject * _mysql_escape_string( - _mysql_ConnectionObject *self, - PyObject *args) -{ - PyObject *str; - char *in, *out; - int len, size; - if (!PyArg_ParseTuple(args, "s#:escape_string", &in, &size)) return NULL; - str = PyBytes_FromStringAndSize((char *) NULL, size*2+1); - if (!str) return PyErr_NoMemory(); - out = PyBytes_AS_STRING(str); - - if (self && PyModule_Check((PyObject*)self)) - self = NULL; - if (self && self->open) { + _mysql_ConnectionObject *self, + PyObject *args) +{ + PyObject *str; + char *in, *out; + int len, size; + if (!PyArg_ParseTuple(args, "s#:escape_string", &in, &size)) return NULL; + str = PyBytes_FromStringAndSize((char *) NULL, size*2+1); + if (!str) return PyErr_NoMemory(); + out = PyBytes_AS_STRING(str); + + if (self && PyModule_Check((PyObject*)self)) + self = NULL; + if (self && self->open) { #if MYSQL_VERSION_ID >= 50707 && !defined(MARIADB_BASE_VERSION) && !defined(MARIADB_VERSION_ID) - len = mysql_real_escape_string_quote(&(self->connection), out, in, size, '\''); + len = mysql_real_escape_string_quote(&(self->connection), out, in, size, '\''); #else - len = mysql_real_escape_string(&(self->connection), out, in, size); + len = mysql_real_escape_string(&(self->connection), out, in, size); #endif - } else { - len = mysql_escape_string(out, in, size); - } - if (_PyBytes_Resize(&str, len) < 0) return NULL; - return (str); + } else { + len = mysql_escape_string(out, in, size); + } + if (_PyBytes_Resize(&str, len) < 0) return NULL; + return (str); } static char _mysql_string_literal__doc__[] = @@ -925,85 +925,85 @@ _mysql.string_literal(obj) cannot handle character sets."; static PyObject * _mysql_string_literal( - _mysql_ConnectionObject *self, - PyObject *args) -{ - PyObject *str, *s, *o, *d; - char *in, *out; - int len, size; - if (self && PyModule_Check((PyObject*)self)) - self = NULL; - if (!PyArg_ParseTuple(args, "O|O:string_literal", &o, &d)) return NULL; - if (PyBytes_Check(o)) { - s = o; - Py_INCREF(s); - } else { - s = PyObject_Str(o); - if (!s) return NULL; + _mysql_ConnectionObject *self, + PyObject *args) +{ + PyObject *str, *s, *o, *d; + char *in, *out; + int len, size; + if (self && PyModule_Check((PyObject*)self)) + self = NULL; + if (!PyArg_ParseTuple(args, "O|O:string_literal", &o, &d)) return NULL; + if (PyBytes_Check(o)) { + s = o; + Py_INCREF(s); + } else { + s = PyObject_Str(o); + if (!s) return NULL; #ifdef IS_PY3K - { - PyObject *t = PyUnicode_AsASCIIString(s); - Py_DECREF(s); - if (!t) return NULL; - s = t; - } + { + PyObject *t = PyUnicode_AsASCIIString(s); + Py_DECREF(s); + if (!t) return NULL; + s = t; + } #endif - } - in = PyBytes_AsString(s); - size = PyBytes_GET_SIZE(s); - str = PyBytes_FromStringAndSize((char *) NULL, size*2+3); - if (!str) { - Py_DECREF(s); - return PyErr_NoMemory(); - } - out = PyBytes_AS_STRING(str); - if (self && self->open) { + } + in = PyBytes_AsString(s); + size = PyBytes_GET_SIZE(s); + str = PyBytes_FromStringAndSize((char *) NULL, size*2+3); + if (!str) { + Py_DECREF(s); + return PyErr_NoMemory(); + } + out = PyBytes_AS_STRING(str); + if (self && self->open) { #if MYSQL_VERSION_ID >= 50707 && !defined(MARIADB_BASE_VERSION) && !defined(MARIADB_VERSION_ID) - len = mysql_real_escape_string_quote(&(self->connection), out+1, in, size, '\''); + len = mysql_real_escape_string_quote(&(self->connection), out+1, in, size, '\''); #else - len = mysql_real_escape_string(&(self->connection), out+1, in, size); + len = mysql_real_escape_string(&(self->connection), out+1, in, size); #endif - } else { - len = mysql_escape_string(out+1, in, size); - } - *out = *(out+len+1) = '\''; - if (_PyBytes_Resize(&str, len+2) < 0) return NULL; - Py_DECREF(s); - return (str); + } else { + len = mysql_escape_string(out+1, in, size); + } + *out = *(out+len+1) = '\''; + if (_PyBytes_Resize(&str, len+2) < 0) return NULL; + Py_DECREF(s); + return (str); } static PyObject *_mysql_NULL; static PyObject * _escape_item( - PyObject *item, - PyObject *d) -{ - PyObject *quoted=NULL, *itemtype, *itemconv; - if (!(itemtype = PyObject_Type(item))) - goto error; - itemconv = PyObject_GetItem(d, itemtype); - Py_DECREF(itemtype); - if (!itemconv) { - PyErr_Clear(); - itemconv = PyObject_GetItem(d, + PyObject *item, + PyObject *d) +{ + PyObject *quoted=NULL, *itemtype, *itemconv; + if (!(itemtype = PyObject_Type(item))) + goto error; + itemconv = PyObject_GetItem(d, itemtype); + Py_DECREF(itemtype); + if (!itemconv) { + PyErr_Clear(); + itemconv = PyObject_GetItem(d, #ifdef IS_PY3K - (PyObject *) &PyUnicode_Type); + (PyObject *) &PyUnicode_Type); #else - (PyObject *) &PyString_Type); + (PyObject *) &PyString_Type); #endif - } - if (!itemconv) { - PyErr_SetString(PyExc_TypeError, - "no default type converter defined"); - goto error; - } - Py_INCREF(d); - quoted = PyObject_CallFunction(itemconv, "OO", item, d); - Py_DECREF(d); - Py_DECREF(itemconv); + } + if (!itemconv) { + PyErr_SetString(PyExc_TypeError, + "no default type converter defined"); + goto error; + } + Py_INCREF(d); + quoted = PyObject_CallFunction(itemconv, "OO", item, d); + Py_DECREF(d); + Py_DECREF(itemconv); error: - return quoted; + return quoted; } static char _mysql_escape__doc__[] = @@ -1012,28 +1012,28 @@ using mapping dict to provide quoting functions for each type.\n\ Returns a SQL literal string."; static PyObject * _mysql_escape( - PyObject *self, - PyObject *args) -{ - PyObject *o=NULL, *d=NULL; - if (!PyArg_ParseTuple(args, "O|O:escape", &o, &d)) - return NULL; - if (d) { - if (!PyMapping_Check(d)) { - PyErr_SetString(PyExc_TypeError, - "argument 2 must be a mapping"); - return NULL; - } - return _escape_item(o, d); - } else { - if (!self) { - PyErr_SetString(PyExc_TypeError, - "argument 2 must be a mapping"); - return NULL; - } - return _escape_item(o, - ((_mysql_ConnectionObject *) self)->converter); - } + PyObject *self, + PyObject *args) +{ + PyObject *o=NULL, *d=NULL; + if (!PyArg_ParseTuple(args, "O|O:escape", &o, &d)) + return NULL; + if (d) { + if (!PyMapping_Check(d)) { + PyErr_SetString(PyExc_TypeError, + "argument 2 must be a mapping"); + return NULL; + } + return _escape_item(o, d); + } else { + if (!self) { + PyErr_SetString(PyExc_TypeError, + "argument 2 must be a mapping"); + return NULL; + } + return _escape_item(o, + ((_mysql_ConnectionObject *) self)->converter); + } } static char _mysql_escape_sequence__doc__[] = @@ -1042,32 +1042,32 @@ seq using mapping dict to provide quoting functions for each type.\n\ Returns a tuple of escaped items."; static PyObject * _mysql_escape_sequence( - PyObject *self, - PyObject *args) -{ - PyObject *o=NULL, *d=NULL, *r=NULL, *item, *quoted; - int i, n; - if (!PyArg_ParseTuple(args, "OO:escape_sequence", &o, &d)) - goto error; - if (!PyMapping_Check(d)) { + PyObject *self, + PyObject *args) +{ + PyObject *o=NULL, *d=NULL, *r=NULL, *item, *quoted; + int i, n; + if (!PyArg_ParseTuple(args, "OO:escape_sequence", &o, &d)) + goto error; + if (!PyMapping_Check(d)) { PyErr_SetString(PyExc_TypeError, "argument 2 must be a mapping"); return NULL; } - if ((n = PyObject_Length(o)) == -1) goto error; - if (!(r = PyTuple_New(n))) goto error; - for (i=0; iresult); - fields = mysql_fetch_fields(self->result); - if (!(d = PyTuple_New(n))) return NULL; - for (i=0; iresult); + fields = mysql_fetch_fields(self->result); + if (!(d = PyTuple_New(n))) return NULL; + for (i=0; iresult); - fields = mysql_fetch_fields(self->result); - if (!(d = PyTuple_New(n))) return NULL; - for (i=0; iresult); + fields = mysql_fetch_fields(self->result); + if (!(d = PyTuple_New(n))) return NULL; + for (i=0; itype; - // Return bytes for binary and string types. - int binary = 0; - if (field_type == FIELD_TYPE_TINY_BLOB || - field_type == FIELD_TYPE_MEDIUM_BLOB || - field_type == FIELD_TYPE_LONG_BLOB || - field_type == FIELD_TYPE_BLOB || - field_type == FIELD_TYPE_VAR_STRING || - field_type == FIELD_TYPE_STRING || - field_type == FIELD_TYPE_GEOMETRY || - field_type == FIELD_TYPE_BIT) { - binary = 1; - } + int field_type = field->type; + // Return bytes for binary and string types. + int binary = 0; + if (field_type == FIELD_TYPE_TINY_BLOB || + field_type == FIELD_TYPE_MEDIUM_BLOB || + field_type == FIELD_TYPE_LONG_BLOB || + field_type == FIELD_TYPE_BLOB || + field_type == FIELD_TYPE_VAR_STRING || + field_type == FIELD_TYPE_STRING || + field_type == FIELD_TYPE_GEOMETRY || + field_type == FIELD_TYPE_BIT) { + binary = 1; + } #endif - if (rowitem) { - if (converter != Py_None) { - v = PyObject_CallFunction(converter, + if (rowitem) { + if (converter != Py_None) { + v = PyObject_CallFunction(converter, #ifdef IS_PY3K - binary ? "y#" : "s#", + binary ? "y#" : "s#", #else - "s#", + "s#", #endif - rowitem, - (int)length); - } else { + rowitem, + (int)length); + } else { #ifdef IS_PY3K - if (!binary) { - v = PyUnicode_FromStringAndSize(rowitem, (int)length); - } else + if (!binary) { + v = PyUnicode_FromStringAndSize(rowitem, (int)length); + } else #endif - v = PyBytes_FromStringAndSize(rowitem, (int)length); - } - if (!v) - return NULL; - } else { - Py_INCREF(Py_None); - v = Py_None; - } - return v; + v = PyBytes_FromStringAndSize(rowitem, (int)length); + } + if (!v) + return NULL; + } else { + Py_INCREF(Py_None); + v = Py_None; + } + return v; } static PyObject * _mysql_row_to_tuple( - _mysql_ResultObject *self, - MYSQL_ROW row) -{ - unsigned int n, i; - unsigned long *length; - PyObject *r, *c; - MYSQL_FIELD *fields; - - n = mysql_num_fields(self->result); - if (!(r = PyTuple_New(n))) return NULL; - length = mysql_fetch_lengths(self->result); - fields = mysql_fetch_fields(self->result); - for (i=0; iconverter, i); - v = _mysql_field_to_python(c, row[i], length[i], &fields[i]); - if (!v) goto error; - PyTuple_SET_ITEM(r, i, v); - } - return r; + _mysql_ResultObject *self, + MYSQL_ROW row) +{ + unsigned int n, i; + unsigned long *length; + PyObject *r, *c; + MYSQL_FIELD *fields; + + n = mysql_num_fields(self->result); + if (!(r = PyTuple_New(n))) return NULL; + length = mysql_fetch_lengths(self->result); + fields = mysql_fetch_fields(self->result); + for (i=0; iconverter, i); + v = _mysql_field_to_python(c, row[i], length[i], &fields[i]); + if (!v) goto error; + PyTuple_SET_ITEM(r, i, v); + } + return r; error: - Py_XDECREF(r); - return NULL; + Py_XDECREF(r); + return NULL; } static PyObject * _mysql_row_to_dict( - _mysql_ResultObject *self, - MYSQL_ROW row) -{ - unsigned int n, i; - unsigned long *length; - PyObject *r, *c; - MYSQL_FIELD *fields; - - n = mysql_num_fields(self->result); - if (!(r = PyDict_New())) return NULL; - length = mysql_fetch_lengths(self->result); - fields = mysql_fetch_fields(self->result); - for (i=0; iconverter, i); - v = _mysql_field_to_python(c, row[i], length[i], &fields[i]); - if (!v) goto error; - if (!PyMapping_HasKeyString(r, fields[i].name)) { - PyMapping_SetItemString(r, fields[i].name, v); - } else { - int len; - char buf[256]; - strncpy(buf, fields[i].table, 256); - len = strlen(buf); - strncat(buf, ".", 256-len); - len = strlen(buf); - strncat(buf, fields[i].name, 256-len); - PyMapping_SetItemString(r, buf, v); - } - Py_DECREF(v); - } - return r; + _mysql_ResultObject *self, + MYSQL_ROW row) +{ + unsigned int n, i; + unsigned long *length; + PyObject *r, *c; + MYSQL_FIELD *fields; + + n = mysql_num_fields(self->result); + if (!(r = PyDict_New())) return NULL; + length = mysql_fetch_lengths(self->result); + fields = mysql_fetch_fields(self->result); + for (i=0; iconverter, i); + v = _mysql_field_to_python(c, row[i], length[i], &fields[i]); + if (!v) goto error; + if (!PyMapping_HasKeyString(r, fields[i].name)) { + PyMapping_SetItemString(r, fields[i].name, v); + } else { + int len; + char buf[256]; + strncpy(buf, fields[i].table, 256); + len = strlen(buf); + strncat(buf, ".", 256-len); + len = strlen(buf); + strncat(buf, fields[i].name, 256-len); + PyMapping_SetItemString(r, buf, v); + } + Py_DECREF(v); + } + return r; error: - Py_XDECREF(r); - return NULL; + Py_XDECREF(r); + return NULL; } static PyObject * _mysql_row_to_dict_old( - _mysql_ResultObject *self, - MYSQL_ROW row) + _mysql_ResultObject *self, + MYSQL_ROW row) { - unsigned int n, i; - unsigned long *length; - PyObject *r, *c; + unsigned int n, i; + unsigned long *length; + PyObject *r, *c; MYSQL_FIELD *fields; - n = mysql_num_fields(self->result); - if (!(r = PyDict_New())) return NULL; - length = mysql_fetch_lengths(self->result); - fields = mysql_fetch_fields(self->result); - for (i=0; iconverter, i); - v = _mysql_field_to_python(c, row[i], length[i], &fields[i]); - if (!v) goto error; - { - int len=0; - char buf[256]=""; - if (strlen(fields[i].table)) { - strncpy(buf, fields[i].table, 256); - len = strlen(buf); - strncat(buf, ".", 256-len); - len = strlen(buf); - } - strncat(buf, fields[i].name, 256-len); - PyMapping_SetItemString(r, buf, v); - } - Py_DECREF(v); - } - return r; + n = mysql_num_fields(self->result); + if (!(r = PyDict_New())) return NULL; + length = mysql_fetch_lengths(self->result); + fields = mysql_fetch_fields(self->result); + for (i=0; iconverter, i); + v = _mysql_field_to_python(c, row[i], length[i], &fields[i]); + if (!v) goto error; + { + int len=0; + char buf[256]=""; + if (strlen(fields[i].table)) { + strncpy(buf, fields[i].table, 256); + len = strlen(buf); + strncat(buf, ".", 256-len); + len = strlen(buf); + } + strncat(buf, fields[i].name, 256-len); + PyMapping_SetItemString(r, buf, v); + } + Py_DECREF(v); + } + return r; error: - Py_XDECREF(r); - return NULL; + Py_XDECREF(r); + return NULL; } typedef PyObject *_PYFUNC(_mysql_ResultObject *, MYSQL_ROW); int _mysql__fetch_row( - _mysql_ResultObject *self, - PyObject **r, - int skiprows, - int maxrows, - _PYFUNC *convert_row) -{ - int i; - MYSQL_ROW row; - - for (i = skiprows; i<(skiprows+maxrows); i++) { - PyObject *v; - if (!self->use) - row = mysql_fetch_row(self->result); - else { - Py_BEGIN_ALLOW_THREADS; - row = mysql_fetch_row(self->result); - Py_END_ALLOW_THREADS; - } - if (!row && mysql_errno(&(((_mysql_ConnectionObject *)(self->conn))->connection))) { - _mysql_Exception((_mysql_ConnectionObject *)self->conn); - goto error; - } - if (!row) { - if (_PyTuple_Resize(r, i) == -1) goto error; - break; - } - v = convert_row(self, row); - if (!v) goto error; - PyTuple_SET_ITEM(*r, i, v); - } - return i-skiprows; + _mysql_ResultObject *self, + PyObject **r, + int skiprows, + int maxrows, + _PYFUNC *convert_row) +{ + int i; + MYSQL_ROW row; + + for (i = skiprows; i<(skiprows+maxrows); i++) { + PyObject *v; + if (!self->use) + row = mysql_fetch_row(self->result); + else { + Py_BEGIN_ALLOW_THREADS; + row = mysql_fetch_row(self->result); + Py_END_ALLOW_THREADS; + } + if (!row && mysql_errno(&(((_mysql_ConnectionObject *)(self->conn))->connection))) { + _mysql_Exception((_mysql_ConnectionObject *)self->conn); + goto error; + } + if (!row) { + if (_PyTuple_Resize(r, i) == -1) goto error; + break; + } + v = convert_row(self, row); + if (!v) goto error; + PyTuple_SET_ITEM(*r, i, v); + } + return i-skiprows; error: - return -1; + return -1; } static char _mysql_ResultObject_fetch_row__doc__[] = @@ -1369,62 +1369,62 @@ The rows are formatted according to how:\n\ static PyObject * _mysql_ResultObject_fetch_row( - _mysql_ResultObject *self, - PyObject *args, - PyObject *kwargs) -{ - typedef PyObject *_PYFUNC(_mysql_ResultObject *, MYSQL_ROW); - static char *kwlist[] = { "maxrows", "how", NULL }; - static _PYFUNC *row_converters[] = - { - _mysql_row_to_tuple, - _mysql_row_to_dict, - _mysql_row_to_dict_old - }; - _PYFUNC *convert_row; - int maxrows=1, how=0, skiprows=0, rowsadded; - PyObject *r=NULL; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii:fetch_row", kwlist, - &maxrows, &how)) - return NULL; - check_result_connection(self); - if (how >= (int)sizeof(row_converters)) { - PyErr_SetString(PyExc_ValueError, "how out of range"); - return NULL; - } - convert_row = row_converters[how]; - if (maxrows) { - if (!(r = PyTuple_New(maxrows))) goto error; - rowsadded = _mysql__fetch_row(self, &r, skiprows, maxrows, - convert_row); - if (rowsadded == -1) goto error; - } else { - if (self->use) { - maxrows = 1000; - if (!(r = PyTuple_New(maxrows))) goto error; - while (1) { - rowsadded = _mysql__fetch_row(self, &r, skiprows, - maxrows, convert_row); - if (rowsadded == -1) goto error; - skiprows += rowsadded; - if (rowsadded < maxrows) break; - if (_PyTuple_Resize(&r, skiprows+maxrows) == -1) - goto error; - } - } else { - /* XXX if overflow, maxrows<0? */ - maxrows = (int) mysql_num_rows(self->result); - if (!(r = PyTuple_New(maxrows))) goto error; - rowsadded = _mysql__fetch_row(self, &r, 0, - maxrows, convert_row); - if (rowsadded == -1) goto error; - } - } - return r; + _mysql_ResultObject *self, + PyObject *args, + PyObject *kwargs) +{ + typedef PyObject *_PYFUNC(_mysql_ResultObject *, MYSQL_ROW); + static char *kwlist[] = { "maxrows", "how", NULL }; + static _PYFUNC *row_converters[] = + { + _mysql_row_to_tuple, + _mysql_row_to_dict, + _mysql_row_to_dict_old + }; + _PYFUNC *convert_row; + int maxrows=1, how=0, skiprows=0, rowsadded; + PyObject *r=NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii:fetch_row", kwlist, + &maxrows, &how)) + return NULL; + check_result_connection(self); + if (how >= (int)sizeof(row_converters)) { + PyErr_SetString(PyExc_ValueError, "how out of range"); + return NULL; + } + convert_row = row_converters[how]; + if (maxrows) { + if (!(r = PyTuple_New(maxrows))) goto error; + rowsadded = _mysql__fetch_row(self, &r, skiprows, maxrows, + convert_row); + if (rowsadded == -1) goto error; + } else { + if (self->use) { + maxrows = 1000; + if (!(r = PyTuple_New(maxrows))) goto error; + while (1) { + rowsadded = _mysql__fetch_row(self, &r, skiprows, + maxrows, convert_row); + if (rowsadded == -1) goto error; + skiprows += rowsadded; + if (rowsadded < maxrows) break; + if (_PyTuple_Resize(&r, skiprows+maxrows) == -1) + goto error; + } + } else { + /* XXX if overflow, maxrows<0? */ + maxrows = (int) mysql_num_rows(self->result); + if (!(r = PyTuple_New(maxrows))) goto error; + rowsadded = _mysql__fetch_row(self, &r, 0, + maxrows, convert_row); + if (rowsadded == -1) goto error; + } + } + return r; error: - Py_XDECREF(r); - return NULL; + Py_XDECREF(r); + return NULL; } static char _mysql_ConnectionObject_change_user__doc__[] = @@ -1446,24 +1446,24 @@ a default database.\n\ static PyObject * _mysql_ConnectionObject_change_user( - _mysql_ConnectionObject *self, - PyObject *args, - PyObject *kwargs) + _mysql_ConnectionObject *self, + PyObject *args, + PyObject *kwargs) { - char *user, *pwd=NULL, *db=NULL; - int r; + char *user, *pwd=NULL, *db=NULL; + int r; static char *kwlist[] = { "user", "passwd", "db", NULL } ; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ss:change_user", - kwlist, &user, &pwd, &db)) - return NULL; - check_connection(self); - Py_BEGIN_ALLOW_THREADS - r = mysql_change_user(&(self->connection), user, pwd, db); - Py_END_ALLOW_THREADS - if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ss:change_user", + kwlist, &user, &pwd, &db)) + return NULL; + check_connection(self); + Py_BEGIN_ALLOW_THREADS + r = mysql_change_user(&(self->connection), user, pwd, db); + Py_END_ALLOW_THREADS + if (r) return _mysql_Exception(self); + Py_INCREF(Py_None); + return Py_None; } static char _mysql_ConnectionObject_character_set_name__doc__[] = @@ -1473,13 +1473,13 @@ Non-standard.\n\ static PyObject * _mysql_ConnectionObject_character_set_name( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - const char *s; - check_connection(self); - s = mysql_character_set_name(&(self->connection)); - return PyString_FromString(s); + const char *s; + check_connection(self); + s = mysql_character_set_name(&(self->connection)); + return PyString_FromString(s); } static char _mysql_ConnectionObject_set_character_set__doc__[] = @@ -1489,19 +1489,19 @@ Non-standard.\n\ static PyObject * _mysql_ConnectionObject_set_character_set( - _mysql_ConnectionObject *self, - PyObject *args) + _mysql_ConnectionObject *self, + PyObject *args) { - const char *s; - int err; - if (!PyArg_ParseTuple(args, "s", &s)) return NULL; - check_connection(self); - Py_BEGIN_ALLOW_THREADS - err = mysql_set_character_set(&(self->connection), s); - Py_END_ALLOW_THREADS - if (err) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + const char *s; + int err; + if (!PyArg_ParseTuple(args, "s", &s)) return NULL; + check_connection(self); + Py_BEGIN_ALLOW_THREADS + err = mysql_set_character_set(&(self->connection), s); + Py_END_ALLOW_THREADS + if (err) return _mysql_Exception(self); + Py_INCREF(Py_None); + return Py_None; } #if MYSQL_VERSION_ID >= 50010 @@ -1528,26 +1528,26 @@ Non-standard.\n\ static PyObject * _mysql_ConnectionObject_get_character_set_info( - _mysql_ConnectionObject *self, - PyObject *noargs) -{ - PyObject *result; - MY_CHARSET_INFO cs; - - check_connection(self); - mysql_get_character_set_info(&(self->connection), &cs); - if (!(result = PyDict_New())) return NULL; - if (cs.csname) - PyDict_SetItemString(result, "name", PyString_FromString(cs.csname)); - if (cs.name) - PyDict_SetItemString(result, "collation", PyString_FromString(cs.name)); - if (cs.comment) - PyDict_SetItemString(result, "comment", PyString_FromString(cs.comment)); - if (cs.dir) - PyDict_SetItemString(result, "dir", PyString_FromString(cs.dir)); - PyDict_SetItemString(result, "mbminlen", PyInt_FromLong(cs.mbminlen)); - PyDict_SetItemString(result, "mbmaxlen", PyInt_FromLong(cs.mbmaxlen)); - return result; + _mysql_ConnectionObject *self, + PyObject *noargs) +{ + PyObject *result; + MY_CHARSET_INFO cs; + + check_connection(self); + mysql_get_character_set_info(&(self->connection), &cs); + if (!(result = PyDict_New())) return NULL; + if (cs.csname) + PyDict_SetItemString(result, "name", PyString_FromString(cs.csname)); + if (cs.name) + PyDict_SetItemString(result, "collation", PyString_FromString(cs.name)); + if (cs.comment) + PyDict_SetItemString(result, "comment", PyString_FromString(cs.comment)); + if (cs.dir) + PyDict_SetItemString(result, "dir", PyString_FromString(cs.dir)); + PyDict_SetItemString(result, "mbminlen", PyInt_FromLong(cs.mbminlen)); + PyDict_SetItemString(result, "mbmaxlen", PyInt_FromLong(cs.mbmaxlen)); + return result; } #endif @@ -1562,14 +1562,14 @@ NOTE: this is a private API introduced ONLY for XTA integration,\n\ static PyObject * _mysql_ConnectionObject_get_native_connection( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - PyObject *result; - check_connection(self); - result = PyCapsule_New(&(self->connection), - "_mysql.connection.native_connection", NULL); - return result; + PyObject *result; + check_connection(self); + result = PyCapsule_New(&(self->connection), + "_mysql.connection.native_connection", NULL); + return result; } @@ -1578,10 +1578,10 @@ static char _mysql_get_client_info__doc__[] = the client library version."; static PyObject * _mysql_get_client_info( - PyObject *self, - PyObject *noargs) + PyObject *self, + PyObject *noargs) { - return PyString_FromString(mysql_get_client_info()); + return PyString_FromString(mysql_get_client_info()); } static char _mysql_ConnectionObject_get_host_info__doc__[] = @@ -1591,11 +1591,11 @@ version. Non-standard.\n\ static PyObject * _mysql_ConnectionObject_get_host_info( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - check_connection(self); - return PyString_FromString(mysql_get_host_info(&(self->connection))); + check_connection(self); + return PyString_FromString(mysql_get_host_info(&(self->connection))); } static char _mysql_ConnectionObject_get_proto_info__doc__[] = @@ -1605,11 +1605,11 @@ used by the current connection. Non-standard.\n\ static PyObject * _mysql_ConnectionObject_get_proto_info( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - check_connection(self); - return PyInt_FromLong((long)mysql_get_proto_info(&(self->connection))); + check_connection(self); + return PyInt_FromLong((long)mysql_get_proto_info(&(self->connection))); } static char _mysql_ConnectionObject_get_server_info__doc__[] = @@ -1619,11 +1619,11 @@ Non-standard.\n\ static PyObject * _mysql_ConnectionObject_get_server_info( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - check_connection(self); - return PyString_FromString(mysql_get_server_info(&(self->connection))); + check_connection(self); + return PyString_FromString(mysql_get_server_info(&(self->connection))); } static char _mysql_ConnectionObject_info__doc__[] = @@ -1634,15 +1634,15 @@ Cursor.messages.\n\ static PyObject * _mysql_ConnectionObject_info( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - const char *s; - check_connection(self); - s = mysql_info(&(self->connection)); - if (s) return PyString_FromString(s); - Py_INCREF(Py_None); - return Py_None; + const char *s; + check_connection(self); + s = mysql_info(&(self->connection)); + if (s) return PyString_FromString(s); + Py_INCREF(Py_None); + return Py_None; } static char _mysql_ConnectionObject_insert_id__doc__[] = @@ -1668,15 +1668,15 @@ in the server.\n\ static PyObject * _mysql_ConnectionObject_insert_id( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - my_ulonglong r; - check_connection(self); - Py_BEGIN_ALLOW_THREADS - r = mysql_insert_id(&(self->connection)); - Py_END_ALLOW_THREADS - return PyLong_FromUnsignedLongLong(r); + my_ulonglong r; + check_connection(self); + Py_BEGIN_ALLOW_THREADS + r = mysql_insert_id(&(self->connection)); + Py_END_ALLOW_THREADS + return PyLong_FromUnsignedLongLong(r); } static char _mysql_ConnectionObject_kill__doc__[] = @@ -1685,19 +1685,19 @@ Non-standard."; static PyObject * _mysql_ConnectionObject_kill( - _mysql_ConnectionObject *self, - PyObject *args) + _mysql_ConnectionObject *self, + PyObject *args) { - unsigned long pid; - int r; - if (!PyArg_ParseTuple(args, "k:kill", &pid)) return NULL; - check_connection(self); - Py_BEGIN_ALLOW_THREADS - r = mysql_kill(&(self->connection), pid); - Py_END_ALLOW_THREADS - if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + unsigned long pid; + int r; + if (!PyArg_ParseTuple(args, "k:kill", &pid)) return NULL; + check_connection(self); + Py_BEGIN_ALLOW_THREADS + r = mysql_kill(&(self->connection), pid); + Py_END_ALLOW_THREADS + if (r) return _mysql_Exception(self); + Py_INCREF(Py_None); + return Py_None; } static char _mysql_ConnectionObject_field_count__doc__[] = @@ -1708,24 +1708,24 @@ on most cursor classes. Use Cursor.rowcount.\n\ static PyObject * _mysql_ConnectionObject_field_count( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - check_connection(self); - return PyInt_FromLong((long)mysql_field_count(&(self->connection))); -} + check_connection(self); + return PyInt_FromLong((long)mysql_field_count(&(self->connection))); +} static char _mysql_ResultObject_num_fields__doc__[] = "Returns the number of fields (column) in the result." ; static PyObject * _mysql_ResultObject_num_fields( - _mysql_ResultObject *self, - PyObject *noargs) + _mysql_ResultObject *self, + PyObject *noargs) { - check_result_connection(self); - return PyInt_FromLong((long)mysql_num_fields(self->result)); -} + check_result_connection(self); + return PyInt_FromLong((long)mysql_num_fields(self->result)); +} static char _mysql_ResultObject_num_rows__doc__[] = "Returns the number of rows in the result set. Note that if\n\ @@ -1735,12 +1735,12 @@ set has been read.\n\ static PyObject * _mysql_ResultObject_num_rows( - _mysql_ResultObject *self, - PyObject *noargs) + _mysql_ResultObject *self, + PyObject *noargs) { - check_result_connection(self); - return PyLong_FromUnsignedLongLong(mysql_num_rows(self->result)); -} + check_result_connection(self); + return PyLong_FromUnsignedLongLong(mysql_num_rows(self->result)); +} static char _mysql_ConnectionObject_ping__doc__[] = "Checks whether or not the connection to the server is\n\ @@ -1763,22 +1763,22 @@ You have been warned.\n\ static PyObject * _mysql_ConnectionObject_ping( - _mysql_ConnectionObject *self, - PyObject *args) -{ - int r, reconnect = -1; - if (!PyArg_ParseTuple(args, "|I", &reconnect)) return NULL; - check_connection(self); - if (reconnect != -1) { - my_bool recon = (my_bool)reconnect; - mysql_options(&self->connection, MYSQL_OPT_RECONNECT, &recon); - } - Py_BEGIN_ALLOW_THREADS - r = mysql_ping(&(self->connection)); - Py_END_ALLOW_THREADS - if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + _mysql_ConnectionObject *self, + PyObject *args) +{ + int r, reconnect = -1; + if (!PyArg_ParseTuple(args, "|I", &reconnect)) return NULL; + check_connection(self); + if (reconnect != -1) { + my_bool recon = (my_bool)reconnect; + mysql_options(&self->connection, MYSQL_OPT_RECONNECT, &recon); + } + Py_BEGIN_ALLOW_THREADS + r = mysql_ping(&(self->connection)); + Py_END_ALLOW_THREADS + if (r) return _mysql_Exception(self); + Py_INCREF(Py_None); + return Py_None; } static char _mysql_ConnectionObject_query__doc__[] = @@ -1789,20 +1789,20 @@ then cursor.execute().\n\ static PyObject * _mysql_ConnectionObject_query( - _mysql_ConnectionObject *self, - PyObject *args) + _mysql_ConnectionObject *self, + PyObject *args) { - char *query; - int len, r; - if (!PyArg_ParseTuple(args, "s#:query", &query, &len)) return NULL; - check_connection(self); + char *query; + int len, r; + if (!PyArg_ParseTuple(args, "s#:query", &query, &len)) return NULL; + check_connection(self); - Py_BEGIN_ALLOW_THREADS - r = mysql_real_query(&(self->connection), query, len); - Py_END_ALLOW_THREADS - if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_BEGIN_ALLOW_THREADS + r = mysql_real_query(&(self->connection), query, len); + Py_END_ALLOW_THREADS + if (r) return _mysql_Exception(self); + Py_INCREF(Py_None); + return Py_None; } @@ -1812,21 +1812,21 @@ Use read_query_result() before calling store_result() or use_result()\n"; static PyObject * _mysql_ConnectionObject_send_query( - _mysql_ConnectionObject *self, - PyObject *args) + _mysql_ConnectionObject *self, + PyObject *args) { - char *query; - int len, r; - MYSQL *mysql = &(self->connection); - if (!PyArg_ParseTuple(args, "s#:query", &query, &len)) return NULL; - check_connection(self); + char *query; + int len, r; + MYSQL *mysql = &(self->connection); + if (!PyArg_ParseTuple(args, "s#:query", &query, &len)) return NULL; + check_connection(self); - Py_BEGIN_ALLOW_THREADS - r = mysql_send_query(mysql, query, len); - Py_END_ALLOW_THREADS - if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_BEGIN_ALLOW_THREADS + r = mysql_send_query(mysql, query, len); + Py_END_ALLOW_THREADS + if (r) return _mysql_Exception(self); + Py_INCREF(Py_None); + return Py_None; } @@ -1835,19 +1835,19 @@ static char _mysql_ConnectionObject_read_query_result__doc__[] = static PyObject * _mysql_ConnectionObject_read_query_result( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - int r; - MYSQL *mysql = &(self->connection); - check_connection(self); + int r; + MYSQL *mysql = &(self->connection); + check_connection(self); - Py_BEGIN_ALLOW_THREADS - r = (int)mysql_read_query_result(mysql); - Py_END_ALLOW_THREADS - if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_BEGIN_ALLOW_THREADS + r = (int)mysql_read_query_result(mysql); + Py_END_ALLOW_THREADS + if (r) return _mysql_Exception(self); + Py_INCREF(Py_None); + return Py_None; } static char _mysql_ConnectionObject_select_db__doc__[] = @@ -1864,19 +1864,19 @@ Non-standard.\n\ static PyObject * _mysql_ConnectionObject_select_db( - _mysql_ConnectionObject *self, - PyObject *args) + _mysql_ConnectionObject *self, + PyObject *args) { - char *db; - int r; - if (!PyArg_ParseTuple(args, "s:select_db", &db)) return NULL; - check_connection(self); - Py_BEGIN_ALLOW_THREADS - r = mysql_select_db(&(self->connection), db); - Py_END_ALLOW_THREADS - if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + char *db; + int r; + if (!PyArg_ParseTuple(args, "s:select_db", &db)) return NULL; + check_connection(self); + Py_BEGIN_ALLOW_THREADS + r = mysql_select_db(&(self->connection), db); + Py_END_ALLOW_THREADS + if (r) return _mysql_Exception(self); + Py_INCREF(Py_None); + return Py_None; } static char _mysql_ConnectionObject_shutdown__doc__[] = @@ -1886,17 +1886,17 @@ have shutdown privileges. Non-standard.\n\ static PyObject * _mysql_ConnectionObject_shutdown( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - int r; - check_connection(self); - Py_BEGIN_ALLOW_THREADS - r = mysql_shutdown(&(self->connection), SHUTDOWN_DEFAULT); - Py_END_ALLOW_THREADS - if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + int r; + check_connection(self); + Py_BEGIN_ALLOW_THREADS + r = mysql_shutdown(&(self->connection), SHUTDOWN_DEFAULT); + Py_END_ALLOW_THREADS + if (r) return _mysql_Exception(self); + Py_INCREF(Py_None); + return Py_None; } static char _mysql_ConnectionObject_stat__doc__[] = @@ -1908,16 +1908,16 @@ questions, reloads, and open tables. Non-standard.\n\ static PyObject * _mysql_ConnectionObject_stat( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - const char *s; - check_connection(self); - Py_BEGIN_ALLOW_THREADS - s = mysql_stat(&(self->connection)); - Py_END_ALLOW_THREADS - if (!s) return _mysql_Exception(self); - return PyString_FromString(s); + const char *s; + check_connection(self); + Py_BEGIN_ALLOW_THREADS + s = mysql_stat(&(self->connection)); + Py_END_ALLOW_THREADS + if (!s) return _mysql_Exception(self); + return PyString_FromString(s); } static char _mysql_ConnectionObject_store_result__doc__[] = @@ -1928,31 +1928,31 @@ None is returned. Non-standard.\n\ static PyObject * _mysql_ConnectionObject_store_result( - _mysql_ConnectionObject *self, - PyObject *noargs) -{ - PyObject *arglist=NULL, *kwarglist=NULL, *result=NULL; - _mysql_ResultObject *r=NULL; - - check_connection(self); - arglist = Py_BuildValue("(OiO)", self, 0, self->converter); - if (!arglist) goto error; - kwarglist = PyDict_New(); - if (!kwarglist) goto error; - r = MyAlloc(_mysql_ResultObject, _mysql_ResultObject_Type); - if (!r) goto error; - if (_mysql_ResultObject_Initialize(r, arglist, kwarglist)) - goto error; - result = (PyObject *) r; - if (!(r->result)) { - Py_DECREF(result); - Py_INCREF(Py_None); - result = Py_None; - } + _mysql_ConnectionObject *self, + PyObject *noargs) +{ + PyObject *arglist=NULL, *kwarglist=NULL, *result=NULL; + _mysql_ResultObject *r=NULL; + + check_connection(self); + arglist = Py_BuildValue("(OiO)", self, 0, self->converter); + if (!arglist) goto error; + kwarglist = PyDict_New(); + if (!kwarglist) goto error; + r = MyAlloc(_mysql_ResultObject, _mysql_ResultObject_Type); + if (!r) goto error; + if (_mysql_ResultObject_Initialize(r, arglist, kwarglist)) + goto error; + result = (PyObject *) r; + if (!(r->result)) { + Py_DECREF(result); + Py_INCREF(Py_None); + result = Py_None; + } error: - Py_XDECREF(arglist); - Py_XDECREF(kwarglist); - return result; + Py_XDECREF(arglist); + Py_XDECREF(kwarglist); + return result; } static char _mysql_ConnectionObject_thread_id__doc__[] = @@ -1968,15 +1968,15 @@ Non-standard."; static PyObject * _mysql_ConnectionObject_thread_id( - _mysql_ConnectionObject *self, - PyObject *noargs) + _mysql_ConnectionObject *self, + PyObject *noargs) { - unsigned long pid; - check_connection(self); - Py_BEGIN_ALLOW_THREADS - pid = mysql_thread_id(&(self->connection)); - Py_END_ALLOW_THREADS - return PyInt_FromLong((long)pid); + unsigned long pid; + check_connection(self); + Py_BEGIN_ALLOW_THREADS + pid = mysql_thread_id(&(self->connection)); + Py_END_ALLOW_THREADS + return PyInt_FromLong((long)pid); } static char _mysql_ConnectionObject_use_result__doc__[] = @@ -1987,59 +1987,59 @@ None is returned. Non-standard.\n\ static PyObject * _mysql_ConnectionObject_use_result( - _mysql_ConnectionObject *self, - PyObject *noargs) -{ - PyObject *arglist=NULL, *kwarglist=NULL, *result=NULL; - _mysql_ResultObject *r=NULL; - - check_connection(self); - arglist = Py_BuildValue("(OiO)", self, 1, self->converter); - if (!arglist) return NULL; - kwarglist = PyDict_New(); - if (!kwarglist) goto error; - r = MyAlloc(_mysql_ResultObject, _mysql_ResultObject_Type); - if (!r) goto error; - result = (PyObject *) r; - if (_mysql_ResultObject_Initialize(r, arglist, kwarglist)) - goto error; - if (!(r->result)) { - Py_DECREF(result); - Py_INCREF(Py_None); - result = Py_None; - } + _mysql_ConnectionObject *self, + PyObject *noargs) +{ + PyObject *arglist=NULL, *kwarglist=NULL, *result=NULL; + _mysql_ResultObject *r=NULL; + + check_connection(self); + arglist = Py_BuildValue("(OiO)", self, 1, self->converter); + if (!arglist) return NULL; + kwarglist = PyDict_New(); + if (!kwarglist) goto error; + r = MyAlloc(_mysql_ResultObject, _mysql_ResultObject_Type); + if (!r) goto error; + result = (PyObject *) r; + if (_mysql_ResultObject_Initialize(r, arglist, kwarglist)) + goto error; + if (!(r->result)) { + Py_DECREF(result); + Py_INCREF(Py_None); + result = Py_None; + } error: - Py_DECREF(arglist); - Py_XDECREF(kwarglist); - return result; + Py_DECREF(arglist); + Py_XDECREF(kwarglist); + return result; } static void _mysql_ConnectionObject_dealloc( - _mysql_ConnectionObject *self) + _mysql_ConnectionObject *self) { - PyObject_GC_UnTrack(self); - if (self->open) { - mysql_close(&(self->connection)); - self->open = 0; - } - Py_CLEAR(self->converter); - MyFree(self); + PyObject_GC_UnTrack(self); + if (self->open) { + mysql_close(&(self->connection)); + self->open = 0; + } + Py_CLEAR(self->converter); + MyFree(self); } static PyObject * _mysql_ConnectionObject_repr( - _mysql_ConnectionObject *self) + _mysql_ConnectionObject *self) { - char buf[300]; - if (self->open) - sprintf(buf, "<_mysql.connection open to '%.256s' at %lx>", - self->connection.host, - (long)self); - else - sprintf(buf, "<_mysql.connection closed at %lx>", - (long)self); - return PyString_FromString(buf); + char buf[300]; + if (self->open) + sprintf(buf, "<_mysql.connection open to '%.256s' at %lx>", + self->connection.host, + (long)self); + else + sprintf(buf, "<_mysql.connection closed at %lx>", + (long)self); + return PyString_FromString(buf); } static char _mysql_ResultObject_data_seek__doc__[] = @@ -2049,623 +2049,623 @@ _mysql_ResultObject_data_seek( _mysql_ResultObject *self, PyObject *args) { - unsigned int row; - if (!PyArg_ParseTuple(args, "i:data_seek", &row)) return NULL; - check_result_connection(self); - mysql_data_seek(self->result, row); - Py_INCREF(Py_None); - return Py_None; + unsigned int row; + if (!PyArg_ParseTuple(args, "i:data_seek", &row)) return NULL; + check_result_connection(self); + mysql_data_seek(self->result, row); + Py_INCREF(Py_None); + return Py_None; } static void _mysql_ResultObject_dealloc( - _mysql_ResultObject *self) + _mysql_ResultObject *self) { - PyObject_GC_UnTrack((PyObject *)self); - mysql_free_result(self->result); - _mysql_ResultObject_clear(self); - MyFree(self); + PyObject_GC_UnTrack((PyObject *)self); + mysql_free_result(self->result); + _mysql_ResultObject_clear(self); + MyFree(self); } static PyObject * _mysql_ResultObject_repr( - _mysql_ResultObject *self) + _mysql_ResultObject *self) { - char buf[300]; - sprintf(buf, "<_mysql.result object at %lx>", (long)self); - return PyString_FromString(buf); + char buf[300]; + sprintf(buf, "<_mysql.result object at %lx>", (long)self); + return PyString_FromString(buf); } static PyMethodDef _mysql_ConnectionObject_methods[] = { - { - "affected_rows", - (PyCFunction)_mysql_ConnectionObject_affected_rows, - METH_NOARGS, - _mysql_ConnectionObject_affected_rows__doc__ - }, - { - "autocommit", - (PyCFunction)_mysql_ConnectionObject_autocommit, - METH_VARARGS, - _mysql_ConnectionObject_autocommit__doc__ - }, - { - "get_autocommit", - (PyCFunction)_mysql_ConnectionObject_get_autocommit, - METH_NOARGS, - _mysql_ConnectionObject_get_autocommit__doc__ - }, - { - "commit", - (PyCFunction)_mysql_ConnectionObject_commit, - METH_NOARGS, - _mysql_ConnectionObject_commit__doc__ - }, - { - "rollback", - (PyCFunction)_mysql_ConnectionObject_rollback, - METH_NOARGS, - _mysql_ConnectionObject_rollback__doc__ - }, - { - "next_result", - (PyCFunction)_mysql_ConnectionObject_next_result, - METH_NOARGS, - _mysql_ConnectionObject_next_result__doc__ - }, - { - "set_server_option", - (PyCFunction)_mysql_ConnectionObject_set_server_option, - METH_VARARGS, - _mysql_ConnectionObject_set_server_option__doc__ - }, - { - "sqlstate", - (PyCFunction)_mysql_ConnectionObject_sqlstate, - METH_NOARGS, - _mysql_ConnectionObject_sqlstate__doc__ - }, - { - "warning_count", - (PyCFunction)_mysql_ConnectionObject_warning_count, - METH_NOARGS, - _mysql_ConnectionObject_warning_count__doc__ - }, - { - "change_user", - (PyCFunction)_mysql_ConnectionObject_change_user, - METH_VARARGS | METH_KEYWORDS, - _mysql_ConnectionObject_change_user__doc__ - }, - { - "character_set_name", - (PyCFunction)_mysql_ConnectionObject_character_set_name, - METH_NOARGS, - _mysql_ConnectionObject_character_set_name__doc__ - }, - { - "set_character_set", - (PyCFunction)_mysql_ConnectionObject_set_character_set, - METH_VARARGS, - _mysql_ConnectionObject_set_character_set__doc__ - }, + { + "affected_rows", + (PyCFunction)_mysql_ConnectionObject_affected_rows, + METH_NOARGS, + _mysql_ConnectionObject_affected_rows__doc__ + }, + { + "autocommit", + (PyCFunction)_mysql_ConnectionObject_autocommit, + METH_VARARGS, + _mysql_ConnectionObject_autocommit__doc__ + }, + { + "get_autocommit", + (PyCFunction)_mysql_ConnectionObject_get_autocommit, + METH_NOARGS, + _mysql_ConnectionObject_get_autocommit__doc__ + }, + { + "commit", + (PyCFunction)_mysql_ConnectionObject_commit, + METH_NOARGS, + _mysql_ConnectionObject_commit__doc__ + }, + { + "rollback", + (PyCFunction)_mysql_ConnectionObject_rollback, + METH_NOARGS, + _mysql_ConnectionObject_rollback__doc__ + }, + { + "next_result", + (PyCFunction)_mysql_ConnectionObject_next_result, + METH_NOARGS, + _mysql_ConnectionObject_next_result__doc__ + }, + { + "set_server_option", + (PyCFunction)_mysql_ConnectionObject_set_server_option, + METH_VARARGS, + _mysql_ConnectionObject_set_server_option__doc__ + }, + { + "sqlstate", + (PyCFunction)_mysql_ConnectionObject_sqlstate, + METH_NOARGS, + _mysql_ConnectionObject_sqlstate__doc__ + }, + { + "warning_count", + (PyCFunction)_mysql_ConnectionObject_warning_count, + METH_NOARGS, + _mysql_ConnectionObject_warning_count__doc__ + }, + { + "change_user", + (PyCFunction)_mysql_ConnectionObject_change_user, + METH_VARARGS | METH_KEYWORDS, + _mysql_ConnectionObject_change_user__doc__ + }, + { + "character_set_name", + (PyCFunction)_mysql_ConnectionObject_character_set_name, + METH_NOARGS, + _mysql_ConnectionObject_character_set_name__doc__ + }, + { + "set_character_set", + (PyCFunction)_mysql_ConnectionObject_set_character_set, + METH_VARARGS, + _mysql_ConnectionObject_set_character_set__doc__ + }, #if MYSQL_VERSION_ID >= 50010 - { - "get_character_set_info", - (PyCFunction)_mysql_ConnectionObject_get_character_set_info, - METH_NOARGS, - _mysql_ConnectionObject_get_character_set_info__doc__ - }, + { + "get_character_set_info", + (PyCFunction)_mysql_ConnectionObject_get_character_set_info, + METH_NOARGS, + _mysql_ConnectionObject_get_character_set_info__doc__ + }, #endif - { - "_get_native_connection", - (PyCFunction)_mysql_ConnectionObject_get_native_connection, - METH_NOARGS, - _mysql_ConnectionObject_get_native_connection__doc__ - }, - { - "close", - (PyCFunction)_mysql_ConnectionObject_close, - METH_NOARGS, - _mysql_ConnectionObject_close__doc__ - }, - { - "fileno", - (PyCFunction)_mysql_ConnectionObject_fileno, - METH_NOARGS, - _mysql_ConnectionObject_fileno__doc__ - }, - { - "dump_debug_info", - (PyCFunction)_mysql_ConnectionObject_dump_debug_info, - METH_NOARGS, - _mysql_ConnectionObject_dump_debug_info__doc__ - }, - { - "escape", - (PyCFunction)_mysql_escape, - METH_VARARGS, - _mysql_escape__doc__ - }, - { - "escape_string", - (PyCFunction)_mysql_escape_string, - METH_VARARGS, - _mysql_escape_string__doc__ - }, - { - "error", - (PyCFunction)_mysql_ConnectionObject_error, - METH_NOARGS, - _mysql_ConnectionObject_error__doc__ - }, - { - "errno", - (PyCFunction)_mysql_ConnectionObject_errno, - METH_NOARGS, - _mysql_ConnectionObject_errno__doc__ - }, - { - "field_count", - (PyCFunction)_mysql_ConnectionObject_field_count, - METH_NOARGS, - _mysql_ConnectionObject_field_count__doc__ - }, - { - "get_host_info", - (PyCFunction)_mysql_ConnectionObject_get_host_info, - METH_NOARGS, - _mysql_ConnectionObject_get_host_info__doc__ - }, - { - "get_proto_info", - (PyCFunction)_mysql_ConnectionObject_get_proto_info, - METH_NOARGS, - _mysql_ConnectionObject_get_proto_info__doc__ - }, - { - "get_server_info", - (PyCFunction)_mysql_ConnectionObject_get_server_info, - METH_NOARGS, - _mysql_ConnectionObject_get_server_info__doc__ - }, - { - "info", - (PyCFunction)_mysql_ConnectionObject_info, - METH_NOARGS, - _mysql_ConnectionObject_info__doc__ - }, - { - "insert_id", - (PyCFunction)_mysql_ConnectionObject_insert_id, - METH_NOARGS, - _mysql_ConnectionObject_insert_id__doc__ - }, - { - "kill", - (PyCFunction)_mysql_ConnectionObject_kill, - METH_VARARGS, - _mysql_ConnectionObject_kill__doc__ - }, - { - "ping", - (PyCFunction)_mysql_ConnectionObject_ping, - METH_VARARGS, - _mysql_ConnectionObject_ping__doc__ - }, - { - "query", - (PyCFunction)_mysql_ConnectionObject_query, - METH_VARARGS, - _mysql_ConnectionObject_query__doc__ - }, - { - "send_query", - (PyCFunction)_mysql_ConnectionObject_send_query, - METH_VARARGS, - _mysql_ConnectionObject_send_query__doc__, - }, - { - "read_query_result", - (PyCFunction)_mysql_ConnectionObject_read_query_result, - METH_NOARGS, - _mysql_ConnectionObject_read_query_result__doc__, - }, - { - "select_db", - (PyCFunction)_mysql_ConnectionObject_select_db, - METH_VARARGS, - _mysql_ConnectionObject_select_db__doc__ - }, - { - "shutdown", - (PyCFunction)_mysql_ConnectionObject_shutdown, - METH_NOARGS, - _mysql_ConnectionObject_shutdown__doc__ - }, - { - "stat", - (PyCFunction)_mysql_ConnectionObject_stat, - METH_NOARGS, - _mysql_ConnectionObject_stat__doc__ - }, - { - "store_result", - (PyCFunction)_mysql_ConnectionObject_store_result, - METH_NOARGS, - _mysql_ConnectionObject_store_result__doc__ - }, - { - "string_literal", - (PyCFunction)_mysql_string_literal, - METH_VARARGS, - _mysql_string_literal__doc__}, - { - "thread_id", - (PyCFunction)_mysql_ConnectionObject_thread_id, - METH_NOARGS, - _mysql_ConnectionObject_thread_id__doc__ - }, - { - "use_result", - (PyCFunction)_mysql_ConnectionObject_use_result, - METH_NOARGS, - _mysql_ConnectionObject_use_result__doc__ - }, - {NULL, NULL} /* sentinel */ + { + "_get_native_connection", + (PyCFunction)_mysql_ConnectionObject_get_native_connection, + METH_NOARGS, + _mysql_ConnectionObject_get_native_connection__doc__ + }, + { + "close", + (PyCFunction)_mysql_ConnectionObject_close, + METH_NOARGS, + _mysql_ConnectionObject_close__doc__ + }, + { + "fileno", + (PyCFunction)_mysql_ConnectionObject_fileno, + METH_NOARGS, + _mysql_ConnectionObject_fileno__doc__ + }, + { + "dump_debug_info", + (PyCFunction)_mysql_ConnectionObject_dump_debug_info, + METH_NOARGS, + _mysql_ConnectionObject_dump_debug_info__doc__ + }, + { + "escape", + (PyCFunction)_mysql_escape, + METH_VARARGS, + _mysql_escape__doc__ + }, + { + "escape_string", + (PyCFunction)_mysql_escape_string, + METH_VARARGS, + _mysql_escape_string__doc__ + }, + { + "error", + (PyCFunction)_mysql_ConnectionObject_error, + METH_NOARGS, + _mysql_ConnectionObject_error__doc__ + }, + { + "errno", + (PyCFunction)_mysql_ConnectionObject_errno, + METH_NOARGS, + _mysql_ConnectionObject_errno__doc__ + }, + { + "field_count", + (PyCFunction)_mysql_ConnectionObject_field_count, + METH_NOARGS, + _mysql_ConnectionObject_field_count__doc__ + }, + { + "get_host_info", + (PyCFunction)_mysql_ConnectionObject_get_host_info, + METH_NOARGS, + _mysql_ConnectionObject_get_host_info__doc__ + }, + { + "get_proto_info", + (PyCFunction)_mysql_ConnectionObject_get_proto_info, + METH_NOARGS, + _mysql_ConnectionObject_get_proto_info__doc__ + }, + { + "get_server_info", + (PyCFunction)_mysql_ConnectionObject_get_server_info, + METH_NOARGS, + _mysql_ConnectionObject_get_server_info__doc__ + }, + { + "info", + (PyCFunction)_mysql_ConnectionObject_info, + METH_NOARGS, + _mysql_ConnectionObject_info__doc__ + }, + { + "insert_id", + (PyCFunction)_mysql_ConnectionObject_insert_id, + METH_NOARGS, + _mysql_ConnectionObject_insert_id__doc__ + }, + { + "kill", + (PyCFunction)_mysql_ConnectionObject_kill, + METH_VARARGS, + _mysql_ConnectionObject_kill__doc__ + }, + { + "ping", + (PyCFunction)_mysql_ConnectionObject_ping, + METH_VARARGS, + _mysql_ConnectionObject_ping__doc__ + }, + { + "query", + (PyCFunction)_mysql_ConnectionObject_query, + METH_VARARGS, + _mysql_ConnectionObject_query__doc__ + }, + { + "send_query", + (PyCFunction)_mysql_ConnectionObject_send_query, + METH_VARARGS, + _mysql_ConnectionObject_send_query__doc__, + }, + { + "read_query_result", + (PyCFunction)_mysql_ConnectionObject_read_query_result, + METH_NOARGS, + _mysql_ConnectionObject_read_query_result__doc__, + }, + { + "select_db", + (PyCFunction)_mysql_ConnectionObject_select_db, + METH_VARARGS, + _mysql_ConnectionObject_select_db__doc__ + }, + { + "shutdown", + (PyCFunction)_mysql_ConnectionObject_shutdown, + METH_NOARGS, + _mysql_ConnectionObject_shutdown__doc__ + }, + { + "stat", + (PyCFunction)_mysql_ConnectionObject_stat, + METH_NOARGS, + _mysql_ConnectionObject_stat__doc__ + }, + { + "store_result", + (PyCFunction)_mysql_ConnectionObject_store_result, + METH_NOARGS, + _mysql_ConnectionObject_store_result__doc__ + }, + { + "string_literal", + (PyCFunction)_mysql_string_literal, + METH_VARARGS, + _mysql_string_literal__doc__}, + { + "thread_id", + (PyCFunction)_mysql_ConnectionObject_thread_id, + METH_NOARGS, + _mysql_ConnectionObject_thread_id__doc__ + }, + { + "use_result", + (PyCFunction)_mysql_ConnectionObject_use_result, + METH_NOARGS, + _mysql_ConnectionObject_use_result__doc__ + }, + {NULL, NULL} /* sentinel */ }; static struct PyMemberDef _mysql_ConnectionObject_memberlist[] = { - { - "open", - T_INT, - offsetof(_mysql_ConnectionObject,open), - READONLY, - "True if connection is open" - }, - { - "converter", - T_OBJECT, - offsetof(_mysql_ConnectionObject,converter), - 0, - "Type conversion mapping" - }, - { - "server_capabilities", - T_UINT, - offsetof(_mysql_ConnectionObject,connection.server_capabilities), - READONLY, - "Capabilities of server; consult MySQLdb.constants.CLIENT" - }, - { - "port", - T_UINT, - offsetof(_mysql_ConnectionObject,connection.port), - READONLY, - "TCP/IP port of the server connection" - }, - { - "client_flag", - T_UINT, - offsetof(_mysql_ConnectionObject,connection.client_flag), - READONLY, - "Client flags; refer to MySQLdb.constants.CLIENT" - }, - {NULL} /* Sentinel */ + { + "open", + T_INT, + offsetof(_mysql_ConnectionObject,open), + READONLY, + "True if connection is open" + }, + { + "converter", + T_OBJECT, + offsetof(_mysql_ConnectionObject,converter), + 0, + "Type conversion mapping" + }, + { + "server_capabilities", + T_UINT, + offsetof(_mysql_ConnectionObject,connection.server_capabilities), + READONLY, + "Capabilities of server; consult MySQLdb.constants.CLIENT" + }, + { + "port", + T_UINT, + offsetof(_mysql_ConnectionObject,connection.port), + READONLY, + "TCP/IP port of the server connection" + }, + { + "client_flag", + T_UINT, + offsetof(_mysql_ConnectionObject,connection.client_flag), + READONLY, + "Client flags; refer to MySQLdb.constants.CLIENT" + }, + {NULL} /* Sentinel */ }; static PyMethodDef _mysql_ResultObject_methods[] = { - { - "data_seek", - (PyCFunction)_mysql_ResultObject_data_seek, - METH_VARARGS, - _mysql_ResultObject_data_seek__doc__ - }, - { - "describe", - (PyCFunction)_mysql_ResultObject_describe, - METH_NOARGS, - _mysql_ResultObject_describe__doc__ - }, - { - "fetch_row", - (PyCFunction)_mysql_ResultObject_fetch_row, - METH_VARARGS | METH_KEYWORDS, - _mysql_ResultObject_fetch_row__doc__ - }, - { - "field_flags", - (PyCFunction)_mysql_ResultObject_field_flags, - METH_NOARGS, - _mysql_ResultObject_field_flags__doc__ - }, - { - "num_fields", - (PyCFunction)_mysql_ResultObject_num_fields, - METH_NOARGS, - _mysql_ResultObject_num_fields__doc__ - }, - { - "num_rows", - (PyCFunction)_mysql_ResultObject_num_rows, - METH_NOARGS, - _mysql_ResultObject_num_rows__doc__ - }, - {NULL, NULL} /* sentinel */ + { + "data_seek", + (PyCFunction)_mysql_ResultObject_data_seek, + METH_VARARGS, + _mysql_ResultObject_data_seek__doc__ + }, + { + "describe", + (PyCFunction)_mysql_ResultObject_describe, + METH_NOARGS, + _mysql_ResultObject_describe__doc__ + }, + { + "fetch_row", + (PyCFunction)_mysql_ResultObject_fetch_row, + METH_VARARGS | METH_KEYWORDS, + _mysql_ResultObject_fetch_row__doc__ + }, + { + "field_flags", + (PyCFunction)_mysql_ResultObject_field_flags, + METH_NOARGS, + _mysql_ResultObject_field_flags__doc__ + }, + { + "num_fields", + (PyCFunction)_mysql_ResultObject_num_fields, + METH_NOARGS, + _mysql_ResultObject_num_fields__doc__ + }, + { + "num_rows", + (PyCFunction)_mysql_ResultObject_num_rows, + METH_NOARGS, + _mysql_ResultObject_num_rows__doc__ + }, + {NULL, NULL} /* sentinel */ }; static struct PyMemberDef _mysql_ResultObject_memberlist[] = { - { - "converter", - T_OBJECT, - offsetof(_mysql_ResultObject,converter), - READONLY, - "Type conversion mapping" - }, - { - "has_next", - T_BOOL, - offsetof(_mysql_ResultObject, has_next), - READONLY, - "Has next result" - }, - {NULL} /* Sentinel */ + { + "converter", + T_OBJECT, + offsetof(_mysql_ResultObject,converter), + READONLY, + "Type conversion mapping" + }, + { + "has_next", + T_BOOL, + offsetof(_mysql_ResultObject, has_next), + READONLY, + "Has next result" + }, + {NULL} /* Sentinel */ }; static PyObject * _mysql_ConnectionObject_getattro( - _mysql_ConnectionObject *self, - PyObject *name) + _mysql_ConnectionObject *self, + PyObject *name) { - char *cname; + char *cname; #ifdef IS_PY3K - cname = PyUnicode_AsUTF8(name); + cname = PyUnicode_AsUTF8(name); #else - cname = PyString_AsString(name); + cname = PyString_AsString(name); #endif - if (strcmp(cname, "closed") == 0) - return PyInt_FromLong((long)!(self->open)); + if (strcmp(cname, "closed") == 0) + return PyInt_FromLong((long)!(self->open)); - return PyObject_GenericGetAttr((PyObject *)self, name); + return PyObject_GenericGetAttr((PyObject *)self, name); } static int _mysql_ConnectionObject_setattro( - _mysql_ConnectionObject *self, - PyObject *name, - PyObject *v) -{ - if (v == NULL) { - PyErr_SetString(PyExc_AttributeError, - "can't delete connection attributes"); - return -1; - } - return PyObject_GenericSetAttr((PyObject *)self, name, v); + _mysql_ConnectionObject *self, + PyObject *name, + PyObject *v) +{ + if (v == NULL) { + PyErr_SetString(PyExc_AttributeError, + "can't delete connection attributes"); + return -1; + } + return PyObject_GenericSetAttr((PyObject *)self, name, v); } static int _mysql_ResultObject_setattro( - _mysql_ResultObject *self, - PyObject *name, - PyObject *v) -{ - if (v == NULL) { - PyErr_SetString(PyExc_AttributeError, - "can't delete connection attributes"); - return -1; - } - return PyObject_GenericSetAttr((PyObject *)self, name, v); + _mysql_ResultObject *self, + PyObject *name, + PyObject *v) +{ + if (v == NULL) { + PyErr_SetString(PyExc_AttributeError, + "can't delete connection attributes"); + return -1; + } + return PyObject_GenericSetAttr((PyObject *)self, name, v); } PyTypeObject _mysql_ConnectionObject_Type = { PyVarObject_HEAD_INIT(NULL, 0) - "_mysql.connection", /* (char *)tp_name For printing */ - sizeof(_mysql_ConnectionObject), - 0, - (destructor)_mysql_ConnectionObject_dealloc, /* tp_dealloc */ - 0, /*tp_print*/ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /*tp_compare*/ - (reprfunc)_mysql_ConnectionObject_repr, /* tp_repr */ - - /* Method suites for standard classes */ - - 0, /* (PyNumberMethods *) tp_as_number */ - 0, /* (PySequenceMethods *) tp_as_sequence */ - 0, /* (PyMappingMethods *) tp_as_mapping */ - - /* More standard operations (here for binary compatibility) */ - - 0, /* (hashfunc) tp_hash */ - 0, /* (ternaryfunc) tp_call */ - 0, /* (reprfunc) tp_str */ - (getattrofunc)_mysql_ConnectionObject_getattro, /* tp_getattro */ - (setattrofunc)_mysql_ConnectionObject_setattro, /* tp_setattro */ - - /* Functions to access object as input/output buffer */ - 0, /* (PyBufferProcs *) tp_as_buffer */ - - /* (tp_flags) Flags to define presence of optional/expanded features */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, - _mysql_connect__doc__, /* (char *) tp_doc Documentation string */ - - /* call function for all accessible objects */ - (traverseproc) _mysql_ConnectionObject_traverse, /* tp_traverse */ - - /* delete references to contained objects */ - (inquiry) _mysql_ConnectionObject_clear, /* tp_clear */ - - /* rich comparisons */ - 0, /* (richcmpfunc) tp_richcompare */ - - /* weak reference enabler */ - 0, /* (long) tp_weaklistoffset */ - - /* Iterators */ - 0, /* (getiterfunc) tp_iter */ - 0, /* (iternextfunc) tp_iternext */ - - /* Attribute descriptor and subclassing stuff */ - (struct PyMethodDef *)_mysql_ConnectionObject_methods, /* tp_methods */ - (struct PyMemberDef *)_mysql_ConnectionObject_memberlist, /* tp_members */ - 0, /* (struct getsetlist *) tp_getset; */ - 0, /* (struct _typeobject *) tp_base; */ - 0, /* (PyObject *) tp_dict */ - 0, /* (descrgetfunc) tp_descr_get */ - 0, /* (descrsetfunc) tp_descr_set */ - 0, /* (long) tp_dictoffset */ - (initproc)_mysql_ConnectionObject_Initialize, /* tp_init */ - NULL, /* tp_alloc */ - NULL, /* tp_new */ - NULL, /* tp_free Low-level free-memory routine */ - 0, /* (PyObject *) tp_bases */ - 0, /* (PyObject *) tp_mro method resolution order */ - 0, /* (PyObject *) tp_defined */ + "_mysql.connection", /* (char *)tp_name For printing */ + sizeof(_mysql_ConnectionObject), + 0, + (destructor)_mysql_ConnectionObject_dealloc, /* tp_dealloc */ + 0, /*tp_print*/ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /*tp_compare*/ + (reprfunc)_mysql_ConnectionObject_repr, /* tp_repr */ + + /* Method suites for standard classes */ + + 0, /* (PyNumberMethods *) tp_as_number */ + 0, /* (PySequenceMethods *) tp_as_sequence */ + 0, /* (PyMappingMethods *) tp_as_mapping */ + + /* More standard operations (here for binary compatibility) */ + + 0, /* (hashfunc) tp_hash */ + 0, /* (ternaryfunc) tp_call */ + 0, /* (reprfunc) tp_str */ + (getattrofunc)_mysql_ConnectionObject_getattro, /* tp_getattro */ + (setattrofunc)_mysql_ConnectionObject_setattro, /* tp_setattro */ + + /* Functions to access object as input/output buffer */ + 0, /* (PyBufferProcs *) tp_as_buffer */ + + /* (tp_flags) Flags to define presence of optional/expanded features */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, + _mysql_connect__doc__, /* (char *) tp_doc Documentation string */ + + /* call function for all accessible objects */ + (traverseproc) _mysql_ConnectionObject_traverse, /* tp_traverse */ + + /* delete references to contained objects */ + (inquiry) _mysql_ConnectionObject_clear, /* tp_clear */ + + /* rich comparisons */ + 0, /* (richcmpfunc) tp_richcompare */ + + /* weak reference enabler */ + 0, /* (long) tp_weaklistoffset */ + + /* Iterators */ + 0, /* (getiterfunc) tp_iter */ + 0, /* (iternextfunc) tp_iternext */ + + /* Attribute descriptor and subclassing stuff */ + (struct PyMethodDef *)_mysql_ConnectionObject_methods, /* tp_methods */ + (struct PyMemberDef *)_mysql_ConnectionObject_memberlist, /* tp_members */ + 0, /* (struct getsetlist *) tp_getset; */ + 0, /* (struct _typeobject *) tp_base; */ + 0, /* (PyObject *) tp_dict */ + 0, /* (descrgetfunc) tp_descr_get */ + 0, /* (descrsetfunc) tp_descr_set */ + 0, /* (long) tp_dictoffset */ + (initproc)_mysql_ConnectionObject_Initialize, /* tp_init */ + NULL, /* tp_alloc */ + NULL, /* tp_new */ + NULL, /* tp_free Low-level free-memory routine */ + 0, /* (PyObject *) tp_bases */ + 0, /* (PyObject *) tp_mro method resolution order */ + 0, /* (PyObject *) tp_defined */ } ; PyTypeObject _mysql_ResultObject_Type = { PyVarObject_HEAD_INIT(NULL, 0) - "_mysql.result", - sizeof(_mysql_ResultObject), - 0, - (destructor)_mysql_ResultObject_dealloc, /* tp_dealloc */ - 0, /*tp_print*/ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /*tp_compare*/ - (reprfunc)_mysql_ResultObject_repr, /* tp_repr */ - - /* Method suites for standard classes */ - - 0, /* (PyNumberMethods *) tp_as_number */ - 0, /* (PySequenceMethods *) tp_as_sequence */ - 0, /* (PyMappingMethods *) tp_as_mapping */ - - /* More standard operations (here for binary compatibility) */ - - 0, /* (hashfunc) tp_hash */ - 0, /* (ternaryfunc) tp_call */ - 0, /* (reprfunc) tp_str */ - (getattrofunc)PyObject_GenericGetAttr, /* tp_getattro */ - (setattrofunc)_mysql_ResultObject_setattro, /* tp_setattr */ - - /* Functions to access object as input/output buffer */ - 0, /* (PyBufferProcs *) tp_as_buffer */ - - /* Flags to define presence of optional/expanded features */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, - - _mysql_ResultObject__doc__, /* (char *) tp_doc Documentation string */ - - /* call function for all accessible objects */ - (traverseproc) _mysql_ResultObject_traverse, /* tp_traverse */ - - /* delete references to contained objects */ - (inquiry) _mysql_ResultObject_clear, /* tp_clear */ - - /* rich comparisons */ - 0, /* (richcmpfunc) tp_richcompare */ - - /* weak reference enabler */ - 0, /* (long) tp_weaklistoffset */ - - /* Iterators */ - 0, /* (getiterfunc) tp_iter */ - 0, /* (iternextfunc) tp_iternext */ - - /* Attribute descriptor and subclassing stuff */ - (struct PyMethodDef *) _mysql_ResultObject_methods, /* tp_methods */ - (struct PyMemberDef *) _mysql_ResultObject_memberlist, /*tp_members */ - 0, /* (struct getsetlist *) tp_getset; */ - 0, /* (struct _typeobject *) tp_base; */ - 0, /* (PyObject *) tp_dict */ - 0, /* (descrgetfunc) tp_descr_get */ - 0, /* (descrsetfunc) tp_descr_set */ - 0, /* (long) tp_dictoffset */ - (initproc)_mysql_ResultObject_Initialize, /* tp_init */ - NULL, /* tp_alloc */ - NULL, /* tp_new */ - NULL, /* tp_free Low-level free-memory routine */ - 0, /* (PyObject *) tp_bases */ - 0, /* (PyObject *) tp_mro method resolution order */ - 0, /* (PyObject *) tp_defined */ + "_mysql.result", + sizeof(_mysql_ResultObject), + 0, + (destructor)_mysql_ResultObject_dealloc, /* tp_dealloc */ + 0, /*tp_print*/ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /*tp_compare*/ + (reprfunc)_mysql_ResultObject_repr, /* tp_repr */ + + /* Method suites for standard classes */ + + 0, /* (PyNumberMethods *) tp_as_number */ + 0, /* (PySequenceMethods *) tp_as_sequence */ + 0, /* (PyMappingMethods *) tp_as_mapping */ + + /* More standard operations (here for binary compatibility) */ + + 0, /* (hashfunc) tp_hash */ + 0, /* (ternaryfunc) tp_call */ + 0, /* (reprfunc) tp_str */ + (getattrofunc)PyObject_GenericGetAttr, /* tp_getattro */ + (setattrofunc)_mysql_ResultObject_setattro, /* tp_setattr */ + + /* Functions to access object as input/output buffer */ + 0, /* (PyBufferProcs *) tp_as_buffer */ + + /* Flags to define presence of optional/expanded features */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, + + _mysql_ResultObject__doc__, /* (char *) tp_doc Documentation string */ + + /* call function for all accessible objects */ + (traverseproc) _mysql_ResultObject_traverse, /* tp_traverse */ + + /* delete references to contained objects */ + (inquiry) _mysql_ResultObject_clear, /* tp_clear */ + + /* rich comparisons */ + 0, /* (richcmpfunc) tp_richcompare */ + + /* weak reference enabler */ + 0, /* (long) tp_weaklistoffset */ + + /* Iterators */ + 0, /* (getiterfunc) tp_iter */ + 0, /* (iternextfunc) tp_iternext */ + + /* Attribute descriptor and subclassing stuff */ + (struct PyMethodDef *) _mysql_ResultObject_methods, /* tp_methods */ + (struct PyMemberDef *) _mysql_ResultObject_memberlist, /*tp_members */ + 0, /* (struct getsetlist *) tp_getset; */ + 0, /* (struct _typeobject *) tp_base; */ + 0, /* (PyObject *) tp_dict */ + 0, /* (descrgetfunc) tp_descr_get */ + 0, /* (descrsetfunc) tp_descr_set */ + 0, /* (long) tp_dictoffset */ + (initproc)_mysql_ResultObject_Initialize, /* tp_init */ + NULL, /* tp_alloc */ + NULL, /* tp_new */ + NULL, /* tp_free Low-level free-memory routine */ + 0, /* (PyObject *) tp_bases */ + 0, /* (PyObject *) tp_mro method resolution order */ + 0, /* (PyObject *) tp_defined */ }; static PyMethodDef _mysql_methods[] = { - { - "connect", - (PyCFunction)_mysql_connect, - METH_VARARGS | METH_KEYWORDS, - _mysql_connect__doc__ - }, - { - "debug", - (PyCFunction)_mysql_debug, - METH_VARARGS, - _mysql_debug__doc__ - }, - { - "escape", - (PyCFunction)_mysql_escape, - METH_VARARGS, - _mysql_escape__doc__ - }, - { - // deprecated. - "escape_sequence", - (PyCFunction)_mysql_escape_sequence, - METH_VARARGS, - _mysql_escape_sequence__doc__ - }, - { - // deprecated. - "escape_dict", - (PyCFunction)_mysql_escape_dict, - METH_VARARGS, - _mysql_escape_dict__doc__ - }, - { - "escape_string", - (PyCFunction)_mysql_escape_string, - METH_VARARGS, - _mysql_escape_string__doc__ - }, - { - "string_literal", - (PyCFunction)_mysql_string_literal, - METH_VARARGS, - _mysql_string_literal__doc__ - }, - { - "get_client_info", - (PyCFunction)_mysql_get_client_info, - METH_NOARGS, - _mysql_get_client_info__doc__ - }, - { - "thread_safe", - (PyCFunction)_mysql_thread_safe, - METH_NOARGS, - _mysql_thread_safe__doc__ - }, - {NULL, NULL} /* sentinel */ + { + "connect", + (PyCFunction)_mysql_connect, + METH_VARARGS | METH_KEYWORDS, + _mysql_connect__doc__ + }, + { + "debug", + (PyCFunction)_mysql_debug, + METH_VARARGS, + _mysql_debug__doc__ + }, + { + "escape", + (PyCFunction)_mysql_escape, + METH_VARARGS, + _mysql_escape__doc__ + }, + { + // deprecated. + "escape_sequence", + (PyCFunction)_mysql_escape_sequence, + METH_VARARGS, + _mysql_escape_sequence__doc__ + }, + { + // deprecated. + "escape_dict", + (PyCFunction)_mysql_escape_dict, + METH_VARARGS, + _mysql_escape_dict__doc__ + }, + { + "escape_string", + (PyCFunction)_mysql_escape_string, + METH_VARARGS, + _mysql_escape_string__doc__ + }, + { + "string_literal", + (PyCFunction)_mysql_string_literal, + METH_VARARGS, + _mysql_string_literal__doc__ + }, + { + "get_client_info", + (PyCFunction)_mysql_get_client_info, + METH_NOARGS, + _mysql_get_client_info__doc__ + }, + { + "thread_safe", + (PyCFunction)_mysql_thread_safe, + METH_NOARGS, + _mysql_thread_safe__doc__ + }, + {NULL, NULL} /* sentinel */ }; static PyObject * _mysql_NewException( - PyObject *dict, - PyObject *edict, - char *name) + PyObject *dict, + PyObject *edict, + char *name) { - PyObject *e; - if (!(e = PyDict_GetItemString(edict, name))) - return NULL; - if (PyDict_SetItemString(dict, name, e)) - return NULL; - Py_INCREF(e); - return e; + PyObject *e; + if (!(e = PyDict_GetItemString(edict, name))) + return NULL; + if (PyDict_SetItemString(dict, name, e)) + return NULL; + Py_INCREF(e); + return e; } #define QUOTE(X) _QUOTE(X) @@ -2702,100 +2702,100 @@ DL_EXPORT(void) init_mysql(void) #endif { - PyObject *dict, *module, *emod, *edict; + PyObject *dict, *module, *emod, *edict; #ifndef IS_PY3K - _mysql_ConnectionObject_Type.ob_type = &PyType_Type; - _mysql_ResultObject_Type.ob_type = &PyType_Type; + _mysql_ConnectionObject_Type.ob_type = &PyType_Type; + _mysql_ResultObject_Type.ob_type = &PyType_Type; #endif #if PY_VERSION_HEX >= 0x02020000 - _mysql_ConnectionObject_Type.tp_alloc = PyType_GenericAlloc; - _mysql_ConnectionObject_Type.tp_new = PyType_GenericNew; - _mysql_ResultObject_Type.tp_alloc = PyType_GenericAlloc; - _mysql_ResultObject_Type.tp_new = PyType_GenericNew; + _mysql_ConnectionObject_Type.tp_alloc = PyType_GenericAlloc; + _mysql_ConnectionObject_Type.tp_new = PyType_GenericNew; + _mysql_ResultObject_Type.tp_alloc = PyType_GenericAlloc; + _mysql_ResultObject_Type.tp_new = PyType_GenericNew; #endif #ifdef IS_PY3K - if (PyType_Ready(&_mysql_ConnectionObject_Type) < 0) - return NULL; - if (PyType_Ready(&_mysql_ResultObject_Type) < 0) - return NULL; + if (PyType_Ready(&_mysql_ConnectionObject_Type) < 0) + return NULL; + if (PyType_Ready(&_mysql_ResultObject_Type) < 0) + return NULL; - module = PyModule_Create(&_mysqlmodule); - if (!module) return module; /* this really should never happen */ + module = PyModule_Create(&_mysqlmodule); + if (!module) return module; /* this really should never happen */ #else - _mysql_ConnectionObject_Type.tp_free = _PyObject_GC_Del; - _mysql_ResultObject_Type.tp_free = _PyObject_GC_Del; - module = Py_InitModule4("_mysql", _mysql_methods, _mysql___doc__, - (PyObject *)NULL, PYTHON_API_VERSION); - if (!module) return; /* this really should never happen */ + _mysql_ConnectionObject_Type.tp_free = _PyObject_GC_Del; + _mysql_ResultObject_Type.tp_free = _PyObject_GC_Del; + module = Py_InitModule4("_mysql", _mysql_methods, _mysql___doc__, + (PyObject *)NULL, PYTHON_API_VERSION); + if (!module) return; /* this really should never happen */ #endif - if (!(dict = PyModule_GetDict(module))) goto error; - if (PyDict_SetItemString(dict, "version_info", - PyRun_String(QUOTE(version_info), Py_eval_input, - dict, dict))) - goto error; - if (PyDict_SetItemString(dict, "__version__", - PyString_FromString(QUOTE(__version__)))) - goto error; - if (PyDict_SetItemString(dict, "connection", - (PyObject *)&_mysql_ConnectionObject_Type)) - goto error; - Py_INCREF(&_mysql_ConnectionObject_Type); - if (PyDict_SetItemString(dict, "result", - (PyObject *)&_mysql_ResultObject_Type)) - goto error; - Py_INCREF(&_mysql_ResultObject_Type); - if (!(emod = PyImport_ImportModule("_mysql_exceptions"))) { - PyErr_Print(); - goto error; - } - if (!(edict = PyModule_GetDict(emod))) goto error; - if (!(_mysql_MySQLError = - _mysql_NewException(dict, edict, "MySQLError"))) - goto error; - if (!(_mysql_Warning = - _mysql_NewException(dict, edict, "Warning"))) - goto error; - if (!(_mysql_Error = - _mysql_NewException(dict, edict, "Error"))) - goto error; - if (!(_mysql_InterfaceError = - _mysql_NewException(dict, edict, "InterfaceError"))) - goto error; - if (!(_mysql_DatabaseError = - _mysql_NewException(dict, edict, "DatabaseError"))) - goto error; - if (!(_mysql_DataError = - _mysql_NewException(dict, edict, "DataError"))) - goto error; - if (!(_mysql_OperationalError = - _mysql_NewException(dict, edict, "OperationalError"))) - goto error; - if (!(_mysql_IntegrityError = - _mysql_NewException(dict, edict, "IntegrityError"))) - goto error; - if (!(_mysql_InternalError = - _mysql_NewException(dict, edict, "InternalError"))) - goto error; - if (!(_mysql_ProgrammingError = - _mysql_NewException(dict, edict, "ProgrammingError"))) - goto error; - if (!(_mysql_NotSupportedError = - _mysql_NewException(dict, edict, "NotSupportedError"))) - goto error; - Py_DECREF(emod); - if (!(_mysql_NULL = PyString_FromString("NULL"))) - goto error; - if (PyDict_SetItemString(dict, "NULL", _mysql_NULL)) goto error; + if (!(dict = PyModule_GetDict(module))) goto error; + if (PyDict_SetItemString(dict, "version_info", + PyRun_String(QUOTE(version_info), Py_eval_input, + dict, dict))) + goto error; + if (PyDict_SetItemString(dict, "__version__", + PyString_FromString(QUOTE(__version__)))) + goto error; + if (PyDict_SetItemString(dict, "connection", + (PyObject *)&_mysql_ConnectionObject_Type)) + goto error; + Py_INCREF(&_mysql_ConnectionObject_Type); + if (PyDict_SetItemString(dict, "result", + (PyObject *)&_mysql_ResultObject_Type)) + goto error; + Py_INCREF(&_mysql_ResultObject_Type); + if (!(emod = PyImport_ImportModule("_mysql_exceptions"))) { + PyErr_Print(); + goto error; + } + if (!(edict = PyModule_GetDict(emod))) goto error; + if (!(_mysql_MySQLError = + _mysql_NewException(dict, edict, "MySQLError"))) + goto error; + if (!(_mysql_Warning = + _mysql_NewException(dict, edict, "Warning"))) + goto error; + if (!(_mysql_Error = + _mysql_NewException(dict, edict, "Error"))) + goto error; + if (!(_mysql_InterfaceError = + _mysql_NewException(dict, edict, "InterfaceError"))) + goto error; + if (!(_mysql_DatabaseError = + _mysql_NewException(dict, edict, "DatabaseError"))) + goto error; + if (!(_mysql_DataError = + _mysql_NewException(dict, edict, "DataError"))) + goto error; + if (!(_mysql_OperationalError = + _mysql_NewException(dict, edict, "OperationalError"))) + goto error; + if (!(_mysql_IntegrityError = + _mysql_NewException(dict, edict, "IntegrityError"))) + goto error; + if (!(_mysql_InternalError = + _mysql_NewException(dict, edict, "InternalError"))) + goto error; + if (!(_mysql_ProgrammingError = + _mysql_NewException(dict, edict, "ProgrammingError"))) + goto error; + if (!(_mysql_NotSupportedError = + _mysql_NewException(dict, edict, "NotSupportedError"))) + goto error; + Py_DECREF(emod); + if (!(_mysql_NULL = PyString_FromString("NULL"))) + goto error; + if (PyDict_SetItemString(dict, "NULL", _mysql_NULL)) goto error; error: - if (PyErr_Occurred()) { - PyErr_SetString(PyExc_ImportError, - "_mysql: init failed"); - module = NULL; + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_ImportError, "_mysql: init failed"); + module = NULL; } #ifdef IS_PY3K return module; #endif } -/* vim: set ts=4 sts=4 sw=4 noexpandtab : */ + +/* vim: set ts=4 sts=4 sw=4 expandtab : */ From bd62c5d5d73369523cd8834763e1c6a11aa1587c Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 4 Dec 2018 19:36:33 +0900 Subject: [PATCH 072/177] cursor: Remove deprecated methods and classes --- MySQLdb/cursors.py | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 9181f237..98b3c738 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -534,38 +534,6 @@ class CursorDictRowsMixIn(object): _fetch_type = 1 - def fetchoneDict(self): - """Fetch a single row as a dictionary. Deprecated: - Use fetchone() instead. Will be removed in 1.3.""" - from warnings import warn - warn("fetchoneDict() is non-standard and will be removed in 1.3", - DeprecationWarning, 2) - return self.fetchone() - - def fetchmanyDict(self, size=None): - """Fetch several rows as a list of dictionaries. Deprecated: - Use fetchmany() instead. Will be removed in 1.3.""" - from warnings import warn - warn("fetchmanyDict() is non-standard and will be removed in 1.3", - DeprecationWarning, 2) - return self.fetchmany(size) - - def fetchallDict(self): - """Fetch all available rows as a list of dictionaries. Deprecated: - Use fetchall() instead. Will be removed in 1.3.""" - from warnings import warn - warn("fetchallDict() is non-standard and will be removed in 1.3", - DeprecationWarning, 2) - return self.fetchall() - - -class CursorOldDictRowsMixIn(CursorDictRowsMixIn): - """This is a MixIn class that returns rows as dictionaries with - the same key convention as the old Mysqldb (MySQLmodule). Don't - use this.""" - - _fetch_type = 2 - class Cursor(CursorStoreResultMixIn, CursorTupleRowsMixIn, BaseCursor): @@ -589,5 +557,3 @@ class SSDictCursor(CursorUseResultMixIn, CursorDictRowsMixIn, BaseCursor): """This is a Cursor class that returns rows as dictionaries and stores the result set in the server.""" - - From 833816ee87b4c88d77fe4c23e6368bc5a7f6e693 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 4 Dec 2018 20:19:04 +0900 Subject: [PATCH 073/177] Move _mysql and _mysql_exceptions into MySQLdb/ (#293) --- MySQLdb/__init__.py | 4 ++-- _mysql.c => MySQLdb/_mysql.c | 2 +- _mysql_exceptions.py => MySQLdb/_mysql_exceptions.py | 0 MySQLdb/connections.py | 5 ++--- MySQLdb/converters.py | 2 +- MySQLdb/cursors.py | 12 +++++++----- MySQLdb/times.py | 2 +- doc/user_guide.rst | 6 +++--- metadata.cfg | 2 +- setup.py | 4 +++- setup_posix.py | 3 +-- setup_windows.py | 3 +-- tests/test_MySQLdb_nonstandard.py | 2 +- tests/test__mysql.py | 2 +- 14 files changed, 25 insertions(+), 24 deletions(-) rename _mysql.c => MySQLdb/_mysql.c (99%) rename _mysql_exceptions.py => MySQLdb/_mysql_exceptions.py (100%) diff --git a/MySQLdb/__init__.py b/MySQLdb/__init__.py index a4a9da94..ad2057bc 100644 --- a/MySQLdb/__init__.py +++ b/MySQLdb/__init__.py @@ -15,7 +15,7 @@ from MySQLdb.release import __version__, version_info, __author__ -import _mysql +from . import _mysql if version_info != _mysql.version_info: raise ImportError("this is MySQLdb version %s, but _mysql is version %r" % @@ -25,7 +25,7 @@ apilevel = "2.0" paramstyle = "format" -from _mysql import * +from ._mysql import * from MySQLdb.compat import PY2 from MySQLdb.constants import FIELD_TYPE from MySQLdb.times import Date, Time, Timestamp, \ diff --git a/_mysql.c b/MySQLdb/_mysql.c similarity index 99% rename from _mysql.c rename to MySQLdb/_mysql.c index 537e61bc..04eadb7c 100644 --- a/_mysql.c +++ b/MySQLdb/_mysql.c @@ -2746,7 +2746,7 @@ init_mysql(void) (PyObject *)&_mysql_ResultObject_Type)) goto error; Py_INCREF(&_mysql_ResultObject_Type); - if (!(emod = PyImport_ImportModule("_mysql_exceptions"))) { + if (!(emod = PyImport_ImportModule("MySQLdb._mysql_exceptions"))) { PyErr_Print(); goto error; } diff --git a/_mysql_exceptions.py b/MySQLdb/_mysql_exceptions.py similarity index 100% rename from _mysql_exceptions.py rename to MySQLdb/_mysql_exceptions.py diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 10a0bf77..98c1ebff 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -7,14 +7,13 @@ import re import sys -from MySQLdb import cursors +from MySQLdb import cursors, _mysql from MySQLdb.compat import unicode, PY2 -from _mysql_exceptions import ( +from MySQLdb._mysql_exceptions import ( Warning, Error, InterfaceError, DataError, DatabaseError, OperationalError, IntegrityError, InternalError, NotSupportedError, ProgrammingError, ) -import _mysql if not PY2: diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index 5e28005d..c13e4265 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -31,7 +31,7 @@ MySQL.connect(). """ -from _mysql import string_literal, escape, NULL +from MySQLdb._mysql import string_literal, escape, NULL from MySQLdb.constants import FIELD_TYPE, FLAG from MySQLdb.times import * from MySQLdb.compat import PY2, long diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 98b3c738..60044830 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -8,8 +8,8 @@ import re import sys -from MySQLdb.compat import unicode -from _mysql_exceptions import ( +from .compat import unicode +from ._mysql_exceptions import ( Warning, Error, InterfaceError, DataError, DatabaseError, OperationalError, IntegrityError, InternalError, NotSupportedError, ProgrammingError) @@ -55,9 +55,11 @@ class BaseCursor(object): #: Default value of max_allowed_packet is 1048576. max_stmt_length = 64*1024 - from _mysql_exceptions import MySQLError, Warning, Error, InterfaceError, \ - DatabaseError, DataError, OperationalError, IntegrityError, \ - InternalError, ProgrammingError, NotSupportedError + from ._mysql_exceptions import ( + MySQLError, Warning, Error, InterfaceError, + DatabaseError, DataError, OperationalError, IntegrityError, + InternalError, ProgrammingError, NotSupportedError, + ) _defer_warnings = False connection = None diff --git a/MySQLdb/times.py b/MySQLdb/times.py index e26f8868..510d1c7c 100644 --- a/MySQLdb/times.py +++ b/MySQLdb/times.py @@ -6,7 +6,7 @@ """ from time import localtime from datetime import date, datetime, time, timedelta -from _mysql import string_literal +from MySQLdb._mysql import string_literal Date = date Time = time diff --git a/doc/user_guide.rst b/doc/user_guide.rst index d686b786..55c2c83f 100644 --- a/doc/user_guide.rst +++ b/doc/user_guide.rst @@ -17,11 +17,11 @@ Installation The ``README`` file has complete installation instructions. -_mysql ------- +MySQLdb._mysql +-------------- If you want to write applications which are portable across databases, -use MySQLdb_, and avoid using this module directly. ``_mysql`` +use MySQLdb_, and avoid using this module directly. ``MySQLdb._mysql`` provides an interface which mostly implements the MySQL C API. For more information, see the `MySQL documentation`_. The documentation for this module is intentionally weak because you probably should use diff --git a/metadata.cfg b/metadata.cfg index 6e05d35d..93b329b6 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -31,7 +31,7 @@ classifiers: Topic :: Database Topic :: Database :: Database Engines/Servers py_modules: - _mysql_exceptions + MySQLdb._mysql_exceptions MySQLdb.compat MySQLdb.connections MySQLdb.converters diff --git a/setup.py b/setup.py index ca4b362e..d1029962 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,9 @@ readme = f.read() metadata, options = get_config() -metadata['ext_modules'] = [setuptools.Extension(sources=['_mysql.c'], **options)] +metadata['ext_modules'] = [ + setuptools.Extension("MySQLdb._mysql", sources=['MySQLdb/_mysql.c'], **options) +] metadata['long_description'] = readme metadata['long_description_content_type'] = "text/markdown" setuptools.setup(**metadata) diff --git a/setup_posix.py b/setup_posix.py index 7b86ec73..9289bb50 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -94,7 +94,6 @@ def get_config(): create_release_file(metadata) del metadata['version_info'] ext_options = dict( - name = "_mysql", library_dirs = library_dirs, libraries = libraries, extra_compile_args = extra_compile_args, @@ -102,7 +101,7 @@ def get_config(): include_dirs = include_dirs, extra_objects = extra_objects, define_macros = define_macros, - ) + ) # newer versions of gcc require libstdc++ if doing a static build if static: diff --git a/setup_windows.py b/setup_windows.py index 74279077..0811e127 100644 --- a/setup_windows.py +++ b/setup_windows.py @@ -35,7 +35,6 @@ def get_config(): create_release_file(metadata) del metadata['version_info'] ext_options = dict( - name = "_mysql", library_dirs = library_dirs, libraries = libraries, extra_compile_args = extra_compile_args, @@ -43,7 +42,7 @@ def get_config(): include_dirs = include_dirs, extra_objects = extra_objects, define_macros = define_macros, - ) + ) return metadata, ext_options if __name__ == "__main__": diff --git a/tests/test_MySQLdb_nonstandard.py b/tests/test_MySQLdb_nonstandard.py index 52f6a4fb..a06d5edf 100644 --- a/tests/test_MySQLdb_nonstandard.py +++ b/tests/test_MySQLdb_nonstandard.py @@ -1,6 +1,6 @@ import unittest -import _mysql +from MySQLdb import _mysql import MySQLdb from MySQLdb.constants import FIELD_TYPE from configdb import connection_factory diff --git a/tests/test__mysql.py b/tests/test__mysql.py index e2cb6088..dd024b72 100644 --- a/tests/test__mysql.py +++ b/tests/test__mysql.py @@ -1,5 +1,5 @@ import pytest -import _mysql +from MySQLdb import _mysql def test_result_type(): From 4e7df80f7fed6047ac42a5e6840d9d894c6de036 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 4 Dec 2018 20:22:28 +0900 Subject: [PATCH 074/177] Add changelog --- HISTORY.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/HISTORY.rst b/HISTORY.rst index dd3bbaeb..0d326404 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,3 +1,17 @@ +====================== + What's new in 1.4.0 +====================== + +Release: TBD + +* Removed ``threadsafe`` and ``embedded`` build options. + +* Remove some deprecated cursor classes and methods. + +* ``_mysql`` and ``_mysql_exceptions`` modules are moved under + ``MySQLdb`` package. (#293) + + ====================== What's new in 1.3.14 ====================== From 0d7fcc269ce3cefc9614549e051092234a5ddb00 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 4 Dec 2018 20:36:04 +0900 Subject: [PATCH 075/177] Remove errorhandler (#294) --- HISTORY.rst | 1 + MySQLdb/connections.py | 27 --------------------------- MySQLdb/cursors.py | 18 +++++------------- 3 files changed, 6 insertions(+), 40 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 0d326404..9e8a19ed 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -11,6 +11,7 @@ Release: TBD * ``_mysql`` and ``_mysql_exceptions`` modules are moved under ``MySQLdb`` package. (#293) +* Remove ``errorhandler`` from Connection and Cursor classes. ====================== What's new in 1.3.14 diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 98c1ebff..1629073d 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -28,31 +28,6 @@ def _fast_surrogateescape(s): return s.decode('ascii', 'surrogateescape') -def defaulterrorhandler(connection, cursor, errorclass, errorvalue): - """ - If cursor is not None, (errorclass, errorvalue) is appended to - cursor.messages; otherwise it is appended to - connection.messages. Then errorclass is raised with errorvalue as - the value. - - You can override this with your own error handler by assigning it - to the instance. - """ - error = errorclass, errorvalue - if cursor: - cursor.messages.append(error) - else: - connection.messages.append(error) - del cursor - del connection - if isinstance(errorvalue, BaseException): - raise errorvalue - if errorclass is not None: - raise errorclass(errorvalue) - else: - raise Exception(errorvalue) - - re_numeric_part = re.compile(r"^(\d+)") def numeric_part(s): @@ -394,6 +369,4 @@ def show_warnings(self): ProgrammingError = ProgrammingError NotSupportedError = NotSupportedError - errorhandler = defaulterrorhandler - # vim: colorcolumn=100 diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 60044830..f503b71c 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -73,7 +73,6 @@ def __init__(self, connection): self._executed = None self.lastrowid = None self.messages = [] - self.errorhandler = connection.errorhandler self._result = None self._warnings = None self.rownumber = None @@ -88,7 +87,6 @@ def close(self): pass finally: self.connection = None - self.errorhandler = None self._result = None def __enter__(self): @@ -126,7 +124,7 @@ def _escape_args(self, args, conn): def _check_executed(self): if not self._executed: - self.errorhandler(self, ProgrammingError, "execute() first") + raise ProgrammingError("execute() first") def _warning_check(self): from warnings import warn @@ -244,17 +242,12 @@ def execute(self, query, args=None): try: query = query % args except TypeError as m: - self.errorhandler(self, ProgrammingError, str(m)) + raise ProgrammingError(str(m)) if isinstance(query, unicode): query = query.encode(db.encoding, 'surrogateescape') - res = None - try: - res = self._query(query) - except Exception: - exc, value = sys.exc_info()[:2] - self.errorhandler(self, exc, value) + res = self._query(query) if not self._defer_warnings: self._warning_check() return res @@ -458,10 +451,9 @@ def scroll(self, value, mode='relative'): elif mode == 'absolute': r = value else: - self.errorhandler(self, ProgrammingError, - "unknown scroll mode %s" % repr(mode)) + raise ProgrammingError("unknown scroll mode %s" % repr(mode)) if r < 0 or r >= len(self._rows): - self.errorhandler(self, IndexError, "out of range") + raise IndexError("out of range") self.rownumber = r def __iter__(self): From c64915b1e5c705f4fb10e86db5dcfed0b58552cc Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 4 Dec 2018 20:53:53 +0900 Subject: [PATCH 076/177] Remove context interface from Connector (#295) --- MySQLdb/connections.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 1629073d..574f72e2 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -253,20 +253,6 @@ def query(self, query): else: _mysql.connection.query(self, query) - def __enter__(self): - from warnings import warn - warn("context interface will be changed. Use explicit conn.commit() or conn.rollback().", - DeprecationWarning, 2) - if self.get_autocommit(): - self.query("BEGIN") - return self.cursor() - - def __exit__(self, exc, value, tb): - if exc: - self.rollback() - else: - self.commit() - def _bytes_literal(self, bs): assert isinstance(bs, (bytes, bytearray)) x = self.string_literal(bs) # x is escaped and quoted bytes From f04067faa9cbf8154afa2ae28646ba0fda6e9713 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 4 Dec 2018 20:55:19 +0900 Subject: [PATCH 077/177] Update history --- HISTORY.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/HISTORY.rst b/HISTORY.rst index 9e8a19ed..c6a2ca28 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -13,6 +13,10 @@ Release: TBD * Remove ``errorhandler`` from Connection and Cursor classes. +* Remove context manager API from Connection. It was for transaction. + New context manager API for closing connection will be added in future version. + + ====================== What's new in 1.3.14 ====================== From de44ff07e7c199f0c8762a8ee0bf7722100db158 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 4 Dec 2018 20:59:16 +0900 Subject: [PATCH 078/177] Remove waiter option --- HISTORY.rst | 1 + MySQLdb/connections.py | 13 +------------ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index c6a2ca28..a20ffe1c 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -16,6 +16,7 @@ Release: TBD * Remove context manager API from Connection. It was for transaction. New context manager API for closing connection will be added in future version. +* Remove ``waiter`` option from Connection. ====================== What's new in 1.3.14 diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 574f72e2..aa9c7529 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -50,7 +50,6 @@ class Connection(_mysql.connection): """MySQL Database Connection Object""" default_cursor = cursors.Cursor - waiter = None def __init__(self, *args, **kwargs): """ @@ -173,11 +172,6 @@ class object, used to create cursors (keyword only) # PEP-249 requires autocommit to be initially off autocommit = kwargs2.pop('autocommit', False) - self.waiter = kwargs2.pop('waiter', None) - if self.waiter: - from warnings import warn - warn("waiter is deprecated and will be removed in 1.4.", - DeprecationWarning, 2) super(Connection, self).__init__(*args, **kwargs2) self.cursorclass = cursorclass @@ -246,12 +240,7 @@ def query(self, query): # Since _mysql releases GIL while querying, we need immutable buffer. if isinstance(query, bytearray): query = bytes(query) - if self.waiter is not None: - self.send_query(query) - self.waiter(self.fileno()) - self.read_query_result() - else: - _mysql.connection.query(self, query) + _mysql.connection.query(self, query) def _bytes_literal(self, bs): assert isinstance(bs, (bytes, bytearray)) From ee91bc54a156b476e1c7842a418bf0c9e63420ee Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 4 Dec 2018 21:28:26 +0900 Subject: [PATCH 079/177] Remove Python 3.4 support --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a3b30d31..6a1df409 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,6 @@ python: - "3.7" - "3.6" - "3.5" - - "3.4" - "2.7" cache: pip From dff668c25b68e36bdd2261b69602019f6dae1ee8 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 4 Dec 2018 21:31:01 +0900 Subject: [PATCH 080/177] Remove Python 3.4 classifiers --- metadata.cfg | 1 - 1 file changed, 1 deletion(-) diff --git a/metadata.cfg b/metadata.cfg index 93b329b6..6901e7af 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -24,7 +24,6 @@ classifiers: Programming Language :: Python :: 2 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 - Programming Language :: Python :: 3.4 Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 From 16915a03530d73216d4342ef58d7f3ca90f19548 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 4 Dec 2018 21:50:30 +0900 Subject: [PATCH 081/177] Remove `fileno`, `escape_sequence`, and `escape_dict` from Connection (#297) --- HISTORY.rst | 3 ++ MySQLdb/__init__.py | 4 +- MySQLdb/_mysql.c | 96 --------------------------------------------- 3 files changed, 5 insertions(+), 98 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index a20ffe1c..0b5003db 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -18,6 +18,9 @@ Release: TBD * Remove ``waiter`` option from Connection. +* Remove ``fileno``, ``escape_sequence``, and ``escape_dict`` methods + from Connection class. + ====================== What's new in 1.3.14 ====================== diff --git a/MySQLdb/__init__.py b/MySQLdb/__init__.py index ad2057bc..fa52a83d 100644 --- a/MySQLdb/__init__.py +++ b/MySQLdb/__init__.py @@ -93,7 +93,7 @@ def Connect(*args, **kwargs): 'MySQLError', 'NULL', 'NUMBER', 'NotSupportedError', 'DBAPISet', 'OperationalError', 'ProgrammingError', 'ROWID', 'STRING', 'TIME', 'TIMESTAMP', 'Warning', 'apilevel', 'connect', 'connections', - 'constants', 'converters', 'cursors', 'debug', 'escape', 'escape_dict', - 'escape_sequence', 'escape_string', 'get_client_info', + 'constants', 'converters', 'cursors', 'debug', 'escape', + 'escape_string', 'get_client_info', 'paramstyle', 'string_literal', 'threadsafety', 'version_info'] diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 04eadb7c..0a527823 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -607,17 +607,6 @@ static int _mysql_ConnectionObject_clear( return 0; } -static char _mysql_ConnectionObject_fileno__doc__[] = -"Return underlaying fd for connection (deprecated)"; - -static PyObject * -_mysql_ConnectionObject_fileno( - _mysql_ConnectionObject *self) -{ - check_connection(self); - return PyInt_FromLong(self->connection.net.fd); -} - static char _mysql_ConnectionObject_close__doc__[] = "Close the connection. No further activity possible."; @@ -1036,71 +1025,6 @@ _mysql_escape( } } -static char _mysql_escape_sequence__doc__[] = -"escape_sequence(seq, dict) -- escape any special characters in sequence\n\ -seq using mapping dict to provide quoting functions for each type.\n\ -Returns a tuple of escaped items."; -static PyObject * -_mysql_escape_sequence( - PyObject *self, - PyObject *args) -{ - PyObject *o=NULL, *d=NULL, *r=NULL, *item, *quoted; - int i, n; - if (!PyArg_ParseTuple(args, "OO:escape_sequence", &o, &d)) - goto error; - if (!PyMapping_Check(d)) { - PyErr_SetString(PyExc_TypeError, - "argument 2 must be a mapping"); - return NULL; - } - if ((n = PyObject_Length(o)) == -1) goto error; - if (!(r = PyTuple_New(n))) goto error; - for (i=0; i Date: Tue, 4 Dec 2018 21:54:14 +0900 Subject: [PATCH 082/177] Remove automatic warning check (#296) --- MySQLdb/cursors.py | 46 ------------------------------ tests/test_MySQLdb_capabilities.py | 22 -------------- 2 files changed, 68 deletions(-) diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index f503b71c..97908249 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -61,7 +61,6 @@ class BaseCursor(object): InternalError, ProgrammingError, NotSupportedError, ) - _defer_warnings = False connection = None def __init__(self, connection): @@ -126,40 +125,6 @@ def _check_executed(self): if not self._executed: raise ProgrammingError("execute() first") - def _warning_check(self): - from warnings import warn - db = self._get_db() - - # None => warnings not interrogated for current query yet - # 0 => no warnings exists or have been handled already for this query - if self._warnings is None: - self._warnings = db.warning_count() - if self._warnings: - # Only propagate warnings for current query once - warning_count = self._warnings - self._warnings = 0 - # When there is next result, fetching warnings cause "command - # out of sync" error. - if self._result and self._result.has_next: - msg = "There are %d MySQL warnings." % (warning_count,) - self.messages.append(msg) - warn(self.Warning(0, msg), stacklevel=3) - return - - warnings = db.show_warnings() - if warnings: - # This is done in two loops in case - # Warnings are set to raise exceptions. - for w in warnings: - self.messages.append((self.Warning, w)) - for w in warnings: - warn(self.Warning(*w[1:3]), stacklevel=3) - else: - info = db.info() - if info: - self.messages.append((self.Warning, info)) - warn(self.Warning(0, info), stacklevel=3) - def nextset(self): """Advance to the next result set. @@ -175,7 +140,6 @@ def nextset(self): return None self._do_get_result(db) self._post_get_result() - self._warning_check() return 1 def _do_get_result(self, db): @@ -248,8 +212,6 @@ def execute(self, query, args=None): query = query.encode(db.encoding, 'surrogateescape') res = self._query(query) - if not self._defer_warnings: - self._warning_check() return res def executemany(self, query, args): @@ -363,8 +325,6 @@ def callproc(self, procname, args=()): if isinstance(q, unicode): q = q.encode(db.encoding, 'surrogateescape') self._query(q) - if not self._defer_warnings: - self._warning_check() return args def _query(self, q): @@ -470,8 +430,6 @@ class CursorUseResultMixIn(object): close() the cursor before additional queries can be performed on the connection.""" - _defer_warnings = True - def _get_result(self): return self._get_db().use_result() @@ -480,7 +438,6 @@ def fetchone(self): self._check_executed() r = self._fetch_row(1) if not r: - self._warning_check() return None self.rownumber = self.rownumber + 1 return r[0] @@ -491,8 +448,6 @@ def fetchmany(self, size=None): self._check_executed() r = self._fetch_row(size or self.arraysize) self.rownumber = self.rownumber + len(r) - if not r: - self._warning_check() return r def fetchall(self): @@ -500,7 +455,6 @@ def fetchall(self): self._check_executed() r = self._fetch_row(0) self.rownumber = self.rownumber + len(r) - self._warning_check() return r def __iter__(self): diff --git a/tests/test_MySQLdb_capabilities.py b/tests/test_MySQLdb_capabilities.py index 0427ed74..e9b0e2a9 100644 --- a/tests/test_MySQLdb_capabilities.py +++ b/tests/test_MySQLdb_capabilities.py @@ -160,28 +160,6 @@ def test_reraise_exception(self): return self.fail("Should raise ProgrammingError") - def test_warning_propagation(self): - with warnings.catch_warnings(): - # Ignore all warnings other than MySQLdb generated ones - warnings.simplefilter("ignore") - warnings.simplefilter("error", category=MySQLdb.Warning) - - # verify for both buffered and unbuffered cursor types - for cursor_class in (cursors.Cursor, cursors.SSCursor): - c = self.connection.cursor(cursor_class) - try: - c.execute("SELECT CAST('124b' AS SIGNED)") - c.fetchall() - except MySQLdb.Warning as e: - # Warnings should have errorcode and string message, just like exceptions - self.assertEqual(len(e.args), 2) - self.assertEqual(e.args[0], 1292) - self.assertTrue(isinstance(e.args[1], unicode)) - else: - self.fail("Should raise Warning") - finally: - c.close() - def test_binary_prefix(self): # verify prefix behaviour when enabled, disabled and for default (disabled) for binary_prefix in (True, False, None): From 2f8eaabf0d136d10709660ff122a53525bc180e5 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 4 Dec 2018 21:59:36 +0900 Subject: [PATCH 083/177] history --- HISTORY.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/HISTORY.rst b/HISTORY.rst index 0b5003db..2fd4500b 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -21,6 +21,9 @@ Release: TBD * Remove ``fileno``, ``escape_sequence``, and ``escape_dict`` methods from Connection class. +* Remove automatic MySQL warning checking. + + ====================== What's new in 1.3.14 ====================== From 20ee2e307b747883f1432d67b3b4c4617839769e Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 4 Dec 2018 22:06:08 +0900 Subject: [PATCH 084/177] Fix warnings --- MySQLdb/_mysql.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 0a527823..ce88d03c 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -367,7 +367,7 @@ _mysql_ConnectionObject_Initialize( MYSQL *conn = NULL; PyObject *conv = NULL; PyObject *ssl = NULL; - char *key = NULL, *cert = NULL, *ca = NULL, + const char *key = NULL, *cert = NULL, *ca = NULL, *capath = NULL, *cipher = NULL; PyObject *ssl_keepref[5] = {NULL}; int n_ssl_keepref = 0; @@ -2335,7 +2335,7 @@ _mysql_ConnectionObject_getattro( _mysql_ConnectionObject *self, PyObject *name) { - char *cname; + const char *cname; #ifdef IS_PY3K cname = PyUnicode_AsUTF8(name); #else From c2e5ff7bd3f705a19dd39cd71dfa2a2b83d6b003 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 4 Dec 2018 22:25:57 +0900 Subject: [PATCH 085/177] Support utf8mb3 --- MySQLdb/connections.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index aa9c7529..96b01528 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -300,7 +300,7 @@ def set_character_set(self, charset): set can only be changed in MySQL-4.1 and newer. If you try to change the character set from the current value in an older version, NotSupportedError will be raised.""" - if charset == "utf8mb4": + if charset in ("utf8mb4", "utf8mb3"): py_charset = "utf8" else: py_charset = charset From 41581254f8497a554f736bac805d02e5b06fda28 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Tue, 4 Dec 2018 21:31:33 -0600 Subject: [PATCH 086/177] Fix a reference leak in store_result and use_result if result object initialization fails. (#298) --- MySQLdb/_mysql.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index ce88d03c..66aba458 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -1865,8 +1865,10 @@ _mysql_ConnectionObject_store_result( if (!kwarglist) goto error; r = MyAlloc(_mysql_ResultObject, _mysql_ResultObject_Type); if (!r) goto error; - if (_mysql_ResultObject_Initialize(r, arglist, kwarglist)) + if (_mysql_ResultObject_Initialize(r, arglist, kwarglist)) { + Py_DECREF(r); goto error; + } result = (PyObject *) r; if (!(r->result)) { Py_DECREF(result); @@ -1924,9 +1926,11 @@ _mysql_ConnectionObject_use_result( if (!kwarglist) goto error; r = MyAlloc(_mysql_ResultObject, _mysql_ResultObject_Type); if (!r) goto error; - result = (PyObject *) r; - if (_mysql_ResultObject_Initialize(r, arglist, kwarglist)) + if (_mysql_ResultObject_Initialize(r, arglist, kwarglist)) { + Py_DECREF(r); goto error; + } + result = (PyObject *) r; if (!(r->result)) { Py_DECREF(result); Py_INCREF(Py_None); From e30deabbc965b9f538bc4eef74ecb1700533f5c0 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 5 Dec 2018 18:44:51 +0900 Subject: [PATCH 087/177] doc: expand TABs --- doc/_mysql.rst | 2 +- doc/user_guide.rst | 196 ++++++++++++++++++++++----------------------- 2 files changed, 99 insertions(+), 99 deletions(-) diff --git a/doc/_mysql.rst b/doc/_mysql.rst index 4a60591b..7ac4918d 100644 --- a/doc/_mysql.rst +++ b/doc/_mysql.rst @@ -4,4 +4,4 @@ _mysql Module .. automodule:: _mysql :members: :undoc-members: - :show-inheritance: \ No newline at end of file + :show-inheritance: diff --git a/doc/user_guide.rst b/doc/user_guide.rst index 55c2c83f..c83c3f3f 100644 --- a/doc/user_guide.rst +++ b/doc/user_guide.rst @@ -8,7 +8,7 @@ MySQLdb User's Guide Introduction ------------ -MySQLdb is a thread-compatible interface to the popular MySQL +MySQLdb is a interface to the popular MySQL database server that provides the Python database API. Installation @@ -28,7 +28,7 @@ for this module is intentionally weak because you probably should use the higher-level MySQLdb module. If you really need it, use the standard MySQL docs and transliterate as necessary. -.. _`MySQL documentation`: http://dev.mysql.com/doc/ +.. _`MySQL documentation`: https://dev.mysql.com/doc/ MySQL C API translation @@ -55,48 +55,48 @@ MySQL C API function mapping C API ``_mysql`` =================================== ================================== ``mysql_affected_rows()`` ``conn.affected_rows()`` - ``mysql_autocommit()`` ``conn.autocommit()`` - ``mysql_character_set_name()`` ``conn.character_set_name()`` - ``mysql_close()`` ``conn.close()`` - ``mysql_commit()`` ``conn.commit()`` - ``mysql_connect()`` ``_mysql.connect()`` - ``mysql_data_seek()`` ``result.data_seek()`` - ``mysql_debug()`` ``_mysql.debug()`` - ``mysql_dump_debug_info`` ``conn.dump_debug_info()`` - ``mysql_escape_string()`` ``_mysql.escape_string()`` - ``mysql_fetch_row()`` ``result.fetch_row()`` + ``mysql_autocommit()`` ``conn.autocommit()`` + ``mysql_character_set_name()`` ``conn.character_set_name()`` + ``mysql_close()`` ``conn.close()`` + ``mysql_commit()`` ``conn.commit()`` + ``mysql_connect()`` ``_mysql.connect()`` + ``mysql_data_seek()`` ``result.data_seek()`` + ``mysql_debug()`` ``_mysql.debug()`` + ``mysql_dump_debug_info`` ``conn.dump_debug_info()`` + ``mysql_escape_string()`` ``_mysql.escape_string()`` + ``mysql_fetch_row()`` ``result.fetch_row()`` ``mysql_get_character_set_info()`` ``conn.get_character_set_info()`` - ``mysql_get_client_info()`` ``_mysql.get_client_info()`` - ``mysql_get_host_info()`` ``conn.get_host_info()`` - ``mysql_get_proto_info()`` ``conn.get_proto_info()`` - ``mysql_get_server_info()`` ``conn.get_server_info()`` - ``mysql_info()`` ``conn.info()`` - ``mysql_insert_id()`` ``conn.insert_id()`` - ``mysql_num_fields()`` ``result.num_fields()`` - ``mysql_num_rows()`` ``result.num_rows()`` - ``mysql_options()`` various options to ``_mysql.connect()`` - ``mysql_ping()`` ``conn.ping()`` - ``mysql_query()`` ``conn.query()`` - ``mysql_real_connect()`` ``_mysql.connect()`` - ``mysql_real_query()`` ``conn.query()`` - ``mysql_real_escape_string()`` ``conn.escape_string()`` - ``mysql_rollback()`` ``conn.rollback()`` - ``mysql_row_seek()`` ``result.row_seek()`` - ``mysql_row_tell()`` ``result.row_tell()`` - ``mysql_select_db()`` ``conn.select_db()`` - ``mysql_set_character_set()`` ``conn.set_character_set()`` + ``mysql_get_client_info()`` ``_mysql.get_client_info()`` + ``mysql_get_host_info()`` ``conn.get_host_info()`` + ``mysql_get_proto_info()`` ``conn.get_proto_info()`` + ``mysql_get_server_info()`` ``conn.get_server_info()`` + ``mysql_info()`` ``conn.info()`` + ``mysql_insert_id()`` ``conn.insert_id()`` + ``mysql_num_fields()`` ``result.num_fields()`` + ``mysql_num_rows()`` ``result.num_rows()`` + ``mysql_options()`` various options to ``_mysql.connect()`` + ``mysql_ping()`` ``conn.ping()`` + ``mysql_query()`` ``conn.query()`` + ``mysql_real_connect()`` ``_mysql.connect()`` + ``mysql_real_query()`` ``conn.query()`` + ``mysql_real_escape_string()`` ``conn.escape_string()`` + ``mysql_rollback()`` ``conn.rollback()`` + ``mysql_row_seek()`` ``result.row_seek()`` + ``mysql_row_tell()`` ``result.row_tell()`` + ``mysql_select_db()`` ``conn.select_db()`` + ``mysql_set_character_set()`` ``conn.set_character_set()`` ``mysql_ssl_set()`` ``ssl`` option to ``_mysql.connect()`` - ``mysql_stat()`` ``conn.stat()`` - ``mysql_store_result()`` ``conn.store_result()`` - ``mysql_thread_id()`` ``conn.thread_id()`` - ``mysql_thread_safe_client()`` ``conn.thread_safe_client()`` - ``mysql_use_result()`` ``conn.use_result()`` - ``mysql_warning_count()`` ``conn.warning_count()`` - ``CLIENT_*`` ``MySQLdb.constants.CLIENT.*`` - ``CR_*`` ``MySQLdb.constants.CR.*`` - ``ER_*`` ``MySQLdb.constants.ER.*`` - ``FIELD_TYPE_*`` ``MySQLdb.constants.FIELD_TYPE.*`` - ``FLAG_*`` ``MySQLdb.constants.FLAG.*`` + ``mysql_stat()`` ``conn.stat()`` + ``mysql_store_result()`` ``conn.store_result()`` + ``mysql_thread_id()`` ``conn.thread_id()`` + ``mysql_thread_safe_client()`` ``conn.thread_safe_client()`` + ``mysql_use_result()`` ``conn.use_result()`` + ``mysql_warning_count()`` ``conn.warning_count()`` + ``CLIENT_*`` ``MySQLdb.constants.CLIENT.*`` + ``CR_*`` ``MySQLdb.constants.CR.*`` + ``ER_*`` ``MySQLdb.constants.ER.*`` + ``FIELD_TYPE_*`` ``MySQLdb.constants.FIELD_TYPE.*`` + ``FLAG_*`` ``MySQLdb.constants.FLAG.*`` =================================== ================================== @@ -126,7 +126,7 @@ We haven't even begun to touch upon all the parameters ``connect()`` can take. For this reason, I prefer to use keyword parameters:: db=_mysql.connect(host="localhost",user="joebob", - passwd="moonpie",db="thangs") + passwd="moonpie",db="thangs") This does exactly what the last example did, but is arguably easier to read. But since the default host is "localhost", and if your login @@ -260,19 +260,19 @@ Functions and attributes Only a few top-level functions and attributes are defined within MySQLdb. -connect(parameters...) - Constructor for creating a connection to the - database. Returns a Connection Object. Parameters are the - same as for the MySQL C API. In addition, there are a few - additional keywords that correspond to what you would pass - ``mysql_options()`` before connecting. Note that some - parameters must be specified as keyword arguments! The - default value for each parameter is NULL or zero, as - appropriate. Consult the MySQL documentation for more - details. The important parameters are: +connect(parameters...) + Constructor for creating a connection to the + database. Returns a Connection Object. Parameters are the + same as for the MySQL C API. In addition, there are a few + additional keywords that correspond to what you would pass + ``mysql_options()`` before connecting. Note that some + parameters must be specified as keyword arguments! The + default value for each parameter is NULL or zero, as + appropriate. Consult the MySQL documentation for more + details. The important parameters are: host - name of host to connect to. Default: use the local host + name of host to connect to. Default: use the local host via a UNIX socket (where applicable) user @@ -284,11 +284,11 @@ connect(parameters...) db database to use. Default: no default database. - port - TCP port of MySQL server. Default: standard port (3306). + port + TCP port of MySQL server. Default: standard port (3306). unix_socket - location of UNIX socket. Default: use default location or + location of UNIX socket. Default: use default location or TCP for remote hosts. conv @@ -331,45 +331,45 @@ connect(parameters...) connecting (MySQL-4.1 and later), you'll need to put the correct character set name in connection.charset. - If False, text-like columns are returned as normal strings, - but you can always write Unicode strings. + If False, text-like columns are returned as normal strings, + but you can always write Unicode strings. - *This must be a keyword parameter.* + *This must be a keyword parameter.* - charset - If present, the connection character set will be changed - to this character set, if they are not equal. Support for - changing the character set requires MySQL-4.1 and later - server; if the server is too old, UnsupportedError will be - raised. This option implies use_unicode=True, but you can - override this with use_unicode=False, though you probably - shouldn't. + charset + If present, the connection character set will be changed + to this character set, if they are not equal. Support for + changing the character set requires MySQL-4.1 and later + server; if the server is too old, UnsupportedError will be + raised. This option implies use_unicode=True, but you can + override this with use_unicode=False, though you probably + shouldn't. - If not present, the default character set is used. + If not present, the default character set is used. - *This must be a keyword parameter.* + *This must be a keyword parameter.* - sql_mode - If present, the session SQL mode will be set to the given - string. For more information on sql_mode, see the MySQL - documentation. Only available for 4.1 and newer servers. + sql_mode + If present, the session SQL mode will be set to the given + string. For more information on sql_mode, see the MySQL + documentation. Only available for 4.1 and newer servers. - If not present, the session SQL mode will be unchanged. + If not present, the session SQL mode will be unchanged. - *This must be a keyword parameter.* + *This must be a keyword parameter.* - ssl - This parameter takes a dictionary or mapping, where the - keys are parameter names used by the mysql_ssl_set_ MySQL - C API call. If this is set, it initiates an SSL connection - to the server; if there is no SSL support in the client, - an exception is raised. *This must be a keyword - parameter.* + ssl + This parameter takes a dictionary or mapping, where the + keys are parameter names used by the mysql_ssl_set_ MySQL + C API call. If this is set, it initiates an SSL connection + to the server; if there is no SSL support in the client, + an exception is raised. *This must be a keyword + parameter.* .. _mysql_ssl_set: http://dev.mysql.com/doc/refman/en/mysql-ssl-set.html -apilevel +apilevel String constant stating the supported DB API level. '2.0' threadsafety @@ -437,11 +437,11 @@ conv value), returning a Python value * a sequence of 2-tuples, where the first value is a combination - of flags from ``MySQLdb.constants.FLAG``, and the second value - is a function as above. The sequence is tested until the flags - on the field match those of the first value. If both values - are None, then the default conversion is done. Presently this - is only used to distinguish TEXT and BLOB columns. + of flags from ``MySQLdb.constants.FLAG``, and the second value + is a function as above. The sequence is tested until the flags + on the field match those of the first value. If both values + are None, then the default conversion is done. Presently this + is only used to distinguish TEXT and BLOB columns. If the key is a Python type or class, then the value is a callable Python object (usually a function) taking two arguments @@ -522,7 +522,7 @@ close() info() Returns some information about the last query. Normally - you don't need to check this. If there are any MySQL + you don't need to check this. If there are any MySQL warnings, it will cause a Warning to be issued through the Python warning module. By default, Warning causes a message to appear on the console. However, it is possible @@ -542,7 +542,7 @@ nextset() sets, it returns None; otherwise it returns a true value. Note that MySQL doesn't support multiple result sets until 4.1. - + Some examples ............. @@ -598,13 +598,13 @@ The only other method you are very likely to use is when you have to do a multi-row insert:: c.executemany( - """INSERT INTO breakfast (name, spam, eggs, sausage, price) - VALUES (%s, %s, %s, %s, %s)""", - [ - ("Spam and Sausage Lover's Plate", 5, 1, 8, 7.95 ), - ("Not So Much Spam Plate", 3, 2, 0, 3.95 ), - ("Don't Wany ANY SPAM! Plate", 0, 4, 3, 5.95 ) - ] ) + """INSERT INTO breakfast (name, spam, eggs, sausage, price) + VALUES (%s, %s, %s, %s, %s)""", + [ + ("Spam and Sausage Lover's Plate", 5, 1, 8, 7.95 ), + ("Not So Much Spam Plate", 3, 2, 0, 3.95 ), + ("Don't Wany ANY SPAM! Plate", 0, 4, 3, 5.95 ) + ] ) Here we are inserting three rows of five values. Notice that there is a mix of types (strings, ints, floats) though we still only use From 3bdcb2683288bebdfd6a9470ee5caf165c2e6113 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 5 Dec 2018 18:46:29 +0900 Subject: [PATCH 088/177] Expand TABs --- MySQLdb/constants/FIELD_TYPE.py | 3 +-- tests/dbapi20.py | 2 +- tests/test_MySQLdb_dbapi20.py | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/MySQLdb/constants/FIELD_TYPE.py b/MySQLdb/constants/FIELD_TYPE.py index 8a57b171..2a307644 100644 --- a/MySQLdb/constants/FIELD_TYPE.py +++ b/MySQLdb/constants/FIELD_TYPE.py @@ -2,7 +2,6 @@ These constants represent the various column (field) types that are supported by MySQL. - """ DECIMAL = 0 @@ -34,4 +33,4 @@ GEOMETRY = 255 CHAR = TINY -INTERVAL = ENUM +INTERVAL = ENUM diff --git a/tests/dbapi20.py b/tests/dbapi20.py index e28d5d1f..106ea405 100644 --- a/tests/dbapi20.py +++ b/tests/dbapi20.py @@ -704,7 +704,7 @@ def test_mixedfetch(self): def help_nextset_setUp(self,cur): ''' Should create a procedure called deleteme that returns two result sets, first the - number of rows in booze then "name from booze" + number of rows in booze then "name from booze" ''' raise NotImplementedError('Helper not implemented') #sql=""" diff --git a/tests/test_MySQLdb_dbapi20.py b/tests/test_MySQLdb_dbapi20.py index 88eaaef8..85fc5d51 100644 --- a/tests/test_MySQLdb_dbapi20.py +++ b/tests/test_MySQLdb_dbapi20.py @@ -149,7 +149,7 @@ def test_callproc(self): def help_nextset_setUp(self,cur): ''' Should create a procedure called deleteme that returns two result sets, first the - number of rows in booze then "name from booze" + number of rows in booze then "name from booze" ''' sql=""" create procedure deleteme() From 27e40186e3d1642cca0f1585bd8e7f5d1f27a82a Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Wed, 5 Dec 2018 04:08:35 -0600 Subject: [PATCH 089/177] Resurrect Connection.fileno() (#300) This permits low-level access to the underlying network connection. For example, this can be used to tweak the TCP timeout (TCP_USER_TIMEOUT). --- MySQLdb/_mysql.c | 20 ++++++++++++++++++++ tests/test_MySQLdb_nonstandard.py | 3 +++ 2 files changed, 23 insertions(+) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 66aba458..81dc924f 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -1639,6 +1639,20 @@ _mysql_ConnectionObject_field_count( return PyInt_FromLong((long)mysql_field_count(&(self->connection))); } +static char _mysql_ConnectionObject_fileno__doc__[] = +"Return file descriptor of the underlying libmysqlclient connection.\n\ +This provides raw access to the underlying network connection.\n\ +"; + +static PyObject * +_mysql_ConnectionObject_fileno( + _mysql_ConnectionObject *self, + PyObject *noargs) +{ + check_connection(self); + return PyInt_FromLong(self->connection.net.fd); +} + static char _mysql_ResultObject_num_fields__doc__[] = "Returns the number of fields (column) in the result." ; @@ -2133,6 +2147,12 @@ static PyMethodDef _mysql_ConnectionObject_methods[] = { METH_NOARGS, _mysql_ConnectionObject_field_count__doc__ }, + { + "fileno", + (PyCFunction)_mysql_ConnectionObject_fileno, + METH_NOARGS, + _mysql_ConnectionObject_fileno__doc__ + }, { "get_host_info", (PyCFunction)_mysql_ConnectionObject_get_host_info, diff --git a/tests/test_MySQLdb_nonstandard.py b/tests/test_MySQLdb_nonstandard.py index a06d5edf..e60a2a77 100644 --- a/tests/test_MySQLdb_nonstandard.py +++ b/tests/test_MySQLdb_nonstandard.py @@ -103,3 +103,6 @@ def test_client_flag(self): conn.client_flag = 0 conn.close() + + def test_fileno(self): + self.assertGreaterEqual(self.conn.fileno(), 0) From 2df4f2f8d821260dd6a7cd49d953e1a46fd50eb3 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 5 Dec 2018 19:08:58 +0900 Subject: [PATCH 090/177] Update HISTORY --- HISTORY.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 2fd4500b..b4fed226 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -18,8 +18,7 @@ Release: TBD * Remove ``waiter`` option from Connection. -* Remove ``fileno``, ``escape_sequence``, and ``escape_dict`` methods - from Connection class. +* Remove ``escape_sequence``, and ``escape_dict`` methods from Connection class. * Remove automatic MySQL warning checking. From c754b2508023e19d6aba17cd8b9eaf0370cdff92 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 6 Dec 2018 02:39:25 +0900 Subject: [PATCH 091/177] Support field name other than UTF-8 (#301) --- MySQLdb/_mysql.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 81dc924f..b45682df 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -1037,15 +1037,39 @@ _mysql_ResultObject_describe( { PyObject *d; MYSQL_FIELD *fields; + MY_CHARSET_INFO cs; + int isutf8 = 0; unsigned int i, n; + check_result_connection(self); + + mysql_get_character_set_info(&result_connection(self)->connection, &cs); + if (strncmp("utf8", cs.name, 4) == 0) { // utf8, utf8mb3, utf8mb4 + isutf8 = 1; + } + n = mysql_num_fields(self->result); fields = mysql_fetch_fields(self->result); if (!(d = PyTuple_New(n))) return NULL; for (i=0; i Date: Thu, 6 Dec 2018 19:33:31 +0900 Subject: [PATCH 092/177] Format --- MySQLdb/_mysql_exceptions.py | 13 - doc/FAQ.rst | 14 +- doc/MySQLdb.constants.rst | 118 +++--- doc/_mysql_exceptions.rst | 14 +- doc/modules.rst | 14 +- setup_windows.py | 1 - tests/capabilities.py | 596 ++++++++++++++--------------- tests/dbapi20.py | 32 +- tests/test_MySQLdb_capabilities.py | 372 +++++++++--------- tests/test_MySQLdb_dbapi20.py | 10 +- 10 files changed, 585 insertions(+), 599 deletions(-) diff --git a/MySQLdb/_mysql_exceptions.py b/MySQLdb/_mysql_exceptions.py index 74b765a7..99a79d77 100644 --- a/MySQLdb/_mysql_exceptions.py +++ b/MySQLdb/_mysql_exceptions.py @@ -13,42 +13,35 @@ class MySQLError(StandardError): - """Exception related to operation with MySQL.""" class Warning(Warning, MySQLError): - """Exception raised for important warnings like data truncations while inserting, etc.""" class Error(MySQLError): - """Exception that is the base class of all other error exceptions (not Warning).""" class InterfaceError(Error): - """Exception raised for errors that are related to the database interface rather than the database itself.""" class DatabaseError(Error): - """Exception raised for errors that are related to the database.""" class DataError(DatabaseError): - """Exception raised for errors that are due to problems with the processed data like division by zero, numeric value out of range, etc.""" class OperationalError(DatabaseError): - """Exception raised for errors that are related to the database's operation and not necessarily under the control of the programmer, e.g. an unexpected disconnect occurs, the data source name is not @@ -57,31 +50,25 @@ class OperationalError(DatabaseError): class IntegrityError(DatabaseError): - """Exception raised when the relational integrity of the database is affected, e.g. a foreign key check fails, duplicate key, etc.""" class InternalError(DatabaseError): - """Exception raised when the database encounters an internal error, e.g. the cursor is not valid anymore, the transaction is out of sync, etc.""" class ProgrammingError(DatabaseError): - """Exception raised for programming errors, e.g. table not found or already exists, syntax error in the SQL statement, wrong number of parameters specified, etc.""" class NotSupportedError(DatabaseError): - """Exception raised in case a method or database API was used which is not supported by the database, e.g. requesting a .rollback() on a connection that does not support transaction or has transactions turned off.""" - - diff --git a/doc/FAQ.rst b/doc/FAQ.rst index 21e00b9b..14c8f72c 100644 --- a/doc/FAQ.rst +++ b/doc/FAQ.rst @@ -26,7 +26,7 @@ probably the issue, but it shouldn't happen any more. ImportError ----------- - ImportError: No module named _mysql + ImportError: No module named _mysql If you see this, it's likely you did some wrong when installing MySQLdb; re-read (or read) README. _mysql is the low-level C module @@ -42,7 +42,7 @@ still have to edit a configuration file so that the setup knows where to find MySQL and what libraries to include. - ImportError: libmysqlclient_r.so.14: cannot open shared object file: No such file or directory + ImportError: libmysqlclient_r.so.14: cannot open shared object file: No such file or directory The number after .so may vary, but this means you have a version of MySQLdb compiled against one version of MySQL, and are now trying to @@ -67,7 +67,7 @@ Solutions: `_. - ImportError: ld.so.1: python: fatal: libmtmalloc.so.1: DF_1_NOOPEN tagged object may not be dlopen()'ed + ImportError: ld.so.1: python: fatal: libmtmalloc.so.1: DF_1_NOOPEN tagged object may not be dlopen()'ed This is a weird one from Solaris. What does it mean? I have no idea. However, things like this can happen if there is some sort of a compiler @@ -79,9 +79,9 @@ different vendors. Solution: Rebuild Python or MySQL (or maybe both) from source. - ImportError: dlopen(./_mysql.so, 2): Symbol not found: _sprintf$LDBLStub - Referenced from: ./_mysql.so - Expected in: dynamic lookup + ImportError: dlopen(./_mysql.so, 2): Symbol not found: _sprintf$LDBLStub + Referenced from: ./_mysql.so + Expected in: dynamic lookup This is one from Mac OS X. It seems to have been a compiler mismatch, but this time between two different versions of GCC. It seems nearly @@ -110,7 +110,7 @@ rolled back, and they cause pending transactions to commit. Other Errors ------------ - OperationalError: (1251, 'Client does not support authentication protocol requested by server; consider upgrading MySQL client') + OperationalError: (1251, 'Client does not support authentication protocol requested by server; consider upgrading MySQL client') This means your server and client libraries are not the same version. More specifically, it probably means you have a 4.1 or newer server diff --git a/doc/MySQLdb.constants.rst b/doc/MySQLdb.constants.rst index e28dee2b..9276f8fa 100644 --- a/doc/MySQLdb.constants.rst +++ b/doc/MySQLdb.constants.rst @@ -1,59 +1,59 @@ -constants Package -================= - -:mod:`constants` Package ------------------------- - -.. automodule:: MySQLdb.constants - :members: - :undoc-members: - :show-inheritance: - -:mod:`CLIENT` Module --------------------- - -.. automodule:: MySQLdb.constants.CLIENT - :members: - :undoc-members: - :show-inheritance: - -:mod:`CR` Module ----------------- - -.. automodule:: MySQLdb.constants.CR - :members: - :undoc-members: - :show-inheritance: - -:mod:`ER` Module ----------------- - -.. automodule:: MySQLdb.constants.ER - :members: - :undoc-members: - :show-inheritance: - -:mod:`FIELD_TYPE` Module ------------------------- - -.. automodule:: MySQLdb.constants.FIELD_TYPE - :members: - :undoc-members: - :show-inheritance: - -:mod:`FLAG` Module ------------------- - -.. automodule:: MySQLdb.constants.FLAG - :members: - :undoc-members: - :show-inheritance: - -:mod:`REFRESH` Module ---------------------- - -.. automodule:: MySQLdb.constants.REFRESH - :members: - :undoc-members: - :show-inheritance: - +constants Package +================= + +:mod:`constants` Package +------------------------ + +.. automodule:: MySQLdb.constants + :members: + :undoc-members: + :show-inheritance: + +:mod:`CLIENT` Module +-------------------- + +.. automodule:: MySQLdb.constants.CLIENT + :members: + :undoc-members: + :show-inheritance: + +:mod:`CR` Module +---------------- + +.. automodule:: MySQLdb.constants.CR + :members: + :undoc-members: + :show-inheritance: + +:mod:`ER` Module +---------------- + +.. automodule:: MySQLdb.constants.ER + :members: + :undoc-members: + :show-inheritance: + +:mod:`FIELD_TYPE` Module +------------------------ + +.. automodule:: MySQLdb.constants.FIELD_TYPE + :members: + :undoc-members: + :show-inheritance: + +:mod:`FLAG` Module +------------------ + +.. automodule:: MySQLdb.constants.FLAG + :members: + :undoc-members: + :show-inheritance: + +:mod:`REFRESH` Module +--------------------- + +.. automodule:: MySQLdb.constants.REFRESH + :members: + :undoc-members: + :show-inheritance: + diff --git a/doc/_mysql_exceptions.rst b/doc/_mysql_exceptions.rst index 9b65de35..2d43525c 100644 --- a/doc/_mysql_exceptions.rst +++ b/doc/_mysql_exceptions.rst @@ -1,7 +1,7 @@ -_mysql_exceptions Module -======================== - -.. automodule:: _mysql_exceptions - :members: - :undoc-members: - :show-inheritance: +_mysql_exceptions Module +======================== + +.. automodule:: _mysql_exceptions + :members: + :undoc-members: + :show-inheritance: diff --git a/doc/modules.rst b/doc/modules.rst index 7cf3faaa..998ac460 100644 --- a/doc/modules.rst +++ b/doc/modules.rst @@ -1,7 +1,7 @@ -MySQLdb -======= - -.. toctree:: - :maxdepth: 4 - - MySQLdb +MySQLdb +======= + +.. toctree:: + :maxdepth: 4 + + MySQLdb diff --git a/setup_windows.py b/setup_windows.py index 0811e127..5a4d236b 100644 --- a/setup_windows.py +++ b/setup_windows.py @@ -47,4 +47,3 @@ def get_config(): if __name__ == "__main__": sys.stderr.write("""You shouldn't be running this directly; it is used by setup.py.""") - diff --git a/tests/capabilities.py b/tests/capabilities.py index c341b3c9..5d913790 100644 --- a/tests/capabilities.py +++ b/tests/capabilities.py @@ -1,298 +1,298 @@ -#!/usr/bin/env python -O -""" Script to test database capabilities and the DB-API interface - for functionality and memory leaks. - - Adapted from a script by M-A Lemburg. - -""" -from time import time -import array -import unittest -from configdb import connection_factory - -from MySQLdb.compat import unichr - - -class DatabaseTest(unittest.TestCase): - - db_module = None - connect_args = () - connect_kwargs = dict() - create_table_extra = '' - rows = 10 - debug = False - - def setUp(self): - import gc - db = connection_factory(**self.connect_kwargs) - self.connection = db - self.cursor = db.cursor() - self.BLOBUText = u''.join([unichr(i) for i in range(16384)]) - self.BLOBBinary = self.db_module.Binary((u''.join([unichr(i) for i in range(256)] * 16)).encode('latin1')) - - leak_test = True - - def tearDown(self): - if self.leak_test: - import gc - del self.cursor - orphans = gc.collect() - self.failIf(orphans, "%d orphaned objects found after deleting cursor" % orphans) - - del self.connection - orphans = gc.collect() - self.failIf(orphans, "%d orphaned objects found after deleting connection" % orphans) - - def table_exists(self, name): - try: - self.cursor.execute('select * from %s where 1=0' % name) - except: - return False - else: - return True - - def quote_identifier(self, ident): - return '"%s"' % ident - - def new_table_name(self): - i = id(self.cursor) - while True: - name = self.quote_identifier('tb%08x' % i) - if not self.table_exists(name): - return name - i = i + 1 - - def create_table(self, columndefs): - - """ Create a table using a list of column definitions given in - columndefs. - - generator must be a function taking arguments (row_number, - col_number) returning a suitable data object for insertion - into the table. - - """ - self.table = self.new_table_name() - self.cursor.execute('CREATE TABLE %s (%s) %s' % - (self.table, - ',\n'.join(columndefs), - self.create_table_extra)) - - def check_data_integrity(self, columndefs, generator): - # insert - self.create_table(columndefs) - insert_statement = ('INSERT INTO %s VALUES (%s)' % - (self.table, - ','.join(['%s'] * len(columndefs)))) - data = [ [ generator(i,j) for j in range(len(columndefs)) ] - for i in range(self.rows) ] - self.cursor.executemany(insert_statement, data) - self.connection.commit() - # verify - self.cursor.execute('select * from %s' % self.table) - l = self.cursor.fetchall() - self.assertEqual(len(l), self.rows) - try: - for i in range(self.rows): - for j in range(len(columndefs)): - self.assertEqual(l[i][j], generator(i,j)) - finally: - if not self.debug: - self.cursor.execute('drop table %s' % (self.table)) - - def test_transactions(self): - columndefs = ( 'col1 INT', 'col2 VARCHAR(255)') - def generator(row, col): - if col == 0: return row - else: return ('%i' % (row%10))*255 - self.create_table(columndefs) - insert_statement = ('INSERT INTO %s VALUES (%s)' % - (self.table, - ','.join(['%s'] * len(columndefs)))) - data = [ [ generator(i,j) for j in range(len(columndefs)) ] - for i in range(self.rows) ] - self.cursor.executemany(insert_statement, data) - # verify - self.connection.commit() - self.cursor.execute('select * from %s' % self.table) - l = self.cursor.fetchall() - self.assertEqual(len(l), self.rows) - for i in range(self.rows): - for j in range(len(columndefs)): - self.assertEqual(l[i][j], generator(i,j)) - delete_statement = 'delete from %s where col1=%%s' % self.table - self.cursor.execute(delete_statement, (0,)) - self.cursor.execute('select col1 from %s where col1=%s' % \ - (self.table, 0)) - l = self.cursor.fetchall() - self.assertFalse(l, "DELETE didn't work") - self.connection.rollback() - self.cursor.execute('select col1 from %s where col1=%s' % \ - (self.table, 0)) - l = self.cursor.fetchall() - self.assertTrue(len(l) == 1, "ROLLBACK didn't work") - self.cursor.execute('drop table %s' % (self.table)) - - def test_truncation(self): - columndefs = ( 'col1 INT', 'col2 VARCHAR(255)') - def generator(row, col): - if col == 0: return row - else: return ('%i' % (row%10))*((255-self.rows//2)+row) - self.create_table(columndefs) - insert_statement = ('INSERT INTO %s VALUES (%s)' % - (self.table, - ','.join(['%s'] * len(columndefs)))) - - try: - self.cursor.execute(insert_statement, (0, '0'*256)) - except self.connection.DataError: - pass - else: - self.fail("Over-long column did not generate warnings/exception with single insert") - - self.connection.rollback() - - try: - for i in range(self.rows): - data = [] - for j in range(len(columndefs)): - data.append(generator(i,j)) - self.cursor.execute(insert_statement,tuple(data)) - except self.connection.DataError: - pass - else: - self.fail("Over-long columns did not generate warnings/exception with execute()") - - self.connection.rollback() - - try: - data = [ [ generator(i,j) for j in range(len(columndefs)) ] - for i in range(self.rows) ] - self.cursor.executemany(insert_statement, data) - except self.connection.DataError: - pass - else: - self.fail("Over-long columns did not generate warnings/exception with executemany()") - - self.connection.rollback() - self.cursor.execute('drop table %s' % (self.table)) - - def test_CHAR(self): - # Character data - def generator(row,col): - return ('%i' % ((row+col) % 10)) * 255 - self.check_data_integrity( - ('col1 char(255)','col2 char(255)'), - generator) - - def test_INT(self): - # Number data - def generator(row,col): - return row*row - self.check_data_integrity( - ('col1 INT',), - generator) - - def test_DECIMAL(self): - # DECIMAL - from decimal import Decimal - def generator(row,col): - return Decimal("%d.%02d" % (row, col)) - self.check_data_integrity( - ('col1 DECIMAL(5,2)',), - generator) - - val = Decimal('1.11111111111111119E-7') - self.cursor.execute('SELECT %s', (val,)) - result = self.cursor.fetchone()[0] - self.assertEqual(result, val) - self.assertIsInstance(result, Decimal) - - self.cursor.execute('SELECT %s + %s', (Decimal('0.1'), Decimal('0.2'))) - result = self.cursor.fetchone()[0] - self.assertEqual(result, Decimal('0.3')) - self.assertIsInstance(result, Decimal) - - def test_DATE(self): - ticks = time() - def generator(row,col): - return self.db_module.DateFromTicks(ticks+row*86400-col*1313) - self.check_data_integrity( - ('col1 DATE',), - generator) - - def test_TIME(self): - ticks = time() - def generator(row,col): - return self.db_module.TimeFromTicks(ticks+row*86400-col*1313) - self.check_data_integrity( - ('col1 TIME',), - generator) - - def test_DATETIME(self): - ticks = time() - def generator(row,col): - return self.db_module.TimestampFromTicks(ticks+row*86400-col*1313) - self.check_data_integrity( - ('col1 DATETIME',), - generator) - - def test_TIMESTAMP(self): - ticks = time() - def generator(row,col): - return self.db_module.TimestampFromTicks(ticks+row*86400-col*1313) - self.check_data_integrity( - ('col1 TIMESTAMP',), - generator) - - def test_fractional_TIMESTAMP(self): - ticks = time() - def generator(row,col): - return self.db_module.TimestampFromTicks(ticks+row*86400-col*1313+row*0.7*col/3.0) - self.check_data_integrity( - ('col1 TIMESTAMP',), - generator) - - def test_LONG(self): - def generator(row,col): - if col == 0: - return row - else: - return self.BLOBUText # 'BLOB Text ' * 1024 - self.check_data_integrity( - ('col1 INT','col2 LONG'), - generator) - - def test_TEXT(self): - def generator(row,col): - return self.BLOBUText # 'BLOB Text ' * 1024 - self.check_data_integrity( - ('col2 TEXT',), - generator) - - def test_LONG_BYTE(self): - def generator(row,col): - if col == 0: - return row - else: - return self.BLOBBinary # 'BLOB\000Binary ' * 1024 - self.check_data_integrity( - ('col1 INT','col2 LONG BYTE'), - generator) - - def test_BLOB(self): - def generator(row,col): - if col == 0: - return row - else: - return self.BLOBBinary # 'BLOB\000Binary ' * 1024 - self.check_data_integrity( - ('col1 INT','col2 BLOB'), - generator) - - def test_DOUBLE(self): - for val in (18014398509481982.0, 0.1): - self.cursor.execute('SELECT %s', (val,)); - result = self.cursor.fetchone()[0] - self.assertEqual(result, val) - self.assertIsInstance(result, float) +#!/usr/bin/env python -O +""" Script to test database capabilities and the DB-API interface + for functionality and memory leaks. + + Adapted from a script by M-A Lemburg. + +""" +from time import time +import array +import unittest +from configdb import connection_factory + +from MySQLdb.compat import unichr + + +class DatabaseTest(unittest.TestCase): + + db_module = None + connect_args = () + connect_kwargs = dict() + create_table_extra = '' + rows = 10 + debug = False + + def setUp(self): + import gc + db = connection_factory(**self.connect_kwargs) + self.connection = db + self.cursor = db.cursor() + self.BLOBUText = u''.join([unichr(i) for i in range(16384)]) + self.BLOBBinary = self.db_module.Binary((u''.join([unichr(i) for i in range(256)] * 16)).encode('latin1')) + + leak_test = True + + def tearDown(self): + if self.leak_test: + import gc + del self.cursor + orphans = gc.collect() + self.failIf(orphans, "%d orphaned objects found after deleting cursor" % orphans) + + del self.connection + orphans = gc.collect() + self.failIf(orphans, "%d orphaned objects found after deleting connection" % orphans) + + def table_exists(self, name): + try: + self.cursor.execute('select * from %s where 1=0' % name) + except: + return False + else: + return True + + def quote_identifier(self, ident): + return '"%s"' % ident + + def new_table_name(self): + i = id(self.cursor) + while True: + name = self.quote_identifier('tb%08x' % i) + if not self.table_exists(name): + return name + i = i + 1 + + def create_table(self, columndefs): + + """ Create a table using a list of column definitions given in + columndefs. + + generator must be a function taking arguments (row_number, + col_number) returning a suitable data object for insertion + into the table. + + """ + self.table = self.new_table_name() + self.cursor.execute('CREATE TABLE %s (%s) %s' % + (self.table, + ',\n'.join(columndefs), + self.create_table_extra)) + + def check_data_integrity(self, columndefs, generator): + # insert + self.create_table(columndefs) + insert_statement = ('INSERT INTO %s VALUES (%s)' % + (self.table, + ','.join(['%s'] * len(columndefs)))) + data = [ [ generator(i,j) for j in range(len(columndefs)) ] + for i in range(self.rows) ] + self.cursor.executemany(insert_statement, data) + self.connection.commit() + # verify + self.cursor.execute('select * from %s' % self.table) + l = self.cursor.fetchall() + self.assertEqual(len(l), self.rows) + try: + for i in range(self.rows): + for j in range(len(columndefs)): + self.assertEqual(l[i][j], generator(i,j)) + finally: + if not self.debug: + self.cursor.execute('drop table %s' % (self.table)) + + def test_transactions(self): + columndefs = ( 'col1 INT', 'col2 VARCHAR(255)') + def generator(row, col): + if col == 0: return row + else: return ('%i' % (row%10))*255 + self.create_table(columndefs) + insert_statement = ('INSERT INTO %s VALUES (%s)' % + (self.table, + ','.join(['%s'] * len(columndefs)))) + data = [ [ generator(i,j) for j in range(len(columndefs)) ] + for i in range(self.rows) ] + self.cursor.executemany(insert_statement, data) + # verify + self.connection.commit() + self.cursor.execute('select * from %s' % self.table) + l = self.cursor.fetchall() + self.assertEqual(len(l), self.rows) + for i in range(self.rows): + for j in range(len(columndefs)): + self.assertEqual(l[i][j], generator(i,j)) + delete_statement = 'delete from %s where col1=%%s' % self.table + self.cursor.execute(delete_statement, (0,)) + self.cursor.execute('select col1 from %s where col1=%s' % \ + (self.table, 0)) + l = self.cursor.fetchall() + self.assertFalse(l, "DELETE didn't work") + self.connection.rollback() + self.cursor.execute('select col1 from %s where col1=%s' % \ + (self.table, 0)) + l = self.cursor.fetchall() + self.assertTrue(len(l) == 1, "ROLLBACK didn't work") + self.cursor.execute('drop table %s' % (self.table)) + + def test_truncation(self): + columndefs = ( 'col1 INT', 'col2 VARCHAR(255)') + def generator(row, col): + if col == 0: return row + else: return ('%i' % (row%10))*((255-self.rows//2)+row) + self.create_table(columndefs) + insert_statement = ('INSERT INTO %s VALUES (%s)' % + (self.table, + ','.join(['%s'] * len(columndefs)))) + + try: + self.cursor.execute(insert_statement, (0, '0'*256)) + except self.connection.DataError: + pass + else: + self.fail("Over-long column did not generate warnings/exception with single insert") + + self.connection.rollback() + + try: + for i in range(self.rows): + data = [] + for j in range(len(columndefs)): + data.append(generator(i,j)) + self.cursor.execute(insert_statement,tuple(data)) + except self.connection.DataError: + pass + else: + self.fail("Over-long columns did not generate warnings/exception with execute()") + + self.connection.rollback() + + try: + data = [ [ generator(i,j) for j in range(len(columndefs)) ] + for i in range(self.rows) ] + self.cursor.executemany(insert_statement, data) + except self.connection.DataError: + pass + else: + self.fail("Over-long columns did not generate warnings/exception with executemany()") + + self.connection.rollback() + self.cursor.execute('drop table %s' % (self.table)) + + def test_CHAR(self): + # Character data + def generator(row,col): + return ('%i' % ((row+col) % 10)) * 255 + self.check_data_integrity( + ('col1 char(255)','col2 char(255)'), + generator) + + def test_INT(self): + # Number data + def generator(row,col): + return row*row + self.check_data_integrity( + ('col1 INT',), + generator) + + def test_DECIMAL(self): + # DECIMAL + from decimal import Decimal + def generator(row,col): + return Decimal("%d.%02d" % (row, col)) + self.check_data_integrity( + ('col1 DECIMAL(5,2)',), + generator) + + val = Decimal('1.11111111111111119E-7') + self.cursor.execute('SELECT %s', (val,)) + result = self.cursor.fetchone()[0] + self.assertEqual(result, val) + self.assertIsInstance(result, Decimal) + + self.cursor.execute('SELECT %s + %s', (Decimal('0.1'), Decimal('0.2'))) + result = self.cursor.fetchone()[0] + self.assertEqual(result, Decimal('0.3')) + self.assertIsInstance(result, Decimal) + + def test_DATE(self): + ticks = time() + def generator(row,col): + return self.db_module.DateFromTicks(ticks+row*86400-col*1313) + self.check_data_integrity( + ('col1 DATE',), + generator) + + def test_TIME(self): + ticks = time() + def generator(row,col): + return self.db_module.TimeFromTicks(ticks+row*86400-col*1313) + self.check_data_integrity( + ('col1 TIME',), + generator) + + def test_DATETIME(self): + ticks = time() + def generator(row,col): + return self.db_module.TimestampFromTicks(ticks+row*86400-col*1313) + self.check_data_integrity( + ('col1 DATETIME',), + generator) + + def test_TIMESTAMP(self): + ticks = time() + def generator(row,col): + return self.db_module.TimestampFromTicks(ticks+row*86400-col*1313) + self.check_data_integrity( + ('col1 TIMESTAMP',), + generator) + + def test_fractional_TIMESTAMP(self): + ticks = time() + def generator(row,col): + return self.db_module.TimestampFromTicks(ticks+row*86400-col*1313+row*0.7*col/3.0) + self.check_data_integrity( + ('col1 TIMESTAMP',), + generator) + + def test_LONG(self): + def generator(row,col): + if col == 0: + return row + else: + return self.BLOBUText # 'BLOB Text ' * 1024 + self.check_data_integrity( + ('col1 INT','col2 LONG'), + generator) + + def test_TEXT(self): + def generator(row,col): + return self.BLOBUText # 'BLOB Text ' * 1024 + self.check_data_integrity( + ('col2 TEXT',), + generator) + + def test_LONG_BYTE(self): + def generator(row,col): + if col == 0: + return row + else: + return self.BLOBBinary # 'BLOB\000Binary ' * 1024 + self.check_data_integrity( + ('col1 INT','col2 LONG BYTE'), + generator) + + def test_BLOB(self): + def generator(row,col): + if col == 0: + return row + else: + return self.BLOBBinary # 'BLOB\000Binary ' * 1024 + self.check_data_integrity( + ('col1 INT','col2 BLOB'), + generator) + + def test_DOUBLE(self): + for val in (18014398509481982.0, 0.1): + self.cursor.execute('SELECT %s', (val,)); + result = self.cursor.fetchone()[0] + self.assertEqual(result, val) + self.assertIsInstance(result, float) diff --git a/tests/dbapi20.py b/tests/dbapi20.py index 106ea405..79c188a5 100644 --- a/tests/dbapi20.py +++ b/tests/dbapi20.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -''' Python DB API 2.0 driver compliance unit test suite. - +''' Python DB API 2.0 driver compliance unit test suite. + This software is Public Domain and may be used without restrictions. "Now we have booze and barflies entering the discussion, plus rumours of @@ -67,8 +67,8 @@ class DatabaseAPI20Test(unittest.TestCase): ''' Test a database self.driver for DB API 2.0 compatibility. This implementation tests Gadfly, but the TestCase - is structured so that other self.drivers can subclass this - test case to ensure compiliance with the DB-API. It is + is structured so that other self.drivers can subclass this + test case to ensure compiliance with the DB-API. It is expected that this TestCase may be expanded in the future if ambiguities or edge conditions are discovered. @@ -78,9 +78,9 @@ class DatabaseAPI20Test(unittest.TestCase): self.driver, connect_args and connect_kw_args. Class specification should be as follows: - import dbapi20 + import dbapi20 class mytest(dbapi20.DatabaseAPI20Test): - [...] + [...] Don't 'import DatabaseAPI20Test from dbapi20', or you will confuse the unit tester - just 'import dbapi20'. @@ -99,7 +99,7 @@ class mytest(dbapi20.DatabaseAPI20Test): xddl2 = 'drop table %sbarflys' % table_prefix lowerfunc = 'lower' # Name of stored procedure to convert string->lowercase - + # Some drivers may need to override these helpers, for example adding # a 'commit' after the execute. def executeDDL1(self,cursor): @@ -123,10 +123,10 @@ def tearDown(self): try: cur = con.cursor() for ddl in (self.xddl1,self.xddl2): - try: + try: cur.execute(ddl) con.commit() - except self.driver.Error: + except self.driver.Error: # Assume table didn't exist. Other tests will check if # execute is busted. pass @@ -238,7 +238,7 @@ def test_rollback(self): con.rollback() except self.driver.NotSupportedError: pass - + def test_cursor(self): con = self._connect() try: @@ -392,7 +392,7 @@ def _paraminsert(self,cur): ) elif self.driver.paramstyle == 'named': cur.execute( - 'insert into %sbooze values (:beer)' % self.table_prefix, + 'insert into %sbooze values (:beer)' % self.table_prefix, {'beer':"Cooper's"} ) elif self.driver.paramstyle == 'format': @@ -532,7 +532,7 @@ def _populate(self): tests. ''' populate = [ - "insert into %sbooze values ('%s')" % (self.table_prefix,s) + "insert into %sbooze values ('%s')" % (self.table_prefix,s) for s in self.samples ] return populate @@ -593,7 +593,7 @@ def test_fetchmany(self): self.assertEqual(len(rows),6) rows = [r[0] for r in rows] rows.sort() - + # Make sure we get the right data back out for i in range(0,6): self.assertEqual(rows[i],self.samples[i], @@ -664,10 +664,10 @@ def test_fetchall(self): 'cursor.fetchall should return an empty list if ' 'a select query returns no rows' ) - + finally: con.close() - + def test_mixedfetch(self): con = self._connect() try: @@ -703,7 +703,7 @@ def test_mixedfetch(self): def help_nextset_setUp(self,cur): ''' Should create a procedure called deleteme - that returns two result sets, first the + that returns two result sets, first the number of rows in booze then "name from booze" ''' raise NotImplementedError('Helper not implemented') diff --git a/tests/test_MySQLdb_capabilities.py b/tests/test_MySQLdb_capabilities.py index e9b0e2a9..6e39d146 100644 --- a/tests/test_MySQLdb_capabilities.py +++ b/tests/test_MySQLdb_capabilities.py @@ -1,186 +1,186 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -import capabilities -from datetime import timedelta -from contextlib import closing -import unittest -import MySQLdb -from MySQLdb.compat import unicode -from MySQLdb import cursors -from configdb import connection_factory -import warnings - - -warnings.filterwarnings('ignore') - - -class test_MySQLdb(capabilities.DatabaseTest): - - db_module = MySQLdb - connect_args = () - connect_kwargs = dict(use_unicode=True, sql_mode="ANSI,STRICT_TRANS_TABLES,TRADITIONAL") - create_table_extra = "ENGINE=INNODB CHARACTER SET UTF8" - leak_test = False - - def quote_identifier(self, ident): - return "`%s`" % ident - - def test_TIME(self): - def generator(row,col): - return timedelta(0, row*8000) - self.check_data_integrity( - ('col1 TIME',), - generator) - - def test_TINYINT(self): - # Number data - def generator(row, col): - v = (row*row) % 256 - if v > 127: - v = v-256 - return v - self.check_data_integrity( - ('col1 TINYINT',), - generator) - - def test_stored_procedures(self): - db = self.connection - c = self.cursor - self.create_table(('pos INT', 'tree CHAR(20)')) - c.executemany("INSERT INTO %s (pos,tree) VALUES (%%s,%%s)" % self.table, - list(enumerate('ash birch cedar Lärche pine'.split()))) - db.commit() - - c.execute(""" - CREATE PROCEDURE test_sp(IN t VARCHAR(255)) - BEGIN - SELECT pos FROM %s WHERE tree = t; - END - """ % self.table) - db.commit() - - c.callproc('test_sp', ('Lärche',)) - rows = c.fetchall() - self.assertEqual(len(rows), 1) - self.assertEqual(rows[0][0], 3) - c.nextset() - - c.execute("DROP PROCEDURE test_sp") - c.execute('drop table %s' % (self.table)) - - def test_small_CHAR(self): - # Character data - def generator(row,col): - i = (row*col+62)%256 - if i == 62: return '' - if i == 63: return None - return chr(i) - self.check_data_integrity( - ('col1 char(1)','col2 char(1)'), - generator) - - def test_BIT(self): - c = self.cursor - try: - c.execute("""create table test_BIT ( - b3 BIT(3), - b7 BIT(10), - b64 BIT(64))""") - - one64 = '1'*64 - c.execute( - "insert into test_BIT (b3, b7, b64)" - " VALUES (b'011', b'1111111111', b'%s')" - % one64) - - c.execute("SELECT b3, b7, b64 FROM test_BIT") - row = c.fetchone() - self.assertEqual(row[0], b'\x03') - self.assertEqual(row[1], b'\x03\xff') - self.assertEqual(row[2], b'\xff'*8) - finally: - c.execute("drop table if exists test_BIT") - - def test_MULTIPOLYGON(self): - c = self.cursor - try: - c.execute("""create table test_MULTIPOLYGON ( - id INTEGER PRIMARY KEY, - border MULTIPOLYGON)""") - - c.execute( - "insert into test_MULTIPOLYGON (id, border)" - " VALUES (1, GeomFromText('MULTIPOLYGON(((1 1, 1 -1, -1 -1, -1 1, 1 1)),((1 1, 3 1, 3 3, 1 3, 1 1)))'))" - ) - - c.execute("SELECT id, AsText(border) FROM test_MULTIPOLYGON") - row = c.fetchone() - self.assertEqual(row[0], 1) - self.assertEqual(row[1], 'MULTIPOLYGON(((1 1,1 -1,-1 -1,-1 1,1 1)),((1 1,3 1,3 3,1 3,1 1)))') - - c.execute("SELECT id, AsWKB(border) FROM test_MULTIPOLYGON") - row = c.fetchone() - self.assertEqual(row[0], 1) - self.assertNotEqual(len(row[1]), 0) - - c.execute("SELECT id, border FROM test_MULTIPOLYGON") - row = c.fetchone() - self.assertEqual(row[0], 1) - self.assertNotEqual(len(row[1]), 0) - finally: - c.execute("drop table if exists test_MULTIPOLYGON") - - def test_bug_2671682(self): - from MySQLdb.constants import ER - try: - self.cursor.execute("describe some_non_existent_table"); - except self.connection.ProgrammingError as msg: - self.assertTrue(str(ER.NO_SUCH_TABLE) in str(msg)) - - def test_bug_3514287(self): - c = self.cursor - try: - c.execute("""create table bug_3541287 ( - c1 CHAR(10), - t1 TIMESTAMP)""") - c.execute("insert into bug_3541287 (c1,t1) values (%s, NOW())", - ("blah",)) - finally: - c.execute("drop table if exists bug_3541287") - - def test_ping(self): - self.connection.ping() - - def test_reraise_exception(self): - c = self.cursor - try: - c.execute("SELECT x FROM not_existing_table") - except MySQLdb.ProgrammingError as e: - self.assertEqual(e.args[0], 1146) - return - self.fail("Should raise ProgrammingError") - - def test_binary_prefix(self): - # verify prefix behaviour when enabled, disabled and for default (disabled) - for binary_prefix in (True, False, None): - kwargs = self.connect_kwargs.copy() - # needs to be set to can guarantee CHARSET response for normal strings - kwargs['charset'] = 'utf8' - if binary_prefix != None: - kwargs['binary_prefix'] = binary_prefix - - with closing(connection_factory(**kwargs)) as conn: - with closing(conn.cursor()) as c: - c.execute('SELECT CHARSET(%s)', (MySQLdb.Binary(b'raw bytes'),)) - self.assertEqual(c.fetchall()[0][0], 'binary' if binary_prefix else 'utf8') - # normal strings should not get prefix - c.execute('SELECT CHARSET(%s)', ('str',)) - self.assertEqual(c.fetchall()[0][0], 'utf8') - - -if __name__ == '__main__': - if test_MySQLdb.leak_test: - import gc - gc.enable() - gc.set_debug(gc.DEBUG_LEAK) - unittest.main() +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import capabilities +from datetime import timedelta +from contextlib import closing +import unittest +import MySQLdb +from MySQLdb.compat import unicode +from MySQLdb import cursors +from configdb import connection_factory +import warnings + + +warnings.filterwarnings('ignore') + + +class test_MySQLdb(capabilities.DatabaseTest): + + db_module = MySQLdb + connect_args = () + connect_kwargs = dict(use_unicode=True, sql_mode="ANSI,STRICT_TRANS_TABLES,TRADITIONAL") + create_table_extra = "ENGINE=INNODB CHARACTER SET UTF8" + leak_test = False + + def quote_identifier(self, ident): + return "`%s`" % ident + + def test_TIME(self): + def generator(row,col): + return timedelta(0, row*8000) + self.check_data_integrity( + ('col1 TIME',), + generator) + + def test_TINYINT(self): + # Number data + def generator(row, col): + v = (row*row) % 256 + if v > 127: + v = v-256 + return v + self.check_data_integrity( + ('col1 TINYINT',), + generator) + + def test_stored_procedures(self): + db = self.connection + c = self.cursor + self.create_table(('pos INT', 'tree CHAR(20)')) + c.executemany("INSERT INTO %s (pos,tree) VALUES (%%s,%%s)" % self.table, + list(enumerate('ash birch cedar Lärche pine'.split()))) + db.commit() + + c.execute(""" + CREATE PROCEDURE test_sp(IN t VARCHAR(255)) + BEGIN + SELECT pos FROM %s WHERE tree = t; + END + """ % self.table) + db.commit() + + c.callproc('test_sp', ('Lärche',)) + rows = c.fetchall() + self.assertEqual(len(rows), 1) + self.assertEqual(rows[0][0], 3) + c.nextset() + + c.execute("DROP PROCEDURE test_sp") + c.execute('drop table %s' % (self.table)) + + def test_small_CHAR(self): + # Character data + def generator(row,col): + i = (row*col+62)%256 + if i == 62: return '' + if i == 63: return None + return chr(i) + self.check_data_integrity( + ('col1 char(1)','col2 char(1)'), + generator) + + def test_BIT(self): + c = self.cursor + try: + c.execute("""create table test_BIT ( + b3 BIT(3), + b7 BIT(10), + b64 BIT(64))""") + + one64 = '1'*64 + c.execute( + "insert into test_BIT (b3, b7, b64)" + " VALUES (b'011', b'1111111111', b'%s')" + % one64) + + c.execute("SELECT b3, b7, b64 FROM test_BIT") + row = c.fetchone() + self.assertEqual(row[0], b'\x03') + self.assertEqual(row[1], b'\x03\xff') + self.assertEqual(row[2], b'\xff'*8) + finally: + c.execute("drop table if exists test_BIT") + + def test_MULTIPOLYGON(self): + c = self.cursor + try: + c.execute("""create table test_MULTIPOLYGON ( + id INTEGER PRIMARY KEY, + border MULTIPOLYGON)""") + + c.execute( + "insert into test_MULTIPOLYGON (id, border)" + " VALUES (1, GeomFromText('MULTIPOLYGON(((1 1, 1 -1, -1 -1, -1 1, 1 1)),((1 1, 3 1, 3 3, 1 3, 1 1)))'))" + ) + + c.execute("SELECT id, AsText(border) FROM test_MULTIPOLYGON") + row = c.fetchone() + self.assertEqual(row[0], 1) + self.assertEqual(row[1], 'MULTIPOLYGON(((1 1,1 -1,-1 -1,-1 1,1 1)),((1 1,3 1,3 3,1 3,1 1)))') + + c.execute("SELECT id, AsWKB(border) FROM test_MULTIPOLYGON") + row = c.fetchone() + self.assertEqual(row[0], 1) + self.assertNotEqual(len(row[1]), 0) + + c.execute("SELECT id, border FROM test_MULTIPOLYGON") + row = c.fetchone() + self.assertEqual(row[0], 1) + self.assertNotEqual(len(row[1]), 0) + finally: + c.execute("drop table if exists test_MULTIPOLYGON") + + def test_bug_2671682(self): + from MySQLdb.constants import ER + try: + self.cursor.execute("describe some_non_existent_table"); + except self.connection.ProgrammingError as msg: + self.assertTrue(str(ER.NO_SUCH_TABLE) in str(msg)) + + def test_bug_3514287(self): + c = self.cursor + try: + c.execute("""create table bug_3541287 ( + c1 CHAR(10), + t1 TIMESTAMP)""") + c.execute("insert into bug_3541287 (c1,t1) values (%s, NOW())", + ("blah",)) + finally: + c.execute("drop table if exists bug_3541287") + + def test_ping(self): + self.connection.ping() + + def test_reraise_exception(self): + c = self.cursor + try: + c.execute("SELECT x FROM not_existing_table") + except MySQLdb.ProgrammingError as e: + self.assertEqual(e.args[0], 1146) + return + self.fail("Should raise ProgrammingError") + + def test_binary_prefix(self): + # verify prefix behaviour when enabled, disabled and for default (disabled) + for binary_prefix in (True, False, None): + kwargs = self.connect_kwargs.copy() + # needs to be set to can guarantee CHARSET response for normal strings + kwargs['charset'] = 'utf8' + if binary_prefix != None: + kwargs['binary_prefix'] = binary_prefix + + with closing(connection_factory(**kwargs)) as conn: + with closing(conn.cursor()) as c: + c.execute('SELECT CHARSET(%s)', (MySQLdb.Binary(b'raw bytes'),)) + self.assertEqual(c.fetchall()[0][0], 'binary' if binary_prefix else 'utf8') + # normal strings should not get prefix + c.execute('SELECT CHARSET(%s)', ('str',)) + self.assertEqual(c.fetchall()[0][0], 'utf8') + + +if __name__ == '__main__': + if test_MySQLdb.leak_test: + import gc + gc.enable() + gc.set_debug(gc.DEBUG_LEAK) + unittest.main() diff --git a/tests/test_MySQLdb_dbapi20.py b/tests/test_MySQLdb_dbapi20.py index 85fc5d51..1e808bd1 100644 --- a/tests/test_MySQLdb_dbapi20.py +++ b/tests/test_MySQLdb_dbapi20.py @@ -20,7 +20,7 @@ def test_nextset(self): pass test for an exception if the statement cannot return a result set. MySQL always returns a result set; it's just that some things return empty result sets.""" - + def test_fetchall(self): con = self._connect() try: @@ -66,10 +66,10 @@ def test_fetchall(self): 'cursor.fetchall should return an empty list if ' 'a select query returns no rows' ) - + finally: con.close() - + def test_fetchone(self): con = self._connect() try: @@ -148,7 +148,7 @@ def test_callproc(self): def help_nextset_setUp(self,cur): ''' Should create a procedure called deleteme - that returns two result sets, first the + that returns two result sets, first the number of rows in booze then "name from booze" ''' sql=""" @@ -200,6 +200,6 @@ def test_nextset(self): finally: con.close() - + if __name__ == '__main__': unittest.main() From 509b6ce83713b32f91203e6148330f57b152f607 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 7 Dec 2018 02:09:22 +0900 Subject: [PATCH 093/177] Document removal of _last_executed Fixes #303 --- HISTORY.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/HISTORY.rst b/HISTORY.rst index b4fed226..4691fceb 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -42,6 +42,11 @@ Release: 2018-12-04 * Fix SSCursor may raise same exception twice (#282) + * This removed ``Cursor._last_executed`` which is duplicate of ``Cursor._executed``. + Both member are private. So this type of change is not documented generally. + But Django used the private member for ``last_executed_query`` implementation. + If you use the method, you shouldn't upgrade mysqlclient to this version. + * ``waiter`` option is now deprecated. (#285) * Fixed SSL support is not detected when built with MySQL < 5.1 (#291) From 628bb1b433f9bb2205964d13e0142aa25e777905 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 7 Dec 2018 02:11:51 +0900 Subject: [PATCH 094/177] Refine last commit --- HISTORY.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 4691fceb..1a5fd824 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -42,10 +42,11 @@ Release: 2018-12-04 * Fix SSCursor may raise same exception twice (#282) - * This removed ``Cursor._last_executed`` which is duplicate of ``Cursor._executed``. - Both member are private. So this type of change is not documented generally. - But Django used the private member for ``last_executed_query`` implementation. - If you use the method, you shouldn't upgrade mysqlclient to this version. + * This removed ``Cursor._last_executed`` which was duplicate of ``Cursor._executed``. + Both members are private. So this type of changes are not documented in changelog + generally. But Django used the private member for ``last_executed_query`` implementation. + If you use the method the method directly or indirectly, this version will break + your application. See https://code.djangoproject.com/ticket/30013 * ``waiter`` option is now deprecated. (#285) From 5e8eeac47f511ca63d6b40dcc3a47190d96a0c0d Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Fri, 7 Dec 2018 02:39:33 +0900 Subject: [PATCH 095/177] Stop using surrogate escape (#302) It was workaround for `bytes %`. Since we dropped Python 3.4 support, we can use just `bytes %` now. --- MySQLdb/_mysql.c | 35 +++++++--------- MySQLdb/connections.py | 48 +++++----------------- MySQLdb/converters.py | 4 +- MySQLdb/cursors.py | 90 +++++++++++++----------------------------- MySQLdb/times.py | 4 +- 5 files changed, 56 insertions(+), 125 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index b45682df..6c12b923 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -915,14 +915,15 @@ _mysql.string_literal(obj) cannot handle character sets."; static PyObject * _mysql_string_literal( _mysql_ConnectionObject *self, - PyObject *args) + PyObject *o) { - PyObject *str, *s, *o, *d; + PyObject *str, *s; char *in, *out; int len, size; + if (self && PyModule_Check((PyObject*)self)) self = NULL; - if (!PyArg_ParseTuple(args, "O|O:string_literal", &o, &d)) return NULL; + if (PyBytes_Check(o)) { s = o; Py_INCREF(s); @@ -965,33 +966,25 @@ static PyObject *_mysql_NULL; static PyObject * _escape_item( + PyObject *self, PyObject *item, PyObject *d) { PyObject *quoted=NULL, *itemtype, *itemconv; - if (!(itemtype = PyObject_Type(item))) - goto error; + if (!(itemtype = PyObject_Type(item))) { + return NULL; + } itemconv = PyObject_GetItem(d, itemtype); Py_DECREF(itemtype); if (!itemconv) { PyErr_Clear(); - itemconv = PyObject_GetItem(d, -#ifdef IS_PY3K - (PyObject *) &PyUnicode_Type); -#else - (PyObject *) &PyString_Type); -#endif - } - if (!itemconv) { - PyErr_SetString(PyExc_TypeError, - "no default type converter defined"); - goto error; + return _mysql_string_literal((_mysql_ConnectionObject*)self, item); } Py_INCREF(d); quoted = PyObject_CallFunction(itemconv, "OO", item, d); Py_DECREF(d); Py_DECREF(itemconv); -error: + return quoted; } @@ -1013,14 +1006,14 @@ _mysql_escape( "argument 2 must be a mapping"); return NULL; } - return _escape_item(o, d); + return _escape_item(self, o, d); } else { if (!self) { PyErr_SetString(PyExc_TypeError, "argument 2 must be a mapping"); return NULL; } - return _escape_item(o, + return _escape_item(self, o, ((_mysql_ConnectionObject *) self)->converter); } } @@ -2264,7 +2257,7 @@ static PyMethodDef _mysql_ConnectionObject_methods[] = { { "string_literal", (PyCFunction)_mysql_string_literal, - METH_VARARGS, + METH_O, _mysql_string_literal__doc__}, { "thread_id", @@ -2587,7 +2580,7 @@ _mysql_methods[] = { { "string_literal", (PyCFunction)_mysql_string_literal, - METH_VARARGS, + METH_O, _mysql_string_literal__doc__ }, { diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 96b01528..27a8d437 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -16,18 +16,6 @@ ) -if not PY2: - if sys.version_info[:2] < (3, 6): - # See http://bugs.python.org/issue24870 - _surrogateescape_table = [chr(i) if i < 0x80 else chr(i + 0xdc00) for i in range(256)] - - def _fast_surrogateescape(s): - return s.decode('latin1').translate(_surrogateescape_table) - else: - def _fast_surrogateescape(s): - return s.decode('ascii', 'surrogateescape') - - re_numeric_part = re.compile(r"^(\d+)") def numeric_part(s): @@ -183,21 +171,8 @@ class object, used to create cursors (keyword only) self.encoding = 'ascii' # overridden in set_character_set() db = proxy(self) - # Note: string_literal() is called for bytes object on Python 3 (via bytes_literal) - def string_literal(obj, dummy=None): - return db.string_literal(obj) - - if PY2: - # unicode_literal is called for only unicode object. - def unicode_literal(u, dummy=None): - return db.string_literal(u.encode(db.encoding)) - else: - # unicode_literal() is called for arbitrary object. - def unicode_literal(u, dummy=None): - return db.string_literal(str(u).encode(db.encoding)) - - def bytes_literal(obj, dummy=None): - return b'_binary' + db.string_literal(obj) + def unicode_literal(u, dummy=None): + return db.string_literal(u.encode(db.encoding)) def string_decoder(s): return s.decode(db.encoding) @@ -214,7 +189,6 @@ def string_decoder(s): FIELD_TYPE.MEDIUM_BLOB, FIELD_TYPE.LONG_BLOB, FIELD_TYPE.BLOB): self.converter[t].append((None, string_decoder)) - self.encoders[bytes] = string_literal self.encoders[unicode] = unicode_literal self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS if self._transactional: @@ -250,7 +224,7 @@ def _bytes_literal(self, bs): return x def _tuple_literal(self, t): - return "(%s)" % (','.join(map(self.literal, t))) + return b"(%s)" % (b','.join(map(self.literal, t))) def literal(self, o): """If o is a single object, returns an SQL literal as a string. @@ -260,7 +234,9 @@ def literal(self, o): Non-standard. For internal use; do not use this in your applications. """ - if isinstance(o, bytearray): + if isinstance(o, unicode): + s = self.string_literal(o.encode(self.encoding)) + elif isinstance(o, bytearray): s = self._bytes_literal(o) elif not PY2 and isinstance(o, bytes): s = self._bytes_literal(o) @@ -268,13 +244,9 @@ def literal(self, o): s = self._tuple_literal(o) else: s = self.escape(o, self.encoders) - # Python 3(~3.4) doesn't support % operation for bytes object. - # We should decode it before using %. - # Decoding with ascii and surrogateescape allows convert arbitrary - # bytes to unicode and back again. - # See http://python.org/dev/peps/pep-0383/ - if not PY2 and isinstance(s, (bytes, bytearray)): - return _fast_surrogateescape(s) + if isinstance(s, unicode): + s = s.encode(self.encoding) + assert isinstance(s, bytes) return s def begin(self): @@ -282,7 +254,7 @@ def begin(self): This method is not used when autocommit=False (default). """ - self.query("BEGIN") + self.query(b"BEGIN") if not hasattr(_mysql.connection, 'warning_count'): diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index c13e4265..20d919f7 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -53,7 +53,7 @@ def Str2Set(s): def Set2Str(s, d): # Only support ascii string. Not tested. - return string_literal(','.join(s), d) + return string_literal(','.join(s)) def Thing2Str(s, d): """Convert something into a string via str().""" @@ -80,7 +80,7 @@ def Thing2Literal(o, d): MySQL-3.23 or newer, string_literal() is a method of the _mysql.MYSQL object, and this function will be overridden with that method when the connection is created.""" - return string_literal(o, d) + return string_literal(o) def Decimal2Literal(o, d): return format(o, 'f') diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 97908249..9a5e76f9 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -15,13 +15,6 @@ NotSupportedError, ProgrammingError) -PY2 = sys.version_info[0] == 2 -if PY2: - text_type = unicode -else: - text_type = str - - #: Regular expression for :meth:`Cursor.executemany`. #: executemany only supports simple bulk insert. #: You can use it to load large dataset. @@ -95,31 +88,28 @@ def __exit__(self, *exc_info): del exc_info self.close() - def _ensure_bytes(self, x, encoding=None): - if isinstance(x, text_type): - x = x.encode(encoding) - elif isinstance(x, (tuple, list)): - x = type(x)(self._ensure_bytes(v, encoding=encoding) for v in x) - return x - def _escape_args(self, args, conn): - ensure_bytes = partial(self._ensure_bytes, encoding=conn.encoding) + encoding = conn.encoding + literal = conn.literal + + def ensure_bytes(x): + if isinstance(x, unicode): + return x.encode(encoding) + elif isinstance(x, tuple): + return tuple(map(ensure_bytes, x)) + elif isinstance(x, list): + return list(map(ensure_bytes, x)) + return x if isinstance(args, (tuple, list)): - if PY2: - args = tuple(map(ensure_bytes, args)) - return tuple(conn.literal(arg) for arg in args) + return tuple(literal(ensure_bytes(arg)) for arg in args) elif isinstance(args, dict): - if PY2: - args = dict((ensure_bytes(key), ensure_bytes(val)) for - (key, val) in args.items()) - return dict((key, conn.literal(val)) for (key, val) in args.items()) + return {ensure_bytes(key): literal(ensure_bytes(val)) + for (key, val) in args.items()} else: # If it's not a dictionary let's try escaping it anyways. # Worst case it will throw a Value error - if PY2: - args = ensure_bytes(args) - return conn.literal(args) + return literal(ensure_bytes(args)) def _check_executed(self): if not self._executed: @@ -186,14 +176,7 @@ def execute(self, query, args=None): pass db = self._get_db() - # NOTE: - # Python 2: query should be bytes when executing %. - # All unicode in args should be encoded to bytes on Python 2. - # Python 3: query should be str (unicode) when executing %. - # All bytes in args should be decoded with ascii and surrogateescape on Python 3. - # db.literal(obj) always returns str. - - if PY2 and isinstance(query, unicode): + if isinstance(query, unicode): query = query.encode(db.encoding) if args is not None: @@ -201,16 +184,12 @@ def execute(self, query, args=None): args = dict((key, db.literal(item)) for key, item in args.items()) else: args = tuple(map(db.literal, args)) - if not PY2 and isinstance(query, (bytes, bytearray)): - query = query.decode(db.encoding) try: query = query % args except TypeError as m: raise ProgrammingError(str(m)) - if isinstance(query, unicode): - query = query.encode(db.encoding, 'surrogateescape') - + assert isinstance(query, (bytes, bytearray)) res = self._query(query) return res @@ -247,29 +226,19 @@ def executemany(self, query, args): def _do_execute_many(self, prefix, values, postfix, args, max_stmt_length, encoding): conn = self._get_db() escape = self._escape_args - if isinstance(prefix, text_type): + if isinstance(prefix, unicode): prefix = prefix.encode(encoding) - if PY2 and isinstance(values, text_type): + if isinstance(values, unicode): values = values.encode(encoding) - if isinstance(postfix, text_type): + if isinstance(postfix, unicode): postfix = postfix.encode(encoding) sql = bytearray(prefix) args = iter(args) v = values % escape(next(args), conn) - if isinstance(v, text_type): - if PY2: - v = v.encode(encoding) - else: - v = v.encode(encoding, 'surrogateescape') sql += v rows = 0 for arg in args: v = values % escape(arg, conn) - if isinstance(v, text_type): - if PY2: - v = v.encode(encoding) - else: - v = v.encode(encoding, 'surrogateescape') if len(sql) + len(v) + len(postfix) + 1 > max_stmt_length: rows += self.execute(sql + postfix) sql = bytearray(prefix) @@ -308,22 +277,19 @@ def callproc(self, procname, args=()): to advance through all result sets; otherwise you may get disconnected. """ - db = self._get_db() + if isinstance(procname, unicode): + procname = procname.encode(db.encoding) if args: - fmt = '@_{0}_%d=%s'.format(procname) - q = 'SET %s' % ','.join(fmt % (index, db.literal(arg)) - for index, arg in enumerate(args)) - if isinstance(q, unicode): - q = q.encode(db.encoding, 'surrogateescape') + fmt = b'@_' + procname + b'_%d=%s' + q = b'SET %s' % b','.join(fmt % (index, db.literal(arg)) + for index, arg in enumerate(args)) self._query(q) self.nextset() - q = "CALL %s(%s)" % (procname, - ','.join(['@_%s_%d' % (procname, i) - for i in range(len(args))])) - if isinstance(q, unicode): - q = q.encode(db.encoding, 'surrogateescape') + q = b"CALL %s(%s)" % (procname, + b','.join([b'@_%s_%d' % (procname, i) + for i in range(len(args))])) self._query(q) return args diff --git a/MySQLdb/times.py b/MySQLdb/times.py index 510d1c7c..a7eaa53b 100644 --- a/MySQLdb/times.py +++ b/MySQLdb/times.py @@ -124,11 +124,11 @@ def Date_or_None(s): def DateTime2literal(d, c): """Format a DateTime object as an ISO timestamp.""" - return string_literal(format_TIMESTAMP(d), c) + return string_literal(format_TIMESTAMP(d)) def DateTimeDelta2literal(d, c): """Format a DateTimeDelta object as a time.""" - return string_literal(format_TIMEDELTA(d),c) + return string_literal(format_TIMEDELTA(d)) def mysql_timestamp_converter(s): """Convert a MySQL TIMESTAMP to a Timestamp object.""" From 819688b630be850ee485d265b60cd9fa4040c07e Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Fri, 7 Dec 2018 13:50:05 +0900 Subject: [PATCH 096/177] Simplify converters (#304) --- MySQLdb/_mysql.c | 3 --- MySQLdb/converters.py | 33 ++++++++----------------------- tests/test_MySQLdb_nonstandard.py | 4 ---- 3 files changed, 8 insertions(+), 32 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 6c12b923..a90d7ab1 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -2730,9 +2730,6 @@ init_mysql(void) _mysql_NewException(dict, edict, "NotSupportedError"))) goto error; Py_DECREF(emod); - if (!(_mysql_NULL = PyString_FromString("NULL"))) - goto error; - if (PyDict_SetItemString(dict, "NULL", _mysql_NULL)) goto error; error: if (PyErr_Occurred()) { PyErr_SetString(PyExc_ImportError, "_mysql: init failed"); diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index 20d919f7..6394a5c6 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -30,8 +30,9 @@ (with the copy() method), modify the copies, and then pass them to MySQL.connect(). """ +from decimal import Decimal -from MySQLdb._mysql import string_literal, escape, NULL +from MySQLdb._mysql import string_literal, escape from MySQLdb.constants import FIELD_TYPE, FLAG from MySQLdb.times import * from MySQLdb.compat import PY2, long @@ -46,10 +47,8 @@ ArrayType = array.array -def Bool2Str(s, d): return str(int(s)) - -def Str2Set(s): - return set([ i for i in s.split(',') if i ]) +def Bool2Str(s, d): + return b'1' if s else b'0' def Set2Str(s, d): # Only support ascii string. Not tested. @@ -73,7 +72,7 @@ def Float2Str(o, d): def None2NULL(o, d): """Convert None to NULL.""" - return NULL # duh + return b"NULL" def Thing2Literal(o, d): """Convert something into a SQL string literal. If using @@ -85,9 +84,6 @@ def Thing2Literal(o, d): def Decimal2Literal(o, d): return format(o, 'f') -def char_array(s): - return array.array('c', s) - def array2Str(o, d): return Thing2Literal(o.tostring(), d) @@ -109,18 +105,18 @@ def quote_tuple(t, d): DateTimeDeltaType: DateTimeDelta2literal, str: Thing2Literal, # default set: Set2Str, + Decimal: Decimal2Literal, FIELD_TYPE.TINY: int, FIELD_TYPE.SHORT: int, FIELD_TYPE.LONG: long, FIELD_TYPE.FLOAT: float, FIELD_TYPE.DOUBLE: float, - FIELD_TYPE.DECIMAL: float, - FIELD_TYPE.NEWDECIMAL: float, + FIELD_TYPE.DECIMAL: Decimal, + FIELD_TYPE.NEWDECIMAL: Decimal, FIELD_TYPE.LONGLONG: long, FIELD_TYPE.INT24: int, FIELD_TYPE.YEAR: int, - FIELD_TYPE.SET: Str2Set, FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter, FIELD_TYPE.DATETIME: DateTime_or_None, FIELD_TYPE.TIME: TimeDelta_or_None, @@ -134,16 +130,3 @@ def quote_tuple(t, d): FIELD_TYPE.VAR_STRING: _bytes_or_str, FIELD_TYPE.VARCHAR: _bytes_or_str, } - -if PY2: - conversions[unicode] = Unicode2Str -else: - conversions[bytes] = Thing2Literal - -try: - from decimal import Decimal - conversions[FIELD_TYPE.DECIMAL] = Decimal - conversions[FIELD_TYPE.NEWDECIMAL] = Decimal - conversions[Decimal] = Decimal2Literal -except ImportError: - pass diff --git a/tests/test_MySQLdb_nonstandard.py b/tests/test_MySQLdb_nonstandard.py index e60a2a77..55cfc630 100644 --- a/tests/test_MySQLdb_nonstandard.py +++ b/tests/test_MySQLdb_nonstandard.py @@ -25,10 +25,6 @@ def test_set_inequality_membership(self): class TestCoreModule(unittest.TestCase): """Core _mysql module features.""" - def test_NULL(self): - """Should have a NULL constant.""" - self.assertEqual(_mysql.NULL, 'NULL') - def test_version(self): """Version information sanity.""" self.assertTrue(isinstance(_mysql.__version__, str)) From e48e393e68112f88f3db575ef4a34b1c5ad08370 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 7 Dec 2018 13:51:17 +0900 Subject: [PATCH 097/177] Update compat --- MySQLdb/compat.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/MySQLdb/compat.py b/MySQLdb/compat.py index 70580b61..8fe6709a 100644 --- a/MySQLdb/compat.py +++ b/MySQLdb/compat.py @@ -1,12 +1,12 @@ import sys -if sys.version_info[0] == 3: - PY2 = False - unicode = str - unichr = chr - long = int -else: +if sys.version_info[0] == 2: PY2 = True unicode = unicode unichr = unichr long = long +else: + PY2 = False + unicode = str + unichr = chr + long = int From ae6b108b56fa5f66f5dcd95cd20ffd1a80fa96c1 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 10 Dec 2018 19:08:19 +0900 Subject: [PATCH 098/177] Remove HAVE_MYSQL_OPT_TIMEOUTS Drop MySQL<5.1.12 support --- MySQLdb/_mysql.c | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index a90d7ab1..2d236982 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -92,12 +92,6 @@ typedef struct { extern PyTypeObject _mysql_ResultObject_Type; -/* According to https://dev.mysql.com/doc/refman/5.1/en/mysql-options.html - The MYSQL_OPT_READ_TIMEOUT appear in the version 5.1.12 */ -#if MYSQL_VERSION_ID > 50112 -#define HAVE_MYSQL_OPT_TIMEOUTS 1 -#endif - PyObject * _mysql_Exception(_mysql_ConnectionObject *c) { @@ -382,16 +376,11 @@ _mysql_ConnectionObject_Initialize( "read_default_file", "read_default_group", "client_flag", "ssl", "local_infile", -#ifdef HAVE_MYSQL_OPT_TIMEOUTS - "read_timeout", - "write_timeout", -#endif + "read_timeout", "write_timeout", NULL } ; int connect_timeout = 0; -#ifdef HAVE_MYSQL_OPT_TIMEOUTS int read_timeout = 0; int write_timeout = 0; -#endif int compress = -1, named_pipe = -1, local_infile = -1; char *init_command=NULL, *read_default_file=NULL, @@ -401,11 +390,7 @@ _mysql_ConnectionObject_Initialize( self->open = 0; if (!PyArg_ParseTupleAndKeywords(args, kwargs, -#ifdef HAVE_MYSQL_OPT_TIMEOUTS "|ssssisOiiisssiOiii:connect", -#else - "|ssssisOiiisssiOi:connect", -#endif kwlist, &host, &user, &passwd, &db, &port, &unix_socket, &conv, @@ -414,11 +399,9 @@ _mysql_ConnectionObject_Initialize( &init_command, &read_default_file, &read_default_group, &client_flag, &ssl, - &local_infile -#ifdef HAVE_MYSQL_OPT_TIMEOUTS - , &read_timeout - , &write_timeout -#endif + &local_infile, + &read_timeout, + &write_timeout )) return -1; @@ -448,7 +431,6 @@ _mysql_ConnectionObject_Initialize( mysql_options(&(self->connection), MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout); } -#ifdef HAVE_MYSQL_OPT_TIMEOUTS if (read_timeout) { unsigned int timeout = read_timeout; mysql_options(&(self->connection), MYSQL_OPT_READ_TIMEOUT, @@ -459,7 +441,6 @@ _mysql_ConnectionObject_Initialize( mysql_options(&(self->connection), MYSQL_OPT_WRITE_TIMEOUT, (char *)&timeout); } -#endif if (compress != -1) { mysql_options(&(self->connection), MYSQL_OPT_COMPRESS, 0); client_flag |= CLIENT_COMPRESS; From c7188e64bbe17f31739210ae8219582efb175913 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 10 Dec 2018 20:26:37 +0900 Subject: [PATCH 099/177] Update HISTORY --- HISTORY.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/HISTORY.rst b/HISTORY.rst index 1a5fd824..fd47e415 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -22,6 +22,12 @@ Release: TBD * Remove automatic MySQL warning checking. +* Drop support for MySQL Connector/C with MySQL<5.1.12. + +* Remove ``_mysql.NULL`` constant. + +* Support non-ASCII field name with non-UTF-8 connection encoding. (#210) + ====================== What's new in 1.3.14 From 3bea8cd8dd89c1d078a6ff2a36ef5804429f398f Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 11 Dec 2018 11:50:42 +0900 Subject: [PATCH 100/177] Faster string and int decoder (#305) --- MySQLdb/_mysql.c | 79 +++++++++++++++++++++++++++++++----------- MySQLdb/connections.py | 7 ++-- MySQLdb/converters.py | 22 ++++++------ 3 files changed, 71 insertions(+), 37 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 2d236982..53e57f85 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -37,6 +37,8 @@ PERFORMANCE OF THIS SOFTWARE. #include "Python.h" #if PY_MAJOR_VERSION >= 3 #define IS_PY3K +#define PyInt_Type PyLong_Type +#define PyInt_FromString PyLong_FromString #define PyInt_FromLong(n) PyLong_FromLong(n) #define PyInt_Check(n) PyLong_Check(n) #define PyInt_AS_LONG(n) PyLong_AS_LONG(n) @@ -87,6 +89,7 @@ typedef struct { int use; char has_next; PyObject *converter; + const char *encoding; } _mysql_ResultObject; extern PyTypeObject _mysql_ResultObject_Type; @@ -203,6 +206,25 @@ _mysql_Exception(_mysql_ConnectionObject *c) return NULL; } +static const char *utf8 = "utf8"; + +static const char* +_get_encoding(MYSQL *mysql) +{ + MY_CHARSET_INFO cs; + mysql_get_character_set_info(mysql, &cs); + if (strncmp(utf8, cs.csname, 4) == 0) { // utf8, utf8mb3, utf8mb4 + return utf8; + } + else if (strncmp("koi8r", cs.csname, 5) == 0) { + return "koi8_r"; + } + else if (strncmp("koi8u", cs.csname, 5) == 0) { + return "koi8_u"; + } + return cs.csname; +} + static char _mysql_thread_safe__doc__[] = "Indicates whether the client is compiled as thread-safe."; @@ -251,6 +273,9 @@ _mysql_ResultObject_Initialize( self->result = result; self->has_next = (char)mysql_more_results(&(conn->connection)); Py_END_ALLOW_THREADS ; + + self->encoding = _get_encoding(&(conn->connection)); + //fprintf(stderr, "encoding=%s\n", self->encoding); if (!result) { if (mysql_errno(&(conn->connection))) { _mysql_Exception(conn); @@ -289,6 +314,8 @@ _mysql_ResultObject_Initialize( int j, n2=PySequence_Size(fun); if (fields[i].charsetnr != 63) { /* maaagic */ flags &= ~BINARY_FLAG; + } else { + flags |= BINARY_FLAG; } for (j=0; jconnection, &cs); - if (strncmp("utf8", cs.name, 4) == 0) { // utf8, utf8mb3, utf8mb4 - isutf8 = 1; - } - n = mysql_num_fields(self->result); fields = mysql_fetch_fields(self->result); if (!(d = PyTuple_New(n))) return NULL; @@ -1029,10 +1047,10 @@ _mysql_ResultObject_describe( PyObject *t; #ifdef IS_PY3K PyObject *name; - if (isutf8) { + if (self->encoding == utf8) { name = PyUnicode_DecodeUTF8(fields[i].name, fields[i].name_length, "replace"); } else { - name = PyUnicode_Decode(fields[i].name, fields[i].name_length, cs.name, "replace"); + name = PyUnicode_Decode(fields[i].name, fields[i].name_length, self->encoding, "replace"); } if (name == NULL) { goto error; @@ -1089,9 +1107,10 @@ _mysql_ResultObject_field_flags( static PyObject * _mysql_field_to_python( PyObject *converter, - char *rowitem, + const char *rowitem, unsigned long length, - MYSQL_FIELD *field) + MYSQL_FIELD *field, + const char *encoding) { PyObject *v; #ifdef IS_PY3K @@ -1110,7 +1129,27 @@ _mysql_field_to_python( } #endif if (rowitem) { - if (converter != Py_None) { + if (converter == (PyObject*)&PyUnicode_Type) { + if (encoding == utf8) { + //fprintf(stderr, "decoding with utf8!\n"); + v = PyUnicode_DecodeUTF8(rowitem, length, NULL); + } else { + //fprintf(stderr, "decoding with %s\n", encoding); + v = PyUnicode_Decode(rowitem, length, encoding, NULL); + } + } + else if (converter == (PyObject*)&PyBytes_Type) { + //fprintf(stderr, "decoding with bytes\n", encoding); + v = PyBytes_FromStringAndSize(rowitem, length); + } + else if (converter == (PyObject*)&PyInt_Type) { + //fprintf(stderr, "decoding with int\n", encoding); + v = PyInt_FromString(rowitem, NULL, 10); + } + else if (converter != Py_None) { + //fprintf(stderr, "decoding with callback\n"); + //PyObject_Print(converter, stderr, 0); + //fprintf(stderr, "\n"); v = PyObject_CallFunction(converter, #ifdef IS_PY3K binary ? "y#" : "s#", @@ -1120,6 +1159,7 @@ _mysql_field_to_python( rowitem, (int)length); } else { + //fprintf(stderr, "converter=None\n"); #ifdef IS_PY3K if (!binary) { v = PyUnicode_FromStringAndSize(rowitem, (int)length); @@ -1153,7 +1193,7 @@ _mysql_row_to_tuple( for (i=0; iconverter, i); - v = _mysql_field_to_python(c, row[i], length[i], &fields[i]); + v = _mysql_field_to_python(c, row[i], length[i], &fields[i], self->encoding); if (!v) goto error; PyTuple_SET_ITEM(r, i, v); } @@ -1180,7 +1220,7 @@ _mysql_row_to_dict( for (i=0; iconverter, i); - v = _mysql_field_to_python(c, row[i], length[i], &fields[i]); + v = _mysql_field_to_python(c, row[i], length[i], &fields[i], self->encoding); if (!v) goto error; if (!PyMapping_HasKeyString(r, fields[i].name)) { PyMapping_SetItemString(r, fields[i].name, v); @@ -1219,7 +1259,7 @@ _mysql_row_to_dict_old( for (i=0; iconverter, i); - v = _mysql_field_to_python(c, row[i], length[i], &fields[i]); + v = _mysql_field_to_python(c, row[i], length[i], &fields[i], self->encoding); if (!v) goto error; { int len=0; @@ -1422,8 +1462,7 @@ _mysql_ConnectionObject_set_character_set( err = mysql_set_character_set(&(self->connection), s); Py_END_ALLOW_THREADS if (err) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } #if MYSQL_VERSION_ID >= 50010 @@ -2634,12 +2673,10 @@ init_mysql(void) _mysql_ConnectionObject_Type.ob_type = &PyType_Type; _mysql_ResultObject_Type.ob_type = &PyType_Type; #endif -#if PY_VERSION_HEX >= 0x02020000 _mysql_ConnectionObject_Type.tp_alloc = PyType_GenericAlloc; _mysql_ConnectionObject_Type.tp_new = PyType_GenericNew; _mysql_ResultObject_Type.tp_alloc = PyType_GenericAlloc; _mysql_ResultObject_Type.tp_new = PyType_GenericNew; -#endif #ifdef IS_PY3K if (PyType_Ready(&_mysql_ConnectionObject_Type) < 0) return NULL; diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 27a8d437..e902912f 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -114,7 +114,7 @@ class object, used to create cursors (keyword only) documentation for the MySQL C API for some hints on what they do. """ from MySQLdb.constants import CLIENT, FIELD_TYPE - from MySQLdb.converters import conversions + from MySQLdb.converters import conversions, _bytes_or_str from weakref import proxy kwargs2 = kwargs.copy() @@ -174,9 +174,6 @@ class object, used to create cursors (keyword only) def unicode_literal(u, dummy=None): return db.string_literal(u.encode(db.encoding)) - def string_decoder(s): - return s.decode(db.encoding) - if not charset: charset = self.character_set_name() self.set_character_set(charset) @@ -187,7 +184,7 @@ def string_decoder(s): if use_unicode: for t in (FIELD_TYPE.STRING, FIELD_TYPE.VAR_STRING, FIELD_TYPE.VARCHAR, FIELD_TYPE.TINY_BLOB, FIELD_TYPE.MEDIUM_BLOB, FIELD_TYPE.LONG_BLOB, FIELD_TYPE.BLOB): - self.converter[t].append((None, string_decoder)) + self.converter[t] = _bytes_or_str self.encoders[unicode] = unicode_literal self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index 6394a5c6..7b1eb035 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -35,7 +35,7 @@ from MySQLdb._mysql import string_literal, escape from MySQLdb.constants import FIELD_TYPE, FLAG from MySQLdb.times import * -from MySQLdb.compat import PY2, long +from MySQLdb.compat import PY2, long, unicode NoneType = type(None) @@ -91,7 +91,7 @@ def quote_tuple(t, d): return "(%s)" % (','.join(escape_sequence(t, d))) # bytes or str regarding to BINARY_FLAG. -_bytes_or_str = [(FLAG.BINARY, bytes)] +_bytes_or_str = ((FLAG.BINARY, bytes), (None, unicode)) conversions = { int: Thing2Str, @@ -109,12 +109,12 @@ def quote_tuple(t, d): FIELD_TYPE.TINY: int, FIELD_TYPE.SHORT: int, - FIELD_TYPE.LONG: long, + FIELD_TYPE.LONG: int, FIELD_TYPE.FLOAT: float, FIELD_TYPE.DOUBLE: float, FIELD_TYPE.DECIMAL: Decimal, FIELD_TYPE.NEWDECIMAL: Decimal, - FIELD_TYPE.LONGLONG: long, + FIELD_TYPE.LONGLONG: int, FIELD_TYPE.INT24: int, FIELD_TYPE.YEAR: int, FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter, @@ -122,11 +122,11 @@ def quote_tuple(t, d): FIELD_TYPE.TIME: TimeDelta_or_None, FIELD_TYPE.DATE: Date_or_None, - FIELD_TYPE.TINY_BLOB: _bytes_or_str, - FIELD_TYPE.MEDIUM_BLOB: _bytes_or_str, - FIELD_TYPE.LONG_BLOB: _bytes_or_str, - FIELD_TYPE.BLOB: _bytes_or_str, - FIELD_TYPE.STRING: _bytes_or_str, - FIELD_TYPE.VAR_STRING: _bytes_or_str, - FIELD_TYPE.VARCHAR: _bytes_or_str, + FIELD_TYPE.TINY_BLOB: bytes, + FIELD_TYPE.MEDIUM_BLOB: bytes, + FIELD_TYPE.LONG_BLOB: bytes, + FIELD_TYPE.BLOB: bytes, + FIELD_TYPE.STRING: bytes, + FIELD_TYPE.VAR_STRING: bytes, + FIELD_TYPE.VARCHAR: bytes, } From 2760691af000c823f035ba2ad32dc520a1a52cf0 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 11 Dec 2018 14:57:15 +0900 Subject: [PATCH 101/177] 1.4.0rc1 --- HISTORY.rst | 1 + metadata.cfg | 10 ++++------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index fd47e415..098a3047 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -28,6 +28,7 @@ Release: TBD * Support non-ASCII field name with non-UTF-8 connection encoding. (#210) +* Optimize decoding speed of string and integer types. ====================== What's new in 1.3.14 diff --git a/metadata.cfg b/metadata.cfg index 6901e7af..4ad40d37 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,11 +1,9 @@ [metadata] -version: 1.3.14 -version_info: (1,3,14,'final',0) +version: 1.4.0 +version_info: (1,4,0,'rc',1) description: Python interface to MySQL -author: Andy Dustman -author_email: farcepest@gmail.com -maintainer: INADA Naoki -maintainer_email: songofacandy@gmail.com +author: Inada Naoki +author_email: songofacandy@gmail.com license: GPL platforms: ALL url: https://github.com/PyMySQL/mysqlclient-python From b14b2bd6843fa243078c62b4af210aee0e8e2b9b Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 11 Dec 2018 16:16:29 +0900 Subject: [PATCH 102/177] fix version string --- metadata.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metadata.cfg b/metadata.cfg index 4ad40d37..82fdeb0f 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,5 +1,5 @@ [metadata] -version: 1.4.0 +version: 1.4.0rc1 version_info: (1,4,0,'rc',1) description: Python interface to MySQL author: Inada Naoki From 9f31f592f226c1687296e8dd0730b21f9c9285a3 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 11 Dec 2018 16:19:47 +0900 Subject: [PATCH 103/177] Removed MySQLdb.constants.REFRESH --- MySQLdb/constants/REFRESH.py | 17 ----------------- MySQLdb/constants/__init__.py | 2 +- doc/MySQLdb.constants.rst | 8 -------- metadata.cfg | 1 - 4 files changed, 1 insertion(+), 27 deletions(-) delete mode 100644 MySQLdb/constants/REFRESH.py diff --git a/MySQLdb/constants/REFRESH.py b/MySQLdb/constants/REFRESH.py deleted file mode 100644 index 4a08b94e..00000000 --- a/MySQLdb/constants/REFRESH.py +++ /dev/null @@ -1,17 +0,0 @@ -"""MySQL REFRESH Constants - -These constants seem to mostly deal with things internal to the -MySQL server. Forget you saw this. - -""" - -GRANT = 1 -LOG = 2 -TABLES = 4 -HOSTS = 8 -STATUS = 16 -THREADS = 32 -SLAVE = 64 -MASTER = 128 -READ_LOCK = 16384 -FAST = 32768 diff --git a/MySQLdb/constants/__init__.py b/MySQLdb/constants/__init__.py index 3da4a0e7..3e774cd9 100644 --- a/MySQLdb/constants/__init__.py +++ b/MySQLdb/constants/__init__.py @@ -1 +1 @@ -__all__ = ['CR', 'FIELD_TYPE','CLIENT','REFRESH','ER','FLAG'] +__all__ = ['CR', 'FIELD_TYPE','CLIENT','ER','FLAG'] diff --git a/doc/MySQLdb.constants.rst b/doc/MySQLdb.constants.rst index 9276f8fa..5c9a5389 100644 --- a/doc/MySQLdb.constants.rst +++ b/doc/MySQLdb.constants.rst @@ -49,11 +49,3 @@ constants Package :undoc-members: :show-inheritance: -:mod:`REFRESH` Module ---------------------- - -.. automodule:: MySQLdb.constants.REFRESH - :members: - :undoc-members: - :show-inheritance: - diff --git a/metadata.cfg b/metadata.cfg index 82fdeb0f..589e52f7 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -40,4 +40,3 @@ py_modules: MySQLdb.constants.ER MySQLdb.constants.FIELD_TYPE MySQLdb.constants.FLAG - MySQLdb.constants.REFRESH From 98dedd93e77066231e321738e49cf66a7ad04e7a Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 11 Dec 2018 16:20:43 +0900 Subject: [PATCH 104/177] Update HISTORY --- HISTORY.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/HISTORY.rst b/HISTORY.rst index 098a3047..54b09a53 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -30,6 +30,8 @@ Release: TBD * Optimize decoding speed of string and integer types. +* Removed ``MySQLdb.constants.REFRESH`` module. + ====================== What's new in 1.3.14 ====================== From 6cc21d9ead9bb2002db9c1844aa5f7583c5e0501 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 12 Dec 2018 16:15:24 +0900 Subject: [PATCH 105/177] Update INSTALL.rst --- INSTALL.rst | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/INSTALL.rst b/INSTALL.rst index 2588b99e..f5be3f4e 100644 --- a/INSTALL.rst +++ b/INSTALL.rst @@ -8,17 +8,17 @@ MySQLdb Installation Prerequisites ------------- -+ Python 2.7, 3.4 or higher ++ Python 2.7, 3.5 or higher + setuptools - * http://pypi.python.org/pypi/setuptools + * https://pypi.org/project/setuptools/ + MySQL 5.5 or higher - * http://www.mysql.com/downloads/ + * https://www.mysql.com/downloads/ - * MySQL-5.0 may work, but not supported. + * MySQL 5.1 may work, but not supported. + C compiler @@ -44,8 +44,7 @@ edit the [options] section of site.cfg: static if True, try to link against a static library; otherwise link - against dynamic libraries (default). You may need static linking - to use the embedded server. + against dynamic libraries (default). This option doesn't work for MySQL>5.6 since libmysqlclient requires libstdc++. If you want to use, add `-lstdc++` to mysql_config manually. @@ -94,25 +93,11 @@ contributes one, I will make it available. Several OS vendors have their own packages available. -RPMs -.... - -If you prefer to install RPMs, you can use the bdist_rpm command with -setup.py. This only builds the RPM; it does not install it. You may -want to use the --python=XXX option, where XXX is the name of the -Python executable, i.e. python, python2, python2.4; the default is -python. Using this will incorporate the Python executable name into -the package name for the RPM so you have install the package multiple -times if you need to support more than one version of Python. You can -also set this in setup.cfg. - - Red Hat Linux ............. MySQL-python is pre-packaged in Red Hat Linux 7.x and newer. This -includes Fedora Core and Red Hat Enterprise Linux. You can also -build your own RPM packages as described above. +includes Fedora Core and Red Hat Enterprise Linux. Debian GNU/Linux From 80278a266e1ccc7d28c768dd430eb5e800395349 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 12 Dec 2018 16:52:03 +0900 Subject: [PATCH 106/177] Update ER and CR (#307) --- MySQLdb/constants/CR.py | 9 +- MySQLdb/constants/ER.py | 251 ++-------------------------------------- 2 files changed, 12 insertions(+), 248 deletions(-) diff --git a/MySQLdb/constants/CR.py b/MySQLdb/constants/CR.py index 1b047243..753408ee 100644 --- a/MySQLdb/constants/CR.py +++ b/MySQLdb/constants/CR.py @@ -83,7 +83,6 @@ SHARED_MEMORY_CONNECT_SET_ERROR = 2046 CONN_UNKNOW_PROTOCOL = 2047 INVALID_CONN_HANDLE = 2048 -SECURE_AUTH = 2049 UNUSED_1 = 2049 FETCH_CANCELED = 2050 NO_DATA = 2051 @@ -94,11 +93,11 @@ STMT_CLOSED = 2056 NEW_STMT_METADATA = 2057 ALREADY_CONNECTED = 2058 -AUTH_PLUGIN_CANNOT_LOAD = 2058 -ALREADY_CONNECTED = 2059 AUTH_PLUGIN_CANNOT_LOAD = 2059 DUPLICATE_CONNECTION_ATTR = 2060 -PLUGIN_FUNCTION_NOT_SUPPORTED = 2060 AUTH_PLUGIN_ERR = 2061 +INSECURE_API_ERR = 2062 +FILE_NAME_TOO_LONG = 2063 +SSL_FIPS_MODE_ERR = 2064 MAX_ERROR = 2999 -ERROR_LAST = 2061 +ERROR_LAST = 2064 diff --git a/MySQLdb/constants/ER.py b/MySQLdb/constants/ER.py index 59db2e1f..2e623b51 100644 --- a/MySQLdb/constants/ER.py +++ b/MySQLdb/constants/ER.py @@ -2,7 +2,6 @@ These constants are error codes for the bulk of the error conditions that may occur. - """ if __name__ == "__main__": @@ -35,8 +34,6 @@ ERROR_FIRST = 1000 -HASHCHK = 1000 -NISAMCHK = 1001 NO = 1002 YES = 1003 CANT_CREATE_FILE = 1004 @@ -44,27 +41,20 @@ CANT_CREATE_DB = 1006 DB_CREATE_EXISTS = 1007 DB_DROP_EXISTS = 1008 -DB_DROP_DELETE = 1009 DB_DROP_RMDIR = 1010 -CANT_DELETE_FILE = 1011 CANT_FIND_SYSTEM_REC = 1012 CANT_GET_STAT = 1013 -CANT_GET_WD = 1014 CANT_LOCK = 1015 CANT_OPEN_FILE = 1016 FILE_NOT_FOUND = 1017 CANT_READ_DIR = 1018 -CANT_SET_WD = 1019 CHECKREAD = 1020 -DISK_FULL = 1021 DUP_KEY = 1022 -ERROR_ON_CLOSE = 1023 ERROR_ON_READ = 1024 ERROR_ON_RENAME = 1025 ERROR_ON_WRITE = 1026 FILE_USED = 1027 FILSORT_ABORT = 1028 -FORM_NOT_FOUND = 1029 GET_ERRNO = 1030 ILLEGAL_HA = 1031 KEY_NOT_FOUND = 1032 @@ -74,7 +64,6 @@ OPEN_AS_READONLY = 1036 OUTOFMEMORY = 1037 OUT_OF_SORTMEMORY = 1038 -UNEXPECTED_EOF = 1039 CON_COUNT_ERROR = 1040 OUT_OF_RESOURCES = 1041 BAD_HOST_ERROR = 1042 @@ -112,8 +101,6 @@ TOO_BIG_FIELDLENGTH = 1074 WRONG_AUTO_KEY = 1075 READY = 1076 -NORMAL_SHUTDOWN = 1077 -GOT_SIGNAL = 1078 SHUTDOWN_COMPLETE = 1079 FORCING_CLOSE = 1080 IPSOCK_ERROR = 1081 @@ -128,7 +115,6 @@ CANT_REMOVE_ALL_FIELDS = 1090 CANT_DROP_FIELD_OR_KEY = 1091 INSERT_INFO = 1092 -INSERT_TABLE_USED = 1093 UPDATE_TABLE_USED = 1093 NO_SUCH_THREAD = 1094 KILL_DENIED_ERROR = 1095 @@ -156,7 +142,7 @@ TOO_MANY_FIELDS = 1117 TOO_BIG_ROWSIZE = 1118 STACK_OVERRUN = 1119 -WRONG_OUTER_JOIN = 1120 +WRONG_OUTER_JOIN_UNUSED = 1120 NULL_COLUMN_IN_INDEX = 1121 CANT_FIND_UDF = 1122 CANT_INITIALIZE_UDF = 1123 @@ -186,10 +172,6 @@ NONEXISTING_TABLE_GRANT = 1147 NOT_ALLOWED_COMMAND = 1148 SYNTAX_ERROR = 1149 -DELAYED_CANT_CHANGE_LOCK = 1150 -UNUSED1 = 1150 -TOO_MANY_DELAYED_THREADS = 1151 -UNUSED2 = 1151 ABORTING_CONNECTION = 1152 NET_PACKET_TOO_LARGE = 1153 NET_READ_ERROR_FROM_PIPE = 1154 @@ -203,8 +185,6 @@ TOO_LONG_STRING = 1162 TABLE_CANT_HANDLE_BLOB = 1163 TABLE_CANT_HANDLE_AUTO_INCREMENT = 1164 -DELAYED_INSERT_TABLE_LOCKED = 1165 -UNUSED3 = 1165 WRONG_COLUMN_NAME = 1166 WRONG_KEY_COLUMN = 1167 WRONG_MRG_TABLE = 1168 @@ -213,7 +193,6 @@ PRIMARY_CANT_HAVE_NULL = 1171 TOO_MANY_ROWS = 1172 REQUIRES_PRIMARY_KEY = 1173 -NO_RAID_COMPILED = 1174 UPDATE_WITHOUT_KEY_IN_SAFE_MODE = 1175 KEY_DOES_NOT_EXITS = 1176 CHECK_NO_SUCH_TABLE = 1177 @@ -222,11 +201,7 @@ ERROR_DURING_COMMIT = 1180 ERROR_DURING_ROLLBACK = 1181 ERROR_DURING_FLUSH_LOGS = 1182 -ERROR_DURING_CHECKPOINT = 1183 NEW_ABORTING_CONNECTION = 1184 -DUMP_NOT_IMPLEMENTED = 1185 -FLUSH_MASTER_BINLOG_CLOSED = 1186 -INDEX_REBUILD = 1187 MASTER = 1188 MASTER_NET_READ = 1189 MASTER_NET_WRITE = 1190 @@ -237,7 +212,6 @@ CRASHED_ON_REPAIR = 1195 WARNING_NOT_COMPLETE_ROLLBACK = 1196 TRANS_CACHE_FULL = 1197 -SLAVE_MUST_STOP = 1198 SLAVE_NOT_RUNNING = 1199 BAD_SLAVE = 1200 MASTER_INFO = 1201 @@ -247,19 +221,14 @@ LOCK_WAIT_TIMEOUT = 1205 LOCK_TABLE_FULL = 1206 READ_ONLY_TRANSACTION = 1207 -DROP_DB_WITH_READ_LOCK = 1208 -CREATE_DB_WITH_READ_LOCK = 1209 WRONG_ARGUMENTS = 1210 NO_PERMISSION_TO_CREATE_USER = 1211 -UNION_TABLES_IN_DIFFERENT_DIR = 1212 LOCK_DEADLOCK = 1213 TABLE_CANT_HANDLE_FT = 1214 -TABLE_CANT_HANDLE_FULLTEXT = 1214 CANNOT_ADD_FOREIGN = 1215 NO_REFERENCED_ROW = 1216 ROW_IS_REFERENCED = 1217 CONNECT_TO_MASTER = 1218 -QUERY_ON_MASTER = 1219 ERROR_WHEN_EXECUTING_COMMAND = 1220 WRONG_USAGE = 1221 WRONG_NUMBER_OF_COLUMNS_IN_SELECT = 1222 @@ -285,7 +254,6 @@ SUBQUERY_NO_1_ROW = 1242 UNKNOWN_STMT_HANDLER = 1243 CORRUPT_HELP_DB = 1244 -CYCLIC_REFERENCE = 1245 AUTO_CONVERT = 1246 ILLEGAL_REFERENCE = 1247 DERIVED_MUST_HAVE_ALIAS = 1248 @@ -294,8 +262,6 @@ NOT_SUPPORTED_AUTH_MODE = 1251 SPATIAL_CANT_HAVE_NULL = 1252 COLLATION_CHARSET_MISMATCH = 1253 -SLAVE_WAS_RUNNING = 1254 -SLAVE_WAS_NOT_RUNNING = 1255 TOO_BIG_FOR_UNCOMPRESS = 1256 ZLIB_Z_MEM_ERROR = 1257 ZLIB_Z_BUF_ERROR = 1258 @@ -308,7 +274,6 @@ WARN_DATA_TRUNCATED = 1265 WARN_USING_OTHER_HANDLER = 1266 CANT_AGGREGATE_2COLLATIONS = 1267 -DROP_USER = 1268 REVOKE_GRANTS = 1269 CANT_AGGREGATE_3COLLATIONS = 1270 CANT_AGGREGATE_NCOLLATIONS = 1271 @@ -322,7 +287,6 @@ UNTIL_COND_IGNORED = 1279 WRONG_NAME_FOR_INDEX = 1280 WRONG_NAME_FOR_CATALOG = 1281 -WARN_QC_RESIZE = 1282 BAD_FT_COLUMN = 1283 UNKNOWN_KEY_CACHE = 1284 WARN_HOSTNAME_WONT_WORK = 1285 @@ -333,7 +297,6 @@ OPTION_PREVENTS_STATEMENT = 1290 DUPLICATED_VALUE_IN_TYPE = 1291 TRUNCATED_WRONG_VALUE = 1292 -TOO_MUCH_AUTO_TIMESTAMP_COLS = 1293 INVALID_ON_UPDATE = 1294 UNSUPPORTED_PS = 1295 GET_ERRMSG = 1296 @@ -386,10 +349,8 @@ FPARSER_ERROR_IN_PARAMETER = 1343 FPARSER_EOF_IN_UNKNOWN_PARAMETER = 1344 VIEW_NO_EXPLAIN = 1345 -FRM_UNKNOWN_TYPE = 1346 WRONG_OBJECT = 1347 NONUPDATEABLE_COLUMN = 1348 -VIEW_SELECT_DERIVED = 1349 VIEW_SELECT_CLAUSE = 1350 VIEW_SELECT_VARIABLE = 1351 VIEW_SELECT_TMPTABLE = 1352 @@ -398,7 +359,6 @@ WARN_VIEW_WITHOUT_KEY = 1355 VIEW_INVALID = 1356 SP_NO_DROP_SP = 1357 -SP_GOTO_IN_HNDLR = 1358 TRG_ALREADY_EXISTS = 1359 TRG_DOES_NOT_EXIST = 1360 TRG_ON_VIEW_OR_TEMP_TABLE = 1361 @@ -412,7 +372,6 @@ VIEW_CHECK_FAILED = 1369 PROCACCESS_DENIED_ERROR = 1370 RELAY_LOG_FAIL = 1371 -PASSWD_LENGTH = 1372 UNKNOWN_TARGET_BINLOG = 1373 IO_ERR_LOG_INDEX_READ = 1374 BINLOG_PURGE_PROHIBITED = 1375 @@ -423,13 +382,6 @@ RELAY_LOG_INIT = 1380 NO_BINARY_LOGGING = 1381 RESERVED_SYNTAX = 1382 -WSAS_FAILED = 1383 -DIFF_GROUPS_PROC = 1384 -NO_GROUP_FOR_PROC = 1385 -ORDER_WITH_PROC = 1386 -LOGGING_PROHIBIT_CHANGING_OF = 1387 -NO_FILE_MAPPING = 1388 -WRONG_MAGIC = 1389 PS_MANY_PARAM = 1390 KEY_PART_0 = 1391 VIEW_CHECKSUM = 1392 @@ -457,10 +409,8 @@ SP_NOT_VAR_ARG = 1414 SP_NO_RETSET = 1415 CANT_CREATE_GEOMETRY_OBJECT = 1416 -FAILED_ROUTINE_BREAK_BINLOG = 1417 BINLOG_UNSAFE_ROUTINE = 1418 BINLOG_CREATE_ROUTINE_NEED_SUPER = 1419 -EXEC_STMT_WITH_OPEN_CURSOR = 1420 STMT_HAS_NO_OPEN_CURSOR = 1421 COMMIT_NOT_ALLOWED_IN_SF_OR_TRG = 1422 NO_DEFAULT_FOR_VIEW_FIELD = 1423 @@ -474,7 +424,6 @@ FOREIGN_DATA_SOURCE_DOESNT_EXIST = 1431 FOREIGN_DATA_STRING_INVALID_CANT_CREATE = 1432 FOREIGN_DATA_STRING_INVALID = 1433 -CANT_CREATE_FEDERATED_TABLE = 1434 TRG_IN_WRONG_SCHEMA = 1435 STACK_OVERRUN_NEED_MORE = 1436 TOO_LONG_BODY = 1437 @@ -486,7 +435,6 @@ VIEW_PREVENT_UPDATE = 1443 PS_NO_RECURSION = 1444 SP_CANT_SET_AUTOCOMMIT = 1445 -MALFORMED_DEFINER = 1446 VIEW_FRM_NO_USER = 1447 VIEW_OTHER_USER = 1448 NO_SUCH_USER = 1449 @@ -497,7 +445,6 @@ TRG_NO_DEFINER = 1454 OLD_FILE_FORMAT = 1455 SP_RECURSION_LIMIT = 1456 -SP_PROC_TABLE_CORRUPT = 1457 SP_WRONG_NAME = 1458 TABLE_NEEDS_UPGRADE = 1459 SP_NO_AGGREGATE = 1460 @@ -522,14 +469,10 @@ PARTITION_REQUIRES_VALUES_ERROR = 1479 PARTITION_WRONG_VALUES_ERROR = 1480 PARTITION_MAXVALUE_ERROR = 1481 -PARTITION_SUBPARTITION_ERROR = 1482 -PARTITION_SUBPART_MIX_ERROR = 1483 PARTITION_WRONG_NO_PART_ERROR = 1484 PARTITION_WRONG_NO_SUBPART_ERROR = 1485 WRONG_EXPR_IN_PARTITION_FUNC_ERROR = 1486 -NO_CONST_EXPR_IN_RANGE_OR_LIST_ERROR = 1487 FIELD_NOT_FOUND_PART_ERROR = 1488 -LIST_OF_FIELDS_ONLY_IN_HASH_ERROR = 1489 INCONSISTENT_PARTITION_INFO_ERROR = 1490 PARTITION_FUNC_NOT_ALLOWED_ERROR = 1491 PARTITIONS_MUST_BE_DEFINED_ERROR = 1492 @@ -562,7 +505,6 @@ CONSECUTIVE_REORG_PARTITIONS = 1519 REORG_OUTSIDE_RANGE = 1520 PARTITION_FUNCTION_FAILURE = 1521 -PART_STATE_ERROR = 1522 LIMITED_PART_RANGE = 1523 PLUGIN_IS_NOT_LOADED = 1524 WRONG_VALUE = 1525 @@ -575,53 +517,30 @@ SIZE_OVERFLOW_ERROR = 1532 ALTER_FILEGROUP_FAILED = 1533 BINLOG_ROW_LOGGING_FAILED = 1534 -BINLOG_ROW_WRONG_TABLE_DEF = 1535 -BINLOG_ROW_RBR_TO_SBR = 1536 EVENT_ALREADY_EXISTS = 1537 -EVENT_STORE_FAILED = 1538 EVENT_DOES_NOT_EXIST = 1539 -EVENT_CANT_ALTER = 1540 -EVENT_DROP_FAILED = 1541 EVENT_INTERVAL_NOT_POSITIVE_OR_TOO_BIG = 1542 EVENT_ENDS_BEFORE_STARTS = 1543 EVENT_EXEC_TIME_IN_THE_PAST = 1544 -EVENT_OPEN_TABLE_FAILED = 1545 -EVENT_NEITHER_M_EXPR_NOR_M_AT = 1546 -COL_COUNT_DOESNT_MATCH_CORRUPTED = 1547 -OBSOLETE_COL_COUNT_DOESNT_MATCH_CORRUPTED = 1547 -CANNOT_LOAD_FROM_TABLE = 1548 -OBSOLETE_CANNOT_LOAD_FROM_TABLE = 1548 -EVENT_CANNOT_DELETE = 1549 -EVENT_COMPILE_ERROR = 1550 EVENT_SAME_NAME = 1551 -EVENT_DATA_TOO_LONG = 1552 DROP_INDEX_FK = 1553 WARN_DEPRECATED_SYNTAX_WITH_VER = 1554 -CANT_WRITE_LOCK_LOG_TABLE = 1555 CANT_LOCK_LOG_TABLE = 1556 -FOREIGN_DUPLICATE_KEY = 1557 FOREIGN_DUPLICATE_KEY_OLD_UNUSED = 1557 COL_COUNT_DOESNT_MATCH_PLEASE_UPDATE = 1558 TEMP_TABLE_PREVENTS_SWITCH_OUT_OF_RBR = 1559 STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_FORMAT = 1560 -NDB_CANT_SWITCH_BINLOG_FORMAT = 1561 PARTITION_NO_TEMPORARY = 1562 PARTITION_CONST_DOMAIN_ERROR = 1563 PARTITION_FUNCTION_IS_NOT_ALLOWED = 1564 -DDL_LOG_ERROR = 1565 NULL_IN_VALUES_LESS_THAN = 1566 WRONG_PARTITION_NAME = 1567 CANT_CHANGE_TX_CHARACTERISTICS = 1568 -CANT_CHANGE_TX_ISOLATION = 1568 DUP_ENTRY_AUTOINCREMENT_CASE = 1569 -EVENT_MODIFY_QUEUE_ERROR = 1570 EVENT_SET_VAR_ERROR = 1571 PARTITION_MERGE_ERROR = 1572 -CANT_ACTIVATE_LOG = 1573 -RBR_NOT_AVAILABLE = 1574 BASE64_DECODE_ERROR = 1575 EVENT_RECURSION_FORBIDDEN = 1576 -EVENTS_DB_ERROR = 1577 ONLY_INTEGERS_ALLOWED = 1578 UNSUPORTED_LOG_ENGINE = 1579 BAD_LOG_STATEMENT = 1580 @@ -634,41 +553,29 @@ BINLOG_PURGE_EMFILE = 1587 EVENT_CANNOT_CREATE_IN_THE_PAST = 1588 EVENT_CANNOT_ALTER_IN_THE_PAST = 1589 -SLAVE_INCIDENT = 1590 NO_PARTITION_FOR_GIVEN_VALUE_SILENT = 1591 BINLOG_UNSAFE_STATEMENT = 1592 -SLAVE_FATAL_ERROR = 1593 -SLAVE_RELAY_LOG_READ_FAILURE = 1594 -SLAVE_RELAY_LOG_WRITE_FAILURE = 1595 -SLAVE_CREATE_EVENT_FAILURE = 1596 -SLAVE_MASTER_COM_FAILURE = 1597 +BINLOG_FATAL_ERROR = 1593 BINLOG_LOGGING_IMPOSSIBLE = 1598 VIEW_NO_CREATION_CTX = 1599 VIEW_INVALID_CREATION_CTX = 1600 -SR_INVALID_CREATION_CTX = 1601 TRG_CORRUPTED_FILE = 1602 TRG_NO_CREATION_CTX = 1603 TRG_INVALID_CREATION_CTX = 1604 EVENT_INVALID_CREATION_CTX = 1605 TRG_CANT_OPEN_TABLE = 1606 -CANT_CREATE_SROUTINE = 1607 -NEVER_USED = 1608 NO_FORMAT_DESCRIPTION_EVENT_BEFORE_BINLOG_STATEMENT = 1609 SLAVE_CORRUPT_EVENT = 1610 -LOAD_DATA_INVALID_COLUMN = 1611 LOG_PURGE_NO_FILE = 1612 XA_RBTIMEOUT = 1613 XA_RBDEADLOCK = 1614 NEED_REPREPARE = 1615 -DELAYED_NOT_SUPPORTED = 1616 WARN_NO_MASTER_INFO = 1617 WARN_OPTION_IGNORED = 1618 PLUGIN_DELETE_BUILTIN = 1619 -WARN_PLUGIN_DELETE_BUILTIN = 1619 WARN_PLUGIN_BUSY = 1620 VARIABLE_IS_READONLY = 1621 WARN_ENGINE_TRANSACTION_ROLLBACK = 1622 -SLAVE_HEARTBEAT_FAILURE = 1623 SLAVE_HEARTBEAT_VALUE_OUT_OF_RANGE = 1624 NDB_REPLICATION_SCHEMA_ERROR = 1625 CONFLICT_FN_PARSE_ERROR = 1626 @@ -696,7 +603,6 @@ COND_ITEM_TOO_LONG = 1648 UNKNOWN_LOCALE = 1649 SLAVE_IGNORE_SERVER_IDS = 1650 -QUERY_CACHE_DISABLED = 1651 SAME_NAME_PARTITION_FIELD = 1652 PARTITION_COLUMN_LIST_ERROR = 1653 WRONG_TYPE_COLUMN_VALUE_ERROR = 1654 @@ -714,8 +620,6 @@ BINLOG_ROW_INJECTION_AND_STMT_MODE = 1666 BINLOG_MULTIPLE_ENGINES_AND_SELF_LOGGING_ENGINE = 1667 BINLOG_UNSAFE_LIMIT = 1668 -BINLOG_UNSAFE_INSERT_DELAYED = 1669 -UNUSED4 = 1669 BINLOG_UNSAFE_SYSTEM_TABLE = 1670 BINLOG_UNSAFE_AUTOINC_COLUMNS = 1671 BINLOG_UNSAFE_UDF = 1672 @@ -723,7 +627,6 @@ BINLOG_UNSAFE_SYSTEM_FUNCTION = 1674 BINLOG_UNSAFE_NONTRANS_AFTER_TRANS = 1675 MESSAGE_AND_STATEMENT = 1676 -SLAVE_CONVERSION_FAILED = 1677 SLAVE_CANT_CREATE_CONVERSION = 1678 INSIDE_TRANSACTION_PREVENTS_SWITCH_BINLOG_FORMAT = 1679 PATH_LENGTH = 1680 @@ -746,7 +649,6 @@ VALUES_IS_NOT_INT_TYPE_ERROR = 1697 ACCESS_DENIED_NO_PASSWORD_ERROR = 1698 SET_PASSWORD_AUTH_PLUGIN = 1699 -GRANT_PLUGIN_USER_EXISTS = 1700 TRUNCATE_ILLEGAL_FK = 1701 PLUGIN_IS_PERMANENT = 1702 SLAVE_HEARTBEAT_VALUE_OUT_OF_RANGE_MIN = 1703 @@ -773,10 +675,8 @@ BINLOG_UNSAFE_INSERT_TWO_KEYS = 1724 TABLE_IN_FK_CHECK = 1725 UNSUPPORTED_ENGINE = 1726 -UNUSED_1 = 1726 BINLOG_UNSAFE_AUTOINC_NOT_FIRST = 1727 CANNOT_LOAD_FROM_TABLE_V2 = 1728 -LAST_MYSQL_ERROR_MESSAGE = 1728 MASTER_DELAY_VALUE_OUT_OF_RANGE = 1729 ONLY_FD_AND_RBR_EVENTS_ALLOWED_IN_BINLOG_STATEMENT = 1730 PARTITION_EXCHANGE_DIFFERENT_OPTION = 1731 @@ -789,15 +689,11 @@ BINLOG_CACHE_SIZE_GREATER_THAN_MAX = 1738 WARN_INDEX_NOT_APPLICABLE = 1739 PARTITION_EXCHANGE_FOREIGN_KEY = 1740 -NO_SUCH_KEY_VALUE = 1741 RPL_INFO_DATA_TOO_LONG = 1742 -NETWORK_READ_EVENT_CHECKSUM_FAILURE = 1743 -BINLOG_READ_EVENT_CHECKSUM_FAILURE = 1744 BINLOG_STMT_CACHE_SIZE_GREATER_THAN_MAX = 1745 CANT_UPDATE_TABLE_IN_CREATE_TABLE_SELECT = 1746 PARTITION_CLAUSE_ON_NONPARTITIONED = 1747 ROW_DOES_NOT_MATCH_GIVEN_PARTITION_SET = 1748 -NO_SUCH_PARTITION__UNUSED = 1749 CHANGE_RPL_INFO_REPOSITORY_FAILURE = 1750 WARNING_NOT_COMPLETE_ROLLBACK_WITH_CREATED_TEMP_TABLE = 1751 WARNING_NOT_COMPLETE_ROLLBACK_WITH_DROPPED_TEMP_TABLE = 1752 @@ -815,24 +711,19 @@ TABLE_HAS_NO_FT = 1764 VARIABLE_NOT_SETTABLE_IN_SF_OR_TRIGGER = 1765 VARIABLE_NOT_SETTABLE_IN_TRANSACTION = 1766 -GTID_NEXT_IS_NOT_IN_GTID_NEXT_LIST = 1767 -CANT_CHANGE_GTID_NEXT_IN_TRANSACTION_WHEN_GTID_NEXT_LIST_IS_NULL = 1768 SET_STATEMENT_CANNOT_INVOKE_FUNCTION = 1769 GTID_NEXT_CANT_BE_AUTOMATIC_IF_GTID_NEXT_LIST_IS_NON_NULL = 1770 -SKIPPING_LOGGED_TRANSACTION = 1771 MALFORMED_GTID_SET_SPECIFICATION = 1772 MALFORMED_GTID_SET_ENCODING = 1773 MALFORMED_GTID_SPECIFICATION = 1774 GNO_EXHAUSTED = 1775 BAD_SLAVE_AUTO_POSITION = 1776 -AUTO_POSITION_REQUIRES_GTID_MODE_ON = 1777 +AUTO_POSITION_REQUIRES_GTID_MODE_NOT_OFF = 1777 CANT_DO_IMPLICIT_COMMIT_IN_TRX_WHEN_GTID_NEXT_IS_SET = 1778 -GTID_MODE_2_OR_3_REQUIRES_ENFORCE_GTID_CONSISTENCY_ON = 1779 -GTID_MODE_REQUIRES_BINLOG = 1780 +GTID_MODE_ON_REQUIRES_ENFORCE_GTID_CONSISTENCY_ON = 1779 CANT_SET_GTID_NEXT_TO_GTID_WHEN_GTID_MODE_IS_OFF = 1781 CANT_SET_GTID_NEXT_TO_ANONYMOUS_WHEN_GTID_MODE_IS_ON = 1782 CANT_SET_GTID_NEXT_LIST_TO_NON_NULL_WHEN_GTID_MODE_IS_OFF = 1783 -FOUND_GTID_EVENT_WHEN_GTID_MODE_IS_OFF = 1784 GTID_UNSAFE_NON_TRANSACTIONAL_TABLE = 1785 GTID_UNSAFE_CREATE_SELECT = 1786 GTID_UNSAFE_CREATE_DROP_TEMPORARY_TABLE_IN_TRANSACTION = 1787 @@ -882,12 +773,10 @@ DUP_INDEX = 1831 FK_COLUMN_CANNOT_CHANGE = 1832 FK_COLUMN_CANNOT_CHANGE_CHILD = 1833 -UNUSED5 = 1834 MALFORMED_PACKET = 1835 READ_ONLY_MODE = 1836 -GTID_NEXT_TYPE_UNDEFINED_GROUP = 1837 +GTID_NEXT_TYPE_UNDEFINED_GTID = 1837 VARIABLE_NOT_SETTABLE_IN_SP = 1838 -CANT_SET_GTID_PURGED_WHEN_GTID_MODE_IS_OFF = 1839 CANT_SET_GTID_PURGED_WHEN_GTID_EXECUTED_IS_NOT_EMPTY = 1840 CANT_SET_GTID_PURGED_WHEN_OWNED_GTIDS_IS_NOT_EMPTY = 1841 GTID_PURGED_WAS_CHANGED = 1842 @@ -900,7 +789,6 @@ ALTER_OPERATION_NOT_SUPPORTED_REASON_FK_RENAME = 1849 ALTER_OPERATION_NOT_SUPPORTED_REASON_COLUMN_TYPE = 1850 ALTER_OPERATION_NOT_SUPPORTED_REASON_FK_CHECK = 1851 -UNUSED6 = 1852 ALTER_OPERATION_NOT_SUPPORTED_REASON_NOPK = 1853 ALTER_OPERATION_NOT_SUPPORTED_REASON_AUTOINC = 1854 ALTER_OPERATION_NOT_SUPPORTED_REASON_HIDDEN_FTS = 1855 @@ -913,7 +801,6 @@ MUST_CHANGE_PASSWORD_LOGIN = 1862 ROW_IN_WRONG_PARTITION = 1863 MTS_EVENT_BIGGER_PENDING_JOBS_SIZE_MAX = 1864 -INNODB_NO_FT_USES_PARSER = 1865 BINLOG_LOGICAL_CORRUPTION = 1866 WARN_PURGE_LOG_IN_USE = 1867 WARN_PURGE_LOG_IS_ACTIVE = 1868 @@ -932,129 +819,7 @@ INNODB_FORCED_RECOVERY = 1881 AES_INVALID_IV = 1882 PLUGIN_CANNOT_BE_UNINSTALLED = 1883 -GTID_UNSAFE_BINLOG_SPLITTABLE_STATEMENT_AND_GTID_GROUP = 1884 -FILE_CORRUPT = 1885 -ERROR_ON_MASTER = 1886 -INCONSISTENT_ERROR = 1887 -STORAGE_ENGINE_NOT_LOADED = 1888 -GET_STACKED_DA_WITHOUT_ACTIVE_HANDLER = 1889 -WARN_LEGACY_SYNTAX_CONVERTED = 1890 -BINLOG_UNSAFE_FULLTEXT_PLUGIN = 1891 -CANNOT_DISCARD_TEMPORARY_TABLE = 1892 -FK_DEPTH_EXCEEDED = 1893 -COL_COUNT_DOESNT_MATCH_PLEASE_UPDATE_V2 = 1894 -WARN_TRIGGER_DOESNT_HAVE_CREATED = 1895 -REFERENCED_TRG_DOES_NOT_EXIST = 1896 -EXPLAIN_NOT_SUPPORTED = 1897 -INVALID_FIELD_SIZE = 1898 -MISSING_HA_CREATE_OPTION = 1899 -ENGINE_OUT_OF_MEMORY = 1900 -VCOL_BASED_ON_VCOL = 1900 -PASSWORD_EXPIRE_ANONYMOUS_USER = 1901 -VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED = 1901 -DATA_CONVERSION_ERROR_FOR_VIRTUAL_COLUMN = 1902 -SLAVE_SQL_THREAD_MUST_STOP = 1902 -NO_FT_MATERIALIZED_SUBQUERY = 1903 -PRIMARY_KEY_BASED_ON_VIRTUAL_COLUMN = 1903 -INNODB_UNDO_LOG_FULL = 1904 -KEY_BASED_ON_GENERATED_VIRTUAL_COLUMN = 1904 -INVALID_ARGUMENT_FOR_LOGARITHM = 1905 -WRONG_FK_OPTION_FOR_VIRTUAL_COLUMN = 1905 -SLAVE_CHANNEL_IO_THREAD_MUST_STOP = 1906 -WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN = 1906 -UNSUPPORTED_ACTION_ON_VIRTUAL_COLUMN = 1907 -WARN_OPEN_TEMP_TABLES_MUST_BE_ZERO = 1907 -CONST_EXPR_IN_VCOL = 1908 -WARN_ONLY_MASTER_LOG_FILE_NO_POS = 1908 -QUERY_TIMEOUT = 1909 -ROW_EXPR_FOR_VCOL = 1909 -NON_RO_SELECT_DISABLE_TIMER = 1910 -UNSUPPORTED_ENGINE_FOR_VIRTUAL_COLUMNS = 1910 -DUP_LIST_ENTRY = 1911 -UNKNOWN_OPTION = 1911 -BAD_OPTION_VALUE = 1912 -SQL_MODE_NO_EFFECT = 1912 -AGGREGATE_ORDER_FOR_UNION = 1913 -NETWORK_READ_EVENT_CHECKSUM_FAILURE = 1913 -AGGREGATE_ORDER_NON_AGG_QUERY = 1914 -BINLOG_READ_EVENT_CHECKSUM_FAILURE = 1914 -CANT_DO_ONLINE = 1915 -SLAVE_WORKER_STOPPED_PREVIOUS_THD_ERROR = 1915 -DATA_OVERFLOW = 1916 -DONT_SUPPORT_SLAVE_PRESERVE_COMMIT_ORDER = 1916 -DATA_TRUNCATED = 1917 -SERVER_OFFLINE_MODE = 1917 -BAD_DATA = 1918 -GIS_DIFFERENT_SRIDS = 1918 -DYN_COL_WRONG_FORMAT = 1919 -GIS_UNSUPPORTED_ARGUMENT = 1919 -DYN_COL_IMPLEMENTATION_LIMIT = 1920 -GIS_UNKNOWN_ERROR = 1920 -DYN_COL_DATA = 1921 -GIS_UNKNOWN_EXCEPTION = 1921 -DYN_COL_WRONG_CHARSET = 1922 -GIS_INVALID_DATA = 1922 -BOOST_GEOMETRY_EMPTY_INPUT_EXCEPTION = 1923 -ILLEGAL_SUBQUERY_OPTIMIZER_SWITCHES = 1923 -BOOST_GEOMETRY_CENTROID_EXCEPTION = 1924 -QUERY_CACHE_IS_DISABLED = 1924 -BOOST_GEOMETRY_OVERLAY_INVALID_INPUT_EXCEPTION = 1925 -QUERY_CACHE_IS_GLOBALY_DISABLED = 1925 -BOOST_GEOMETRY_TURN_INFO_EXCEPTION = 1926 -VIEW_ORDERBY_IGNORED = 1926 -BOOST_GEOMETRY_SELF_INTERSECTION_POINT_EXCEPTION = 1927 -CONNECTION_KILLED = 1927 -BOOST_GEOMETRY_UNKNOWN_EXCEPTION = 1928 -INTERNAL_ERROR = 1928 -INSIDE_TRANSACTION_PREVENTS_SWITCH_SKIP_REPLICATION = 1929 -STD_BAD_ALLOC_ERROR = 1929 -STD_DOMAIN_ERROR = 1930 -STORED_FUNCTION_PREVENTS_SWITCH_SKIP_REPLICATION = 1930 -QUERY_EXCEEDED_ROWS_EXAMINED_LIMIT = 1931 -STD_LENGTH_ERROR = 1931 -NO_SUCH_TABLE_IN_ENGINE = 1932 -STD_INVALID_ARGUMENT = 1932 -GEOMETRY_SRID_MISMATCH = 1933 -STD_OUT_OF_RANGE_ERROR = 1933 -NO_SUCH_SPATIAL_REF_ID = 1934 -STD_OVERFLOW_ERROR = 1934 -STD_RANGE_ERROR = 1935 -STD_UNDERFLOW_ERROR = 1936 -STD_LOGIC_ERROR = 1937 -STD_RUNTIME_ERROR = 1938 -STD_UNKNOWN_EXCEPTION = 1939 -GIS_DATA_WRONG_ENDIANESS = 1940 -CHANGE_MASTER_PASSWORD_LENGTH = 1941 -USER_LOCK_WRONG_NAME = 1942 -USER_LOCK_DEADLOCK = 1943 -REPLACE_INACCESSIBLE_ROWS = 1944 -ALTER_OPERATION_NOT_SUPPORTED_REASON_GIS = 1945 -ILLEGAL_USER_VAR = 1946 -GTID_MODE_OFF = 1947 -UNSUPPORTED_BY_REPLICATION_THREAD = 1948 -INCORRECT_TYPE = 1949 -FIELD_IN_ORDER_NOT_SELECT = 1950 -AGGREGATE_IN_ORDER_NOT_SELECT = 1951 -INVALID_RPL_WILD_TABLE_FILTER_PATTERN = 1952 -NET_OK_PACKET_TOO_LARGE = 1953 -INVALID_JSON_DATA = 1954 -INVALID_GEOJSON_MISSING_MEMBER = 1955 -INVALID_GEOJSON_WRONG_TYPE = 1956 -INVALID_GEOJSON_UNSPECIFIED = 1957 -DIMENSION_UNSUPPORTED = 1958 -SLAVE_CHANNEL_DOES_NOT_EXIST = 1959 -SLAVE_MULTIPLE_CHANNELS_HOST_PORT = 1960 -SLAVE_CHANNEL_NAME_INVALID_OR_TOO_LONG = 1961 -SLAVE_NEW_CHANNEL_WRONG_REPOSITORY = 1962 -SLAVE_CHANNEL_DELETE = 1963 -SLAVE_MULTIPLE_CHANNELS_CMD = 1964 -SLAVE_MAX_CHANNELS_EXCEEDED = 1965 -SLAVE_CHANNEL_MUST_STOP = 1966 -SLAVE_CHANNEL_NOT_RUNNING = 1967 -SLAVE_CHANNEL_WAS_RUNNING = 1968 -SLAVE_CHANNEL_WAS_NOT_RUNNING = 1969 -SLAVE_CHANNEL_SQL_THREAD_MUST_STOP = 1970 -SLAVE_CHANNEL_SQL_SKIP_COUNTER = 1971 -WRONG_FIELD_WITH_GROUP_V2 = 1972 -MIX_OF_GROUP_FUNC_AND_FIELDS_V2 = 1973 +GTID_UNSAFE_BINLOG_SPLITTABLE_STATEMENT_AND_ASSIGNED_GTID = 1884 +SLAVE_HAS_MORE_GTIDS_THAN_MASTER = 1885 +MISSING_KEY = 1886 ERROR_LAST = 1973 From aca3a396c766dfc36c83ae8cbf955c08bd4c1286 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 12 Dec 2018 17:51:48 +0900 Subject: [PATCH 107/177] Remove old TIMESTAMP format for MySQL<4.1 (#308) --- MySQLdb/converters.py | 2 +- MySQLdb/times.py | 14 -------------- tests/test_MySQLdb_times.py | 12 ------------ 3 files changed, 1 insertion(+), 27 deletions(-) diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index 7b1eb035..f0d01786 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -117,7 +117,7 @@ def quote_tuple(t, d): FIELD_TYPE.LONGLONG: int, FIELD_TYPE.INT24: int, FIELD_TYPE.YEAR: int, - FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter, + FIELD_TYPE.TIMESTAMP: DateTime_or_None, FIELD_TYPE.DATETIME: DateTime_or_None, FIELD_TYPE.TIME: TimeDelta_or_None, FIELD_TYPE.DATE: Date_or_None, diff --git a/MySQLdb/times.py b/MySQLdb/times.py index a7eaa53b..d47c8fb8 100644 --- a/MySQLdb/times.py +++ b/MySQLdb/times.py @@ -129,17 +129,3 @@ def DateTime2literal(d, c): def DateTimeDelta2literal(d, c): """Format a DateTimeDelta object as a time.""" return string_literal(format_TIMEDELTA(d)) - -def mysql_timestamp_converter(s): - """Convert a MySQL TIMESTAMP to a Timestamp object.""" - # MySQL>4.1 returns TIMESTAMP in the same format as DATETIME - if s[4] == '-': return DateTime_or_None(s) - s = s + "0"*(14-len(s)) # padding - parts = map(int, filter(None, (s[:4],s[4:6],s[6:8], - s[8:10],s[10:12],s[12:14]))) - try: - return Timestamp(*parts) - except (SystemExit, KeyboardInterrupt): - raise # pragma: no cover - except: - return None diff --git a/tests/test_MySQLdb_times.py b/tests/test_MySQLdb_times.py index 8000f645..d9d3e027 100644 --- a/tests/test_MySQLdb_times.py +++ b/tests/test_MySQLdb_times.py @@ -81,18 +81,6 @@ def test_timestamp_from_ticks(self, mock): assert times.TimestampFromTicks(1430000000.123) == datetime(2015, 4, 25, 22, 13, 20) -class TestTimestampConverter(unittest.TestCase): - def test_mysql_timestamp_converter(self): - assert times.mysql_timestamp_converter('2015-12-13') == date(2015, 12, 13) - assert times.mysql_timestamp_converter('2038-01-19 03:14:07') == datetime(2038, 1, 19, 3, 14, 7) - - assert times.mysql_timestamp_converter('2015121310') == datetime(2015, 12, 13, 10, 0) - assert times.mysql_timestamp_converter('20151213101112') == datetime(2015, 12, 13, 10, 11, 12) - - assert times.mysql_timestamp_converter('20151313') is None - assert times.mysql_timestamp_converter('2015-13-13') is None - - class TestToLiteral(unittest.TestCase): def test_datetime_to_literal(self): assert times.DateTime2literal(datetime(2015, 12, 13), '') == b"'2015-12-13 00:00:00'" From bf77dd06338592832e70812ec813382f805ef167 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 12 Dec 2018 20:25:12 +0900 Subject: [PATCH 108/177] Update FIELD_TYPEs (#309) --- MySQLdb/__init__.py | 3 +-- MySQLdb/constants/FIELD_TYPE.py | 6 +++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/MySQLdb/__init__.py b/MySQLdb/__init__.py index fa52a83d..92b5d264 100644 --- a/MySQLdb/__init__.py +++ b/MySQLdb/__init__.py @@ -37,7 +37,6 @@ from sets import ImmutableSet as frozenset class DBAPISet(frozenset): - """A special type of set for which A == x is true if A is a DBAPISet and x is a member of that set.""" @@ -54,7 +53,7 @@ def __eq__(self, other): NUMBER = DBAPISet([FIELD_TYPE.DECIMAL, FIELD_TYPE.DOUBLE, FIELD_TYPE.FLOAT, FIELD_TYPE.INT24, FIELD_TYPE.LONG, FIELD_TYPE.LONGLONG, FIELD_TYPE.TINY, FIELD_TYPE.YEAR, FIELD_TYPE.NEWDECIMAL]) -DATE = DBAPISet([FIELD_TYPE.DATE, FIELD_TYPE.NEWDATE]) +DATE = DBAPISet([FIELD_TYPE.DATE]) TIME = DBAPISet([FIELD_TYPE.TIME]) TIMESTAMP = DBAPISet([FIELD_TYPE.TIMESTAMP, FIELD_TYPE.DATETIME]) DATETIME = TIMESTAMP diff --git a/MySQLdb/constants/FIELD_TYPE.py b/MySQLdb/constants/FIELD_TYPE.py index 2a307644..3c4eca9f 100644 --- a/MySQLdb/constants/FIELD_TYPE.py +++ b/MySQLdb/constants/FIELD_TYPE.py @@ -18,9 +18,13 @@ TIME = 11 DATETIME = 12 YEAR = 13 -NEWDATE = 14 +# NEWDATE = 14 # Internal to MySQL. VARCHAR = 15 BIT = 16 +# TIMESTAMP2 = 17 +# DATETIME2 = 18 +# TIME2 = 19 +JSON = 245 NEWDECIMAL = 246 ENUM = 247 SET = 248 From fe8032430c3642078fefc4cb7661cc192cfe4ea6 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 13 Dec 2018 12:41:53 +0900 Subject: [PATCH 109/177] Reimplement JSON support (#310) --- MySQLdb/_mysql.c | 19 ++++--------------- MySQLdb/connections.py | 2 +- MySQLdb/converters.py | 1 + 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 53e57f85..84714170 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -1138,7 +1138,7 @@ _mysql_field_to_python( v = PyUnicode_Decode(rowitem, length, encoding, NULL); } } - else if (converter == (PyObject*)&PyBytes_Type) { + else if (converter == (PyObject*)&PyBytes_Type || converter == Py_None) { //fprintf(stderr, "decoding with bytes\n", encoding); v = PyBytes_FromStringAndSize(rowitem, length); } @@ -1146,7 +1146,7 @@ _mysql_field_to_python( //fprintf(stderr, "decoding with int\n", encoding); v = PyInt_FromString(rowitem, NULL, 10); } - else if (converter != Py_None) { + else { //fprintf(stderr, "decoding with callback\n"); //PyObject_Print(converter, stderr, 0); //fprintf(stderr, "\n"); @@ -1158,17 +1158,7 @@ _mysql_field_to_python( #endif rowitem, (int)length); - } else { - //fprintf(stderr, "converter=None\n"); -#ifdef IS_PY3K - if (!binary) { - v = PyUnicode_FromStringAndSize(rowitem, (int)length); - } else -#endif - v = PyBytes_FromStringAndSize(rowitem, (int)length); } - if (!v) - return NULL; } else { Py_INCREF(Py_None); v = Py_None; @@ -1414,7 +1404,7 @@ _mysql_ConnectionObject_change_user( { char *user, *pwd=NULL, *db=NULL; int r; - static char *kwlist[] = { "user", "passwd", "db", NULL } ; + static char *kwlist[] = { "user", "passwd", "db", NULL } ; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ss:change_user", kwlist, &user, &pwd, &db)) @@ -1424,8 +1414,7 @@ _mysql_ConnectionObject_change_user( r = mysql_change_user(&(self->connection), user, pwd, db); Py_END_ALLOW_THREADS if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static char _mysql_ConnectionObject_character_set_name__doc__[] = diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index e902912f..29cc4f32 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -183,7 +183,7 @@ def unicode_literal(u, dummy=None): if use_unicode: for t in (FIELD_TYPE.STRING, FIELD_TYPE.VAR_STRING, FIELD_TYPE.VARCHAR, FIELD_TYPE.TINY_BLOB, - FIELD_TYPE.MEDIUM_BLOB, FIELD_TYPE.LONG_BLOB, FIELD_TYPE.BLOB): + FIELD_TYPE.MEDIUM_BLOB, FIELD_TYPE.LONG_BLOB, FIELD_TYPE.BLOB, FIELD_TYPE.JSON): self.converter[t] = _bytes_or_str self.encoders[unicode] = unicode_literal diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index f0d01786..9b9cb8be 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -129,4 +129,5 @@ def quote_tuple(t, d): FIELD_TYPE.STRING: bytes, FIELD_TYPE.VAR_STRING: bytes, FIELD_TYPE.VARCHAR: bytes, + FIELD_TYPE.JSON: bytes, } From aca63e5d1c749881c27b5d3ea478d3150e619fbe Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 13 Dec 2018 12:44:25 +0900 Subject: [PATCH 110/177] Use Py_RETURN_NONE --- MySQLdb/_mysql.c | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 84714170..08ea9ae0 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -664,8 +664,7 @@ _mysql_debug( char *debug; if (!PyArg_ParseTuple(args, "s", &debug)) return NULL; mysql_debug(debug); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static char _mysql_ConnectionObject_dump_debug_info__doc__[] = @@ -685,8 +684,7 @@ _mysql_ConnectionObject_dump_debug_info( err = mysql_dump_debug_info(&(self->connection)); Py_END_ALLOW_THREADS if (err) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static char _mysql_ConnectionObject_autocommit__doc__[] = @@ -704,8 +702,7 @@ _mysql_ConnectionObject_autocommit( err = mysql_autocommit(&(self->connection), flag); Py_END_ALLOW_THREADS if (err) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static char _mysql_ConnectionObject_get_autocommit__doc__[] = @@ -737,8 +734,7 @@ _mysql_ConnectionObject_commit( err = mysql_commit(&(self->connection)); Py_END_ALLOW_THREADS if (err) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static char _mysql_ConnectionObject_rollback__doc__[] = @@ -1591,8 +1587,7 @@ _mysql_ConnectionObject_info( check_connection(self); s = mysql_info(&(self->connection)); if (s) return PyString_FromString(s); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static char _mysql_ConnectionObject_insert_id__doc__[] = @@ -1646,8 +1641,7 @@ _mysql_ConnectionObject_kill( r = mysql_kill(&(self->connection), pid); Py_END_ALLOW_THREADS if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static char _mysql_ConnectionObject_field_count__doc__[] = @@ -1741,8 +1735,7 @@ _mysql_ConnectionObject_ping( r = mysql_ping(&(self->connection)); Py_END_ALLOW_THREADS if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static char _mysql_ConnectionObject_query__doc__[] = @@ -1765,8 +1758,7 @@ _mysql_ConnectionObject_query( r = mysql_real_query(&(self->connection), query, len); Py_END_ALLOW_THREADS if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } @@ -1789,8 +1781,7 @@ _mysql_ConnectionObject_send_query( r = mysql_send_query(mysql, query, len); Py_END_ALLOW_THREADS if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } @@ -1810,8 +1801,7 @@ _mysql_ConnectionObject_read_query_result( r = (int)mysql_read_query_result(mysql); Py_END_ALLOW_THREADS if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static char _mysql_ConnectionObject_select_db__doc__[] = @@ -1839,8 +1829,7 @@ _mysql_ConnectionObject_select_db( r = mysql_select_db(&(self->connection), db); Py_END_ALLOW_THREADS if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static char _mysql_ConnectionObject_shutdown__doc__[] = @@ -1859,8 +1848,7 @@ _mysql_ConnectionObject_shutdown( r = mysql_shutdown(&(self->connection), SHUTDOWN_DEFAULT); Py_END_ALLOW_THREADS if (r) return _mysql_Exception(self); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static char _mysql_ConnectionObject_stat__doc__[] = @@ -2021,8 +2009,7 @@ _mysql_ResultObject_data_seek( if (!PyArg_ParseTuple(args, "i:data_seek", &row)) return NULL; check_result_connection(self); mysql_data_seek(self->result, row); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static void From 4b40c8ce056a9633c3ca2738ce59b821d918ac26 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 13 Dec 2018 16:10:58 +0900 Subject: [PATCH 111/177] Remove _mysql.thread_safe() (#311) --- MySQLdb/_mysql.c | 16 ---------------- doc/user_guide.rst | 1 - tests/test_MySQLdb_nonstandard.py | 3 --- 3 files changed, 20 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 08ea9ae0..e90d45eb 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -225,16 +225,6 @@ _get_encoding(MYSQL *mysql) return cs.csname; } -static char _mysql_thread_safe__doc__[] = -"Indicates whether the client is compiled as thread-safe."; - -static PyObject *_mysql_thread_safe( - PyObject *self, - PyObject *noargs) -{ - return PyInt_FromLong((long)mysql_thread_safe()); -} - static char _mysql_ResultObject__doc__[] = "result(connection, use=0, converter={}) -- Result set from a query.\n\ \n\ @@ -2585,12 +2575,6 @@ _mysql_methods[] = { METH_NOARGS, _mysql_get_client_info__doc__ }, - { - "thread_safe", - (PyCFunction)_mysql_thread_safe, - METH_NOARGS, - _mysql_thread_safe__doc__ - }, {NULL, NULL} /* sentinel */ }; diff --git a/doc/user_guide.rst b/doc/user_guide.rst index c83c3f3f..173918dd 100644 --- a/doc/user_guide.rst +++ b/doc/user_guide.rst @@ -89,7 +89,6 @@ MySQL C API function mapping ``mysql_stat()`` ``conn.stat()`` ``mysql_store_result()`` ``conn.store_result()`` ``mysql_thread_id()`` ``conn.thread_id()`` - ``mysql_thread_safe_client()`` ``conn.thread_safe_client()`` ``mysql_use_result()`` ``conn.use_result()`` ``mysql_warning_count()`` ``conn.warning_count()`` ``CLIENT_*`` ``MySQLdb.constants.CLIENT.*`` diff --git a/tests/test_MySQLdb_nonstandard.py b/tests/test_MySQLdb_nonstandard.py index 55cfc630..2b33c398 100644 --- a/tests/test_MySQLdb_nonstandard.py +++ b/tests/test_MySQLdb_nonstandard.py @@ -35,9 +35,6 @@ def test_version(self): def test_client_info(self): self.assertTrue(isinstance(_mysql.get_client_info(), str)) - def test_thread_safe(self): - self.assertTrue(isinstance(_mysql.thread_safe(), int)) - def test_escape_string(self): self.assertEqual(_mysql.escape_string(b'foo"bar'), b'foo\\"bar', "escape byte string") From 0b6c6656f262409139fcd430a5a5e44883251970 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 13 Dec 2018 17:40:37 +0900 Subject: [PATCH 112/177] Refactoring JSON support (#312) --- MySQLdb/_mysql.c | 106 ++++++++++++++++++++++------------------- MySQLdb/connections.py | 5 +- 2 files changed, 60 insertions(+), 51 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index e90d45eb..b0ba5310 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -302,10 +302,13 @@ _mysql_ResultObject_Initialize( long flags = fields[i].flags; PyObject *fun2=NULL; int j, n2=PySequence_Size(fun); - if (fields[i].charsetnr != 63) { /* maaagic */ - flags &= ~BINARY_FLAG; - } else { + // BINARY_FLAG means ***_bin collation is used. + // To distinguish text and binary, we shoud use charsetnr==63 (binary). + // But we abuse BINARY_FLAG for historical reason. + if (fields[i].charsetnr == 63) { flags |= BINARY_FLAG; + } else { + flags &= ~BINARY_FLAG; } for (j=0; jtype; - // Return bytes for binary and string types. - int binary = 0; - if (field_type == FIELD_TYPE_TINY_BLOB || - field_type == FIELD_TYPE_MEDIUM_BLOB || - field_type == FIELD_TYPE_LONG_BLOB || - field_type == FIELD_TYPE_BLOB || - field_type == FIELD_TYPE_VAR_STRING || - field_type == FIELD_TYPE_STRING || - field_type == FIELD_TYPE_GEOMETRY || - field_type == FIELD_TYPE_BIT) { - binary = 1; + if (rowitem == NULL) { + Py_RETURN_NONE; } -#endif - if (rowitem) { - if (converter == (PyObject*)&PyUnicode_Type) { - if (encoding == utf8) { - //fprintf(stderr, "decoding with utf8!\n"); - v = PyUnicode_DecodeUTF8(rowitem, length, NULL); - } else { - //fprintf(stderr, "decoding with %s\n", encoding); - v = PyUnicode_Decode(rowitem, length, encoding, NULL); - } - } - else if (converter == (PyObject*)&PyBytes_Type || converter == Py_None) { - //fprintf(stderr, "decoding with bytes\n", encoding); - v = PyBytes_FromStringAndSize(rowitem, length); - } - else if (converter == (PyObject*)&PyInt_Type) { - //fprintf(stderr, "decoding with int\n", encoding); - v = PyInt_FromString(rowitem, NULL, 10); + + // Fast paths for int, string and binary. + if (converter == (PyObject*)&PyUnicode_Type) { + if (encoding == utf8) { + //fprintf(stderr, "decoding with utf8!\n"); + return PyUnicode_DecodeUTF8(rowitem, length, NULL); + } else { + //fprintf(stderr, "decoding with %s\n", encoding); + return PyUnicode_Decode(rowitem, length, encoding, NULL); } - else { - //fprintf(stderr, "decoding with callback\n"); - //PyObject_Print(converter, stderr, 0); - //fprintf(stderr, "\n"); - v = PyObject_CallFunction(converter, + } + if (converter == (PyObject*)&PyBytes_Type || converter == Py_None) { + //fprintf(stderr, "decoding with bytes\n", encoding); + return PyBytes_FromStringAndSize(rowitem, length); + } + if (converter == (PyObject*)&PyInt_Type) { + //fprintf(stderr, "decoding with int\n", encoding); + return PyInt_FromString(rowitem, NULL, 10); + } + + //fprintf(stderr, "decoding with callback\n"); + //PyObject_Print(converter, stderr, 0); + //fprintf(stderr, "\n"); #ifdef IS_PY3K - binary ? "y#" : "s#", + int binary; + switch (field->type) { + case FIELD_TYPE_TINY_BLOB: + case FIELD_TYPE_MEDIUM_BLOB: + case FIELD_TYPE_LONG_BLOB: + case FIELD_TYPE_BLOB: + case FIELD_TYPE_VAR_STRING: + case FIELD_TYPE_STRING: + case FIELD_TYPE_GEOMETRY: + case FIELD_TYPE_BIT: +#ifdef FIELD_TYPE_JSON + case FIELD_TYPE_JSON: #else - "s#", + case 245: // JSON #endif - rowitem, - (int)length); - } - } else { - Py_INCREF(Py_None); - v = Py_None; + // Call converter with bytes + binary = 1; + default: // e.g. FIELD_TYPE_DATETIME, etc. + // Call converter with unicode string + binary = 0; } - return v; + return PyObject_CallFunction(converter, + binary ? "y#" : "s#", + rowitem, (int)length); +#else + return PyObject_CallFunction(converter, + "s#", rowitem, (int)length); +#endif } static PyObject * @@ -1226,7 +1232,7 @@ _mysql_row_to_dict_old( unsigned int n, i; unsigned long *length; PyObject *r, *c; - MYSQL_FIELD *fields; + MYSQL_FIELD *fields; n = mysql_num_fields(self->result); if (!(r = PyDict_New())) return NULL; diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 29cc4f32..7de8c8c2 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -183,8 +183,11 @@ def unicode_literal(u, dummy=None): if use_unicode: for t in (FIELD_TYPE.STRING, FIELD_TYPE.VAR_STRING, FIELD_TYPE.VARCHAR, FIELD_TYPE.TINY_BLOB, - FIELD_TYPE.MEDIUM_BLOB, FIELD_TYPE.LONG_BLOB, FIELD_TYPE.BLOB, FIELD_TYPE.JSON): + FIELD_TYPE.MEDIUM_BLOB, FIELD_TYPE.LONG_BLOB, FIELD_TYPE.BLOB): self.converter[t] = _bytes_or_str + # Unlike other string/blob types, JSON is always text. + # MySQL may return JSON with charset==binary. + self.converter[FIELD_TYPE.JSON] = unicode self.encoders[unicode] = unicode_literal self._transactional = self.server_capabilities & CLIENT.TRANSACTIONS From 911bef988c49fbfdc59e44f9198a57e9357dd4ff Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 13 Dec 2018 20:23:46 +0900 Subject: [PATCH 113/177] Refactor extension module initialization (#313) --- MySQLdb/_mysql.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index b0ba5310..1f1b299e 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -2468,7 +2468,7 @@ PyTypeObject _mysql_ConnectionObject_Type = { 0, /* (long) tp_dictoffset */ (initproc)_mysql_ConnectionObject_Initialize, /* tp_init */ NULL, /* tp_alloc */ - NULL, /* tp_new */ + PyType_GenericNew, /* tp_new */ NULL, /* tp_free Low-level free-memory routine */ 0, /* (PyObject *) tp_bases */ 0, /* (PyObject *) tp_mro method resolution order */ @@ -2536,7 +2536,7 @@ PyTypeObject _mysql_ResultObject_Type = { 0, /* (long) tp_dictoffset */ (initproc)_mysql_ResultObject_Initialize, /* tp_init */ NULL, /* tp_alloc */ - NULL, /* tp_new */ + PyType_GenericNew, /* tp_new */ NULL, /* tp_free Low-level free-memory routine */ 0, /* (PyObject *) tp_bases */ 0, /* (PyObject *) tp_mro method resolution order */ @@ -2635,14 +2635,6 @@ init_mysql(void) { PyObject *dict, *module, *emod, *edict; -#ifndef IS_PY3K - _mysql_ConnectionObject_Type.ob_type = &PyType_Type; - _mysql_ResultObject_Type.ob_type = &PyType_Type; -#endif - _mysql_ConnectionObject_Type.tp_alloc = PyType_GenericAlloc; - _mysql_ConnectionObject_Type.tp_new = PyType_GenericNew; - _mysql_ResultObject_Type.tp_alloc = PyType_GenericAlloc; - _mysql_ResultObject_Type.tp_new = PyType_GenericNew; #ifdef IS_PY3K if (PyType_Ready(&_mysql_ConnectionObject_Type) < 0) return NULL; @@ -2652,8 +2644,10 @@ init_mysql(void) module = PyModule_Create(&_mysqlmodule); if (!module) return module; /* this really should never happen */ #else - _mysql_ConnectionObject_Type.tp_free = _PyObject_GC_Del; - _mysql_ResultObject_Type.tp_free = _PyObject_GC_Del; + if (PyType_Ready(&_mysql_ConnectionObject_Type) < 0) + return; + if (PyType_Ready(&_mysql_ResultObject_Type) < 0) + return; module = Py_InitModule4("_mysql", _mysql_methods, _mysql___doc__, (PyObject *)NULL, PYTHON_API_VERSION); if (!module) return; /* this really should never happen */ From 8ad1525c3f11ac299cdf94e9a6b7f6244d37ee7f Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Fri, 14 Dec 2018 16:46:30 +0900 Subject: [PATCH 114/177] Raise ProgrammingError for nan and inf (#314) * Raise ProgrammingError when inf or nan is passed Fixes #246 * Rename _mysql_exceptions -> _exceptions --- MySQLdb/{_mysql_exceptions.py => _exceptions.py} | 10 +++------- MySQLdb/_mysql.c | 2 +- MySQLdb/compat.py | 2 ++ MySQLdb/connections.py | 2 +- MySQLdb/converters.py | 3 +++ MySQLdb/cursors.py | 4 ++-- doc/{_mysql_exceptions.rst => _exceptions.rst} | 4 ++-- doc/_mysql.rst | 2 +- doc/user_guide.rst | 8 ++++---- metadata.cfg | 2 +- 10 files changed, 20 insertions(+), 19 deletions(-) rename MySQLdb/{_mysql_exceptions.py => _exceptions.py} (91%) rename doc/{_mysql_exceptions.rst => _exceptions.rst} (58%) diff --git a/MySQLdb/_mysql_exceptions.py b/MySQLdb/_exceptions.py similarity index 91% rename from MySQLdb/_mysql_exceptions.py rename to MySQLdb/_exceptions.py index 99a79d77..0f14f3bd 100644 --- a/MySQLdb/_mysql_exceptions.py +++ b/MySQLdb/_exceptions.py @@ -1,15 +1,10 @@ -"""_mysql_exceptions: Exception classes for _mysql and MySQLdb. +"""Exception classes for _mysql and MySQLdb. These classes are dictated by the DB API v2.0: https://www.python.org/dev/peps/pep-0249/ """ - -try: - from exceptions import Exception, StandardError, Warning -except ImportError: - # Python 3 - StandardError = Exception +from .compat import StandardError class MySQLError(StandardError): @@ -20,6 +15,7 @@ class Warning(Warning, MySQLError): """Exception raised for important warnings like data truncations while inserting, etc.""" + class Error(MySQLError): """Exception that is the base class of all other error exceptions (not Warning).""" diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 1f1b299e..5ae6652c 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -2669,7 +2669,7 @@ init_mysql(void) (PyObject *)&_mysql_ResultObject_Type)) goto error; Py_INCREF(&_mysql_ResultObject_Type); - if (!(emod = PyImport_ImportModule("MySQLdb._mysql_exceptions"))) { + if (!(emod = PyImport_ImportModule("MySQLdb._exceptions"))) { PyErr_Print(); goto error; } diff --git a/MySQLdb/compat.py b/MySQLdb/compat.py index 8fe6709a..f8d98ac2 100644 --- a/MySQLdb/compat.py +++ b/MySQLdb/compat.py @@ -5,8 +5,10 @@ unicode = unicode unichr = unichr long = long + StandardError = StandardError else: PY2 = False unicode = str unichr = chr long = int + StandardError = Exception diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 7de8c8c2..c7c4c14f 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -9,7 +9,7 @@ from MySQLdb import cursors, _mysql from MySQLdb.compat import unicode, PY2 -from MySQLdb._mysql_exceptions import ( +from MySQLdb._exceptions import ( Warning, Error, InterfaceError, DataError, DatabaseError, OperationalError, IntegrityError, InternalError, NotSupportedError, ProgrammingError, diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index 9b9cb8be..4c4a1a1b 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -36,6 +36,7 @@ from MySQLdb.constants import FIELD_TYPE, FLAG from MySQLdb.times import * from MySQLdb.compat import PY2, long, unicode +from MySQLdb._exceptions import ProgrammingError NoneType = type(None) @@ -66,6 +67,8 @@ def Unicode2Str(s, d): def Float2Str(o, d): s = repr(o) + if s in ('inf', 'nan'): + raise ProgrammingError("%s can not be used with MySQL" % s) if 'e' not in s: s += 'e0' return s diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 9a5e76f9..b8c0d886 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -9,7 +9,7 @@ import sys from .compat import unicode -from ._mysql_exceptions import ( +from ._exceptions import ( Warning, Error, InterfaceError, DataError, DatabaseError, OperationalError, IntegrityError, InternalError, NotSupportedError, ProgrammingError) @@ -48,7 +48,7 @@ class BaseCursor(object): #: Default value of max_allowed_packet is 1048576. max_stmt_length = 64*1024 - from ._mysql_exceptions import ( + from ._exceptions import ( MySQLError, Warning, Error, InterfaceError, DatabaseError, DataError, OperationalError, IntegrityError, InternalError, ProgrammingError, NotSupportedError, diff --git a/doc/_mysql_exceptions.rst b/doc/_exceptions.rst similarity index 58% rename from doc/_mysql_exceptions.rst rename to doc/_exceptions.rst index 2d43525c..b509338c 100644 --- a/doc/_mysql_exceptions.rst +++ b/doc/_exceptions.rst @@ -1,7 +1,7 @@ -_mysql_exceptions Module +_exceptions Module ======================== -.. automodule:: _mysql_exceptions +.. automodule:: MySQLdb._exceptions :members: :undoc-members: :show-inheritance: diff --git a/doc/_mysql.rst b/doc/_mysql.rst index 7ac4918d..cf464b4f 100644 --- a/doc/_mysql.rst +++ b/doc/_mysql.rst @@ -1,7 +1,7 @@ _mysql Module ============= -.. automodule:: _mysql +.. automodule:: MySQLdb._mysql :members: :undoc-members: :show-inheritance: diff --git a/doc/user_guide.rst b/doc/user_guide.rst index 173918dd..7c4302b4 100644 --- a/doc/user_guide.rst +++ b/doc/user_guide.rst @@ -106,7 +106,7 @@ Okay, so you want to use ``_mysql`` anyway. Here are some examples. The simplest possible database connection is:: - import _mysql + from MySQLdb import _mysql db=_mysql.connect() This creates a connection to the MySQL server running on the local @@ -162,8 +162,8 @@ substitution, so you have to pass a complete query string to WHERE price < 5""") There's no return value from this, but exceptions can be raised. The -exceptions are defined in a separate module, ``_mysql_exceptions``, -but ``_mysql`` exports them. Read DB API specification PEP-249_ to +exceptions are defined in a separate module, ``MySQLdb._exceptions``, +but ``MySQLdb._mysql`` exports them. Read DB API specification PEP-249_ to find out what they are, or you can use the catch-all ``MySQLError``. .. _PEP-249: https://www.python.org/dev/peps/pep-0249/ @@ -213,7 +213,7 @@ implicitly asked for one row, since we didn't specify ``maxrows``. The other oddity is: Assuming these are numeric columns, why are they returned as strings? Because MySQL returns all data as strings and expects you to convert it yourself. This would be a real pain in the -ass, but in fact, ``_mysql`` can do this for you. (And ``MySQLdb`` +ass, but in fact, ``MySQLdb._mysql`` can do this for you. (And ``MySQLdb`` does do this for you.) To have automatic type conversion done, you need to create a type converter dictionary, and pass this to ``connect()`` as the ``conv`` keyword parameter. diff --git a/metadata.cfg b/metadata.cfg index 589e52f7..8d185a81 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -28,7 +28,7 @@ classifiers: Topic :: Database Topic :: Database :: Database Engines/Servers py_modules: - MySQLdb._mysql_exceptions + MySQLdb._exceptions MySQLdb.compat MySQLdb.connections MySQLdb.converters From 74c16e7afa79bf5c8b1ef45546502c9839910797 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Fri, 14 Dec 2018 17:24:15 +0900 Subject: [PATCH 115/177] Remove unused converters (#315) --- MySQLdb/converters.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index 4c4a1a1b..645e3780 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -59,12 +59,6 @@ def Thing2Str(s, d): """Convert something into a string via str().""" return str(s) -def Unicode2Str(s, d): - """Convert a unicode object to a string using the default encoding. - This is only used as a placeholder for the real function, which - is connection-dependent.""" - return s.encode() - def Float2Str(o, d): s = repr(o) if s in ('inf', 'nan'): @@ -90,9 +84,6 @@ def Decimal2Literal(o, d): def array2Str(o, d): return Thing2Literal(o.tostring(), d) -def quote_tuple(t, d): - return "(%s)" % (','.join(escape_sequence(t, d))) - # bytes or str regarding to BINARY_FLAG. _bytes_or_str = ((FLAG.BINARY, bytes), (None, unicode)) @@ -106,7 +97,6 @@ def quote_tuple(t, d): Date: Thing2Literal, DateTimeType: DateTime2literal, DateTimeDeltaType: DateTimeDelta2literal, - str: Thing2Literal, # default set: Set2Str, Decimal: Decimal2Literal, From ecf6d53aef525ce2f7dfd4bdbf70fd1b470d6b19 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sat, 15 Dec 2018 10:15:13 +0900 Subject: [PATCH 116/177] 1.4.0rc2 --- metadata.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metadata.cfg b/metadata.cfg index 8d185a81..431e6014 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.4.0rc1 -version_info: (1,4,0,'rc',1) +version: 1.4.0rc2 +version_info: (1,4,0,'rc',2) description: Python interface to MySQL author: Inada Naoki author_email: songofacandy@gmail.com From ea80e8f07a803307778fa43ae8f70f28f39a4a5b Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 17 Dec 2018 19:27:54 +0900 Subject: [PATCH 117/177] Fix error handling of mysql_real_connect (#317) Fixes #316 --- MySQLdb/_mysql.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 5ae6652c..cf4566d6 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -446,6 +446,7 @@ _mysql_ConnectionObject_Initialize( Py_BEGIN_ALLOW_THREADS ; conn = mysql_init(&(self->connection)); + self->open = 1; if (connect_timeout) { unsigned int timeout = connect_timeout; mysql_options(&(self->connection), MYSQL_OPT_CONNECT_TIMEOUT, @@ -496,6 +497,7 @@ _mysql_ConnectionObject_Initialize( if (!conn) { _mysql_Exception(self); + self->open = 0; return -1; } @@ -515,7 +517,6 @@ _mysql_ConnectionObject_Initialize( be done here. tp_dealloc still needs to call PyObject_GC_UnTrack(), however. */ - self->open = 1; return 0; } From f7544710979033523a7009e64da05c8fa5c7a9d0 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 17 Dec 2018 19:28:16 +0900 Subject: [PATCH 118/177] Update HISTORY --- HISTORY.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/HISTORY.rst b/HISTORY.rst index 54b09a53..47356b7f 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -26,11 +26,18 @@ Release: TBD * Remove ``_mysql.NULL`` constant. +* Remove ``_mysql.thread_safe()`` function. + * Support non-ASCII field name with non-UTF-8 connection encoding. (#210) * Optimize decoding speed of string and integer types. -* Removed ``MySQLdb.constants.REFRESH`` module. +* Remove ``MySQLdb.constants.REFRESH`` module. + +* Remove support for old datetime format for MySQL < 4.1. + +* Fix wrong errno is raised when ``mysql_real_connect`` is failed. (#316) + ====================== What's new in 1.3.14 From 0d1b6b6dc98d388abbf18bc4b144018559f95a43 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 17 Dec 2018 19:28:47 +0900 Subject: [PATCH 119/177] 1.4.0rc3 --- metadata.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metadata.cfg b/metadata.cfg index 431e6014..c3fddb50 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.4.0rc2 -version_info: (1,4,0,'rc',2) +version: 1.4.0rc3 +version_info: (1,4,0,'rc',3) description: Python interface to MySQL author: Inada Naoki author_email: songofacandy@gmail.com From 3870135037e602f2efd680e7a43b47f0ee9efd3e Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 18 Jan 2019 20:52:10 +0900 Subject: [PATCH 120/177] Fix windows build (#322) --- MySQLdb/_mysql.c | 13 ++++++------- setup_windows.py | 23 +++++++++++++---------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index cf4566d6..45df37c3 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -917,7 +917,8 @@ _mysql_string_literal( { PyObject *str, *s; char *in, *out; - int len, size; + unsigned long len; + Py_ssize_t size; if (self && PyModule_Check((PyObject*)self)) self = NULL; @@ -1986,12 +1987,10 @@ _mysql_ConnectionObject_repr( { char buf[300]; if (self->open) - sprintf(buf, "<_mysql.connection open to '%.256s' at %lx>", - self->connection.host, - (long)self); + snprintf(buf, 300, "<_mysql.connection open to '%.256s' at %p>", + self->connection.host, self); else - sprintf(buf, "<_mysql.connection closed at %lx>", - (long)self); + snprintf(buf, 300, "<_mysql.connection closed at %p>", self); return PyString_FromString(buf); } @@ -2024,7 +2023,7 @@ _mysql_ResultObject_repr( _mysql_ResultObject *self) { char buf[300]; - sprintf(buf, "<_mysql.result object at %lx>", (long)self); + snprintf(buf, 300, "<_mysql.result object at %p>", self); return PyString_FromString(buf); } diff --git a/setup_windows.py b/setup_windows.py index 5a4d236b..cb2cbab0 100644 --- a/setup_windows.py +++ b/setup_windows.py @@ -11,21 +11,24 @@ def get_config(): extra_objects = [] - if enabled(options, 'embedded'): - client = "mysqld" - else: - client = "mysqlclient" + # client = "mysqlclient" + client = "mariadbclient" vcversion = int(get_build_version()) - library_dirs = [ os.path.join(connector, r'lib\vs%d' % vcversion) ] - libraries = [ 'kernel32', 'advapi32', 'wsock32', client ] - include_dirs = [ os.path.join(connector, r'include') ] - extra_compile_args = [ '/Zl' ] + if client == "mariadbclient": + library_dirs = [os.path.join(connector, 'lib', 'mariadb')] + libraries = ['kernel32', 'advapi32', 'wsock32', 'shlwapi', 'Ws2_32', client ] + include_dirs = [os.path.join(connector, 'include', 'mariadb')] + else: + library_dirs = [os.path.join(connector, r'lib\vs%d' % vcversion), + os.path.join(connector, "lib")] + libraries = ['kernel32', 'advapi32', 'wsock32', client ] + include_dirs = [os.path.join(connector, r'include')] + + extra_compile_args = ['/Zl', '/D_CRT_SECURE_NO_WARNINGS' ] extra_link_args = ['/MANIFEST'] name = "mysqlclient" - if enabled(options, 'embedded'): - name = name + "-embedded" metadata['name'] = name define_macros = [ From bf0ef58baae90f2f76d88c404025fde83f572fdb Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 18 Jan 2019 20:55:42 +0900 Subject: [PATCH 121/177] 1.4.0 --- HISTORY.rst | 2 +- metadata.cfg | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 47356b7f..3fe5212d 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,7 +2,7 @@ What's new in 1.4.0 ====================== -Release: TBD +Release: 2019-01-18 * Removed ``threadsafe`` and ``embedded`` build options. diff --git a/metadata.cfg b/metadata.cfg index c3fddb50..e3685b88 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.4.0rc3 -version_info: (1,4,0,'rc',3) +version: 1.4.0 +version_info: (1,4,0,'final',0) description: Python interface to MySQL author: Inada Naoki author_email: songofacandy@gmail.com From f48a294aaae8199c25d8f8e4c0a22e14c9bab415 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sat, 19 Jan 2019 12:55:41 +0900 Subject: [PATCH 122/177] Fix dict parameter support (#324) --- MySQLdb/cursors.py | 7 ++++++- tests/test_cursor.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index b8c0d886..4460799e 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -181,7 +181,12 @@ def execute(self, query, args=None): if args is not None: if isinstance(args, dict): - args = dict((key, db.literal(item)) for key, item in args.items()) + nargs = {} + for key, item in args.items(): + if isinstance(key, unicode): + key = key.encode(db.encoding) + nargs[key] = db.literal(item) + args = nargs else: args = tuple(map(db.literal, args)) try: diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 7919b561..ff96368f 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -1,3 +1,5 @@ +from __future__ import print_function, absolute_import + import pytest import MySQLdb.cursors from configdb import connection_factory @@ -76,3 +78,13 @@ def test_executemany(): assert cursor._executed.endswith(b"(3, 4),(5, 6)"), "executemany with %% not in one query" finally: cursor.execute("DROP TABLE IF EXISTS percent_test") + + +def test_pyparam(): + conn = connect() + cursor = conn.cursor() + + cursor.execute(u"SELECT %(a)s, %(b)s", {u'a': 1, u'b': 2}) + assert cursor._executed == b"SELECT 1, 2" + cursor.execute(b"SELECT %(a)s, %(b)s", {b'a': 3, b'b': 4}) + assert cursor._executed == b"SELECT 3, 4" From 4d278c9f43adde9b1880b0385ee3ec9f80af5ebe Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sat, 19 Jan 2019 12:57:15 +0900 Subject: [PATCH 123/177] Update HISTORY --- HISTORY.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/HISTORY.rst b/HISTORY.rst index 3fe5212d..0583cab9 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,9 +1,19 @@ +====================== + What's new in 1.4.1 +====================== + +Release: 2019-01-19 + +* Fix dict parameter support (#323, regression of 1.4.0) + ====================== What's new in 1.4.0 ====================== Release: 2019-01-18 +* Dropped Python 3.4 support. + * Removed ``threadsafe`` and ``embedded`` build options. * Remove some deprecated cursor classes and methods. From c29d1660234b79cdfc797a91b21524dda5372bee Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sat, 19 Jan 2019 13:24:20 +0900 Subject: [PATCH 124/177] v1.4.1 --- metadata.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metadata.cfg b/metadata.cfg index e3685b88..1834c1c8 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.4.0 -version_info: (1,4,0,'final',0) +version: 1.4.1 +version_info: (1,4,1,'final',0) description: Python interface to MySQL author: Inada Naoki author_email: songofacandy@gmail.com From 1f6e18d345723a29a0f9f8ca8ea6a825809c97c3 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Thu, 7 Feb 2019 18:05:40 +0900 Subject: [PATCH 125/177] Add some garbage for Django 1.11 compatibility (#327) Django touched private area of this library. Removing unused variables broke Django. While Django 2.0 fixed it, Django 1.11 doesn't fix it because it is in security-only fix mode. So add unused variables for Django 1.11 compatibility. They will be removed in next minor release. Fix #303, #306 --- MySQLdb/connections.py | 14 ++++++++++++-- MySQLdb/cursors.py | 9 +++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index c7c4c14f..37b52d81 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -166,6 +166,13 @@ class object, used to create cursors (keyword only) self.encoders = dict([ (k, v) for k, v in conv.items() if type(k) is not int ]) + # XXX THIS IS GARBAGE: While this is just a garbage and undocumented, + # Django 1.11 depends on it. And they don't fix it because + # they are in security-only fix mode. + # So keep this garbage for now. This will be removed in 1.5. + # See PyMySQL/mysqlclient-python#306 + self.encoders[bytes] = bytes + self._server_version = tuple([ numeric_part(n) for n in self.get_server_info().split('.')[:2] ]) self.encoding = 'ascii' # overridden in set_character_set() @@ -238,8 +245,11 @@ def literal(self, o): s = self.string_literal(o.encode(self.encoding)) elif isinstance(o, bytearray): s = self._bytes_literal(o) - elif not PY2 and isinstance(o, bytes): - s = self._bytes_literal(o) + elif isinstance(o, bytes): + if PY2: + s = self.string_literal(o) + else: + s = self._bytes_literal(o) elif isinstance(o, (tuple, list)): s = self._tuple_literal(o) else: diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 4460799e..5f6e4995 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -63,6 +63,14 @@ def __init__(self, connection): self.rowcount = -1 self.arraysize = 1 self._executed = None + + # XXX THIS IS GARBAGE: While this is totally garbage and private, + # Django 1.11 depends on it. And they don't fix it because + # they are in security-only fix mode. + # So keep this garbage for now. This will be removed in 1.5. + # See PyMySQL/mysqlclient-python#303 + self._last_executed = None + self.lastrowid = None self.messages = [] self._result = None @@ -305,6 +313,7 @@ def _query(self, q): self._do_get_result(db) self._post_get_result() self._executed = q + self._last_executed = q # XXX THIS IS GARBAGE: See above. return self.rowcount def _fetch_row(self, size=1): From 9339b153f0805a2fb38c5a0999fea7df4081eb44 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Thu, 7 Feb 2019 20:39:33 +0900 Subject: [PATCH 126/177] travis: run django-1.11.18 tests (#328) --- .travis.yml | 25 +++++++++++++++++++++++++ ci/test_mysql.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 ci/test_mysql.py diff --git a/.travis.yml b/.travis.yml index 6a1df409..c7dfe890 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,4 +36,29 @@ script: after_succes: - codecov +matrix: + include: + - &django_py27 + python: "2.7" + install: + - pip install -U pip + - wget https://github.com/django/django/archive/1.11.18.tar.gz + - tar xf 1.11.18.tar.gz + - pip install django-1.11.18/ + - cp ci/test_mysql.py django-1.11.18/tests/ + - pip install . + + before_script: + - mysql -e 'create user django identified by "secret"' + - mysql -e 'grant all on *.* to django' + - mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql + + script: + - cd django-1.11.18/tests/ + - ./runtests.py --parallel=1 --settings=test_mysql + + - &django_py3 + <<: *django_py27 + python: "3.7" + # vim: sw=2 ts=2 sts=2 diff --git a/ci/test_mysql.py b/ci/test_mysql.py new file mode 100644 index 00000000..d24f30f1 --- /dev/null +++ b/ci/test_mysql.py @@ -0,0 +1,45 @@ +# This is an example test settings file for use with the Django test suite. +# +# The 'sqlite3' backend requires only the ENGINE setting (an in- +# memory database will be used). All other backends will require a +# NAME and potentially authentication information. See the +# following section in the docs for more information: +# +# https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/unit-tests/ +# +# The different databases that Django supports behave differently in certain +# situations, so it is recommended to run the test suite against as many +# database backends as possible. You may want to create a separate settings +# file for each of the backends you test against. + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': 'django_default', + 'USER': 'django', + 'HOST': '127.0.0.1', + 'PASSWORD': 'secret', + 'TEST': { + 'CHARSET': 'utf8mb4', + 'COLLATION': 'utf8mb4_general_ci', + }, + }, + 'other': { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': 'django_other', + 'USER': 'django', + 'HOST': '127.0.0.1', + 'PASSWORD': 'secret', + 'TEST': { + 'CHARSET': 'utf8mb4', + 'COLLATION': 'utf8mb4_general_ci', + }, + } +} + +SECRET_KEY = "django_tests_secret_key" + +# Use a fast hasher to speed up tests. +PASSWORD_HASHERS = [ + 'django.contrib.auth.hashers.MD5PasswordHasher', +] From c3dcb16f8e497953102e4d3f55fc35aff1ca0d98 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 8 Feb 2019 21:23:05 +0900 Subject: [PATCH 127/177] v1.4.2 --- HISTORY.rst | 12 ++++++++++++ metadata.cfg | 4 ++-- setup.cfg | 16 ---------------- 3 files changed, 14 insertions(+), 18 deletions(-) delete mode 100644 setup.cfg diff --git a/HISTORY.rst b/HISTORY.rst index 0583cab9..e8127ba9 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,3 +1,15 @@ +====================== + What's new in 1.4.2 +====================== + +Release: 2019-02-08 + +* Fix Django 1.11 compatibility. (#327) + mysqlclient 1.5 will not support Django 1.11. It is not because + mysqlclient will break backward compatibility, but Django used + unsupported APIs and Django 1.11 don't fix bugs including + compatibility issues. + ====================== What's new in 1.4.1 ====================== diff --git a/metadata.cfg b/metadata.cfg index 1834c1c8..fd7790b1 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.4.1 -version_info: (1,4,1,'final',0) +version: 1.4.2 +version_info: (1,4,2,'final',0) description: Python interface to MySQL author: Inada Naoki author_email: songofacandy@gmail.com diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index eb5b5513..00000000 --- a/setup.cfg +++ /dev/null @@ -1,16 +0,0 @@ -[build_ext] -## Only uncomment/set these if the default configuration doesn't work -## Also see https://docs.python.org/distutils/configfile.html -# include-dirs = ? -# library-dirs = ? -# link-objects = ? -# rpath = ? -# libraries = ? - -[bdist_rpm] -doc_files = README MANIFEST doc/*.txt -vendor = MySQL-python SourceForge Project -packager = Andy Dustman -distribution-name = Red Stains Linux -requires = python -build-requires = python-devel mysql-devel zlib-devel openssl-devel From 474dac6b45b81f401172c0e330ae0014fa27b772 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 13 Feb 2019 20:55:46 +0900 Subject: [PATCH 128/177] post1 release --- metadata.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metadata.cfg b/metadata.cfg index fd7790b1..c8112de2 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.4.2 -version_info: (1,4,2,'final',0) +version: 1.4.2.post1 +version_info: (1,4,2,'post',1) description: Python interface to MySQL author: Inada Naoki author_email: songofacandy@gmail.com From 79cd82388e807c2b3e16bfe8b8e3ee5d2aef51f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 16 Feb 2019 15:06:48 +0200 Subject: [PATCH 129/177] travis: Fix after_success spelling, should actually upload to codecov now (#332) --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c7dfe890..017c3bb9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,7 +33,7 @@ script: - pip install -e . - pytest --cov ./MySQLdb -after_succes: +after_success: - codecov matrix: From 7893c5a97d485834c4416ff3af6c98dcb0629d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sun, 17 Feb 2019 05:13:10 +0200 Subject: [PATCH 130/177] Grammar fixes (#333) --- doc/user_guide.rst | 2 +- tests/test_MySQLdb_nonstandard.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/user_guide.rst b/doc/user_guide.rst index 7c4302b4..405b444e 100644 --- a/doc/user_guide.rst +++ b/doc/user_guide.rst @@ -8,7 +8,7 @@ MySQLdb User's Guide Introduction ------------ -MySQLdb is a interface to the popular MySQL +MySQLdb is an interface to the popular MySQL database server that provides the Python database API. Installation diff --git a/tests/test_MySQLdb_nonstandard.py b/tests/test_MySQLdb_nonstandard.py index 2b33c398..ea05079f 100644 --- a/tests/test_MySQLdb_nonstandard.py +++ b/tests/test_MySQLdb_nonstandard.py @@ -83,7 +83,7 @@ def test_proto_info(self): def test_server_info(self): self.assertTrue(isinstance(self.conn.get_server_info(), str), - "Should return an str.") + "Should return a string.") def test_client_flag(self): conn = connection_factory( From f683ee7826363ca8f2044eeaabe5b081a580176f Mon Sep 17 00:00:00 2001 From: Carson Ip Date: Sun, 3 Mar 2019 11:13:38 +0800 Subject: [PATCH 131/177] Fix typo in docstring and minor formatting (#336) --- MySQLdb/_mysql.c | 2 +- MySQLdb/cursors.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 45df37c3..380e2ef6 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -732,7 +732,7 @@ _mysql_ConnectionObject_commit( } static char _mysql_ConnectionObject_rollback__doc__[] = -"Rolls backs the current transaction\n\ +"Rolls back the current transaction\n\ "; static PyObject * _mysql_ConnectionObject_rollback( diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 5f6e4995..82eb64e7 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -471,7 +471,7 @@ class Cursor(CursorStoreResultMixIn, CursorTupleRowsMixIn, class DictCursor(CursorStoreResultMixIn, CursorDictRowsMixIn, BaseCursor): - """This is a Cursor class that returns rows as dictionaries and + """This is a Cursor class that returns rows as dictionaries and stores the result set in the client.""" From 1a67ae2cc2da1a2bf511cf70b20dcd5fca5ba7fe Mon Sep 17 00:00:00 2001 From: Carson Ip Date: Mon, 4 Mar 2019 18:24:43 +0800 Subject: [PATCH 132/177] Fix description of SEGV issue in HISTORY (#337) --- HISTORY.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index e8127ba9..c5221a21 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -73,8 +73,9 @@ Release: 2018-12-04 * Add ``Connection._get_native_connection`` for XTA project (#269) -* Fix SEGV on MariaDB Connector/C when ``Connection.close()`` is called - for closed connection. (#270, #272, #276) +* Fix SEGV on MariaDB Connector/C when some methods of ``Connection`` + objects are called after ``Connection.close()`` is called. (#270, #272, #276) + See https://jira.mariadb.org/browse/CONC-289 * Fix ``Connection.client_flag`` (#266) From e52812fd15084344910d4eb55b4a6d3b12ef1baf Mon Sep 17 00:00:00 2001 From: Keery Nie Date: Tue, 19 Mar 2019 15:55:38 +0800 Subject: [PATCH 133/177] Improve docstring of `use_unicode` and `charset` options (#345) --- MySQLdb/connections.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 37b52d81..cbfc9afb 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -73,21 +73,23 @@ class object, used to create cursors (keyword only) :param bool use_unicode: If True, text-like columns are returned as unicode objects - using the connection's character set. Otherwise, text-like - columns are returned as strings. columns are returned as - normal strings. Unicode objects will always be encoded to - the connection's character set regardless of this setting. - Default to False on Python 2 and True on Python 3. + using the connection's character set. Otherwise, text-like + columns are returned as bytes. Unicode objects will always + be encoded to the connection's character set regardless of + this setting. + Default to False on Python 2 and True on Python 3 + so that you can always get python `str` object by default. :param str charset: If supplied, the connection character set will be changed - to this character set (MySQL-4.1 and newer). This implies - use_unicode=True. + to this character set. + On Python 2, this option changes default value of `use_unicode` + option from False to True. :param str sql_mode: If supplied, the session SQL mode will be changed to this - setting (MySQL-4.1 and newer). For more details and legal - values, see the MySQL documentation. + setting. + For more details and legal values, see the MySQL documentation. :param int client_flag: flags to use or 0 (see MySQL docs or constants/CLIENTS.py) From e04c5972f526805e7c310aa213d1eadbabb45371 Mon Sep 17 00:00:00 2001 From: Jason Fried Date: Tue, 19 Mar 2019 01:02:44 -0700 Subject: [PATCH 134/177] Add missing break (#346) Fixes #343 --- MySQLdb/_mysql.c | 1 + 1 file changed, 1 insertion(+) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 380e2ef6..3c846274 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -1147,6 +1147,7 @@ _mysql_field_to_python( #endif // Call converter with bytes binary = 1; + break; default: // e.g. FIELD_TYPE_DATETIME, etc. // Call converter with unicode string binary = 0; From b66971ee36be96b772ae7fdec79ccc1611376f3c Mon Sep 17 00:00:00 2001 From: Jason Fried Date: Mon, 25 Mar 2019 14:46:32 -0700 Subject: [PATCH 135/177] memory leak in ConnectionObject_Initialize when mysql_real_connect fails (#350) We can't set open=0 after we mysql_init or dealloc will not cleanup the memory. Also if mysql_init returns NULL we are out of memory and shouldn't set open=1, or we could segfault in dealloc if we didn't seg before that. --- MySQLdb/_mysql.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 3c846274..f12231bc 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -444,8 +444,12 @@ _mysql_ConnectionObject_Initialize( _stringsuck(cipher, value, ssl); } - Py_BEGIN_ALLOW_THREADS ; conn = mysql_init(&(self->connection)); + if (!conn) { + PyErr_SetNone(PyExc_MemoryError); + return -1; + } + Py_BEGIN_ALLOW_THREADS ; self->open = 1; if (connect_timeout) { unsigned int timeout = connect_timeout; @@ -497,7 +501,6 @@ _mysql_ConnectionObject_Initialize( if (!conn) { _mysql_Exception(self); - self->open = 0; return -1; } From 3d046add194a2bb7c708f1d3271377ad8e78c26f Mon Sep 17 00:00:00 2001 From: Ben MacLeod Date: Thu, 6 Jun 2019 07:54:54 -0300 Subject: [PATCH 136/177] Add to the client library list for static builds (#357) --- setup_posix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup_posix.py b/setup_posix.py index 9289bb50..2b594102 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -69,7 +69,7 @@ def get_config(): # properly handle mysql client libraries that are not called libmysqlclient client = None CLIENT_LIST = ['mysqlclient', 'mysqlclient_r', 'mysqld', 'mariadb', - 'perconaserverclient', 'perconaserverclient_r'] + 'mariadbclient', 'perconaserverclient', 'perconaserverclient_r'] for c in CLIENT_LIST: if c in libraries: client = c From 6f5f48b76c4e0b81565b5b9221a6728ffb590b0a Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sat, 8 Jun 2019 23:53:46 +0900 Subject: [PATCH 137/177] use PY_SSIZE_T_CLEAN (#360) Fixes #359 --- MySQLdb/_mysql.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index f12231bc..cef196d1 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -34,6 +34,7 @@ PERFORMANCE OF THIS SOFTWARE. #define my_bool _Bool #endif +#define PY_SSIZE_T_CLEAN 1 #include "Python.h" #if PY_MAJOR_VERSION >= 3 #define IS_PY3K @@ -882,7 +883,8 @@ _mysql_escape_string( { PyObject *str; char *in, *out; - int len, size; + int len; + Py_ssize_t size; if (!PyArg_ParseTuple(args, "s#:escape_string", &in, &size)) return NULL; str = PyBytes_FromStringAndSize((char *) NULL, size*2+1); if (!str) return PyErr_NoMemory(); @@ -1102,7 +1104,7 @@ static PyObject * _mysql_field_to_python( PyObject *converter, const char *rowitem, - unsigned long length, + Py_ssize_t length, MYSQL_FIELD *field, const char *encoding) { @@ -1157,10 +1159,10 @@ _mysql_field_to_python( } return PyObject_CallFunction(converter, binary ? "y#" : "s#", - rowitem, (int)length); + rowitem, (Py_ssize_t)length); #else return PyObject_CallFunction(converter, - "s#", rowitem, (int)length); + "s#", rowitem, (Py_ssize_t)length); #endif } @@ -1752,7 +1754,8 @@ _mysql_ConnectionObject_query( PyObject *args) { char *query; - int len, r; + Py_ssize_t len; + int r; if (!PyArg_ParseTuple(args, "s#:query", &query, &len)) return NULL; check_connection(self); @@ -1774,7 +1777,8 @@ _mysql_ConnectionObject_send_query( PyObject *args) { char *query; - int len, r; + Py_ssize_t len; + int r; MYSQL *mysql = &(self->connection); if (!PyArg_ParseTuple(args, "s#:query", &query, &len)) return NULL; check_connection(self); From 23addef86d4bd3b00975d203d05e89fafbd82e6c Mon Sep 17 00:00:00 2001 From: ckclark Date: Wed, 17 Jul 2019 13:10:32 +0800 Subject: [PATCH 138/177] Remove unused "NULL" from `__all__` in `__init__.py` (#369) Since the symbol "NULL" is removed in 819688b6, it should be also removed from `__all__`. Fix issue #368. --- MySQLdb/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MySQLdb/__init__.py b/MySQLdb/__init__.py index 92b5d264..d4c7fa56 100644 --- a/MySQLdb/__init__.py +++ b/MySQLdb/__init__.py @@ -89,7 +89,7 @@ def Connect(*args, **kwargs): 'Date', 'Time', 'Timestamp', 'DateFromTicks', 'TimeFromTicks', 'TimestampFromTicks', 'DataError', 'DatabaseError', 'Error', 'FIELD_TYPE', 'IntegrityError', 'InterfaceError', 'InternalError', - 'MySQLError', 'NULL', 'NUMBER', 'NotSupportedError', 'DBAPISet', + 'MySQLError', 'NUMBER', 'NotSupportedError', 'DBAPISet', 'OperationalError', 'ProgrammingError', 'ROWID', 'STRING', 'TIME', 'TIMESTAMP', 'Warning', 'apilevel', 'connect', 'connections', 'constants', 'converters', 'cursors', 'debug', 'escape', From b3e775a780a20caad0ba85d7de9458e1da71eb73 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Thu, 8 Aug 2019 18:55:21 +0900 Subject: [PATCH 139/177] fix Cursor.executemany created many circular references (#375) --- MySQLdb/cursors.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 82eb64e7..ee834e45 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -110,14 +110,17 @@ def ensure_bytes(x): return x if isinstance(args, (tuple, list)): - return tuple(literal(ensure_bytes(arg)) for arg in args) + ret = tuple(literal(ensure_bytes(arg)) for arg in args) elif isinstance(args, dict): - return {ensure_bytes(key): literal(ensure_bytes(val)) - for (key, val) in args.items()} + ret = {ensure_bytes(key): literal(ensure_bytes(val)) + for (key, val) in args.items()} else: # If it's not a dictionary let's try escaping it anyways. # Worst case it will throw a Value error - return literal(ensure_bytes(args)) + ret = literal(ensure_bytes(args)) + + ensure_bytes = None # break circular reference + return ret def _check_executed(self): if not self._executed: From 8e9d75925dccb21ff210528f07ee8c07ac448e80 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Thu, 8 Aug 2019 18:55:52 +0900 Subject: [PATCH 140/177] try mariadb_config when mysql_config is not found (#374) --- setup_posix.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/setup_posix.py b/setup_posix.py index 2b594102..c65c045f 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -37,6 +37,16 @@ def get_config(): if 'mysql_config' in options: _mysql_config_path = options['mysql_config'] + else: + try: + mysql_config('version') + except EnvironmentError: + # try mariadb_config + _mysql_config_path = "mariadb_config" + try: + mysql_config('version') + except EnvironmentError: + _mysql_config_path = "mysql_config" extra_objects = [] static = enabled(options, 'static') From 62c8c8c62bd0c7e943c5dc68678d9fb2a0fd1610 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Thu, 8 Aug 2019 21:31:25 +0900 Subject: [PATCH 141/177] call mysql_library_init in module initialization (#377) --- MySQLdb/_mysql.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index cef196d1..f5e98f12 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -2643,6 +2643,15 @@ init_mysql(void) { PyObject *dict, *module, *emod, *edict; + if (mysql_library_init(0, NULL, NULL)) { + PyErr_SetString(PyExc_ImportError, "_mysql: mysql_library_init failed"); +#ifdef IS_PY3K + return NULL; +#else + return; +#endif + } + #ifdef IS_PY3K if (PyType_Ready(&_mysql_ConnectionObject_Type) < 0) return NULL; From a8329a3096596d65ff9ab26cdd579b1d1ca3a0af Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Thu, 8 Aug 2019 21:48:28 +0900 Subject: [PATCH 142/177] travis: use build names (#378) --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 017c3bb9..76952ceb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ python: - "3.6" - "3.5" - "2.7" + - "3.8-dev" cache: pip @@ -39,6 +40,7 @@ after_success: matrix: include: - &django_py27 + name: "Django test (Python 2.7) python: "2.7" install: - pip install -U pip @@ -59,6 +61,7 @@ matrix: - &django_py3 <<: *django_py27 + name: "Django test (Python 3.7)" python: "3.7" # vim: sw=2 ts=2 sts=2 From 5a40d5772ec8e634626241cdfbe19acb35f3313e Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 9 Aug 2019 17:55:33 +0900 Subject: [PATCH 143/177] 1.4.3 --- HISTORY.rst | 15 +++++++++++++++ metadata.cfg | 5 +++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index c5221a21..3245dfd6 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,3 +1,18 @@ +====================== + What's new in 1.4.3 +====================== + +Release: 2019-08-09 + +* ``--static`` build supports ``libmariadbclient.a`` +* Try ``mariadb_config`` when ``mysql_config`` is not found +* Fixed warning happend in Python 3.8 (#359) +* Fixed ``from MySQLdb import *``, while I don't recommend it. (#369) +* Fixed SEGV ``MySQLdb.escape_string("1")`` when libmariadb is used and + no connection is created. (#367) +* Fixed many circular references are created in ``Cursor.executemany()``. (#375) + + ====================== What's new in 1.4.2 ====================== diff --git a/metadata.cfg b/metadata.cfg index c8112de2..19df5421 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.4.2.post1 -version_info: (1,4,2,'post',1) +version: 1.4.3 +version_info: (1,4,3,'final',0) description: Python interface to MySQL author: Inada Naoki author_email: songofacandy@gmail.com @@ -25,6 +25,7 @@ classifiers: Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 Topic :: Database Topic :: Database :: Database Engines/Servers py_modules: From b400acddd0e1ee28e3fcfecc77512f660765efc9 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sun, 11 Aug 2019 11:23:40 +0900 Subject: [PATCH 144/177] set charset before connect (#382) --- MySQLdb/_mysql.c | 13 +++++++++---- MySQLdb/connections.py | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index f5e98f12..2a431dc3 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -397,7 +397,7 @@ _mysql_ConnectionObject_Initialize( "read_default_file", "read_default_group", "client_flag", "ssl", "local_infile", - "read_timeout", "write_timeout", + "read_timeout", "write_timeout", "charset", NULL } ; int connect_timeout = 0; int read_timeout = 0; @@ -405,13 +405,14 @@ _mysql_ConnectionObject_Initialize( int compress = -1, named_pipe = -1, local_infile = -1; char *init_command=NULL, *read_default_file=NULL, - *read_default_group=NULL; + *read_default_group=NULL, + *charset=NULL; self->converter = NULL; self->open = 0; if (!PyArg_ParseTupleAndKeywords(args, kwargs, - "|ssssisOiiisssiOiii:connect", + "|ssssisOiiisssiOiiis:connect", kwlist, &host, &user, &passwd, &db, &port, &unix_socket, &conv, @@ -422,7 +423,8 @@ _mysql_ConnectionObject_Initialize( &client_flag, &ssl, &local_infile, &read_timeout, - &write_timeout + &write_timeout, + &charset )) return -1; @@ -486,6 +488,9 @@ _mysql_ConnectionObject_Initialize( if (ssl) { mysql_ssl_set(&(self->connection), key, cert, ca, capath, cipher); } + if (charset) { + mysql_options(&(self->connection), MYSQL_SET_CHARSET_NAME, charset); + } conn = mysql_real_connect(&(self->connection), host, user, passwd, db, port, unix_socket, client_flag); diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index cbfc9afb..5c5e6d48 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -140,7 +140,7 @@ class object, used to create cursors (keyword only) kwargs2['conv'] = conv2 cursorclass = kwargs2.pop('cursorclass', self.default_cursor) - charset = kwargs2.pop('charset', '') + charset = kwargs2.get('charset', '') if charset or not PY2: use_unicode = True From 17045e8ed04579ec2d80e9daa9c7b69fbfcd5c2f Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 12 Aug 2019 00:35:04 +0900 Subject: [PATCH 145/177] 1.4.4 --- HISTORY.rst | 11 +++++++++++ metadata.cfg | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 3245dfd6..7a30a0a2 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,3 +1,14 @@ +====================== + What's new in 1.4.4 +====================== + +Release: 2019-08-12 + +* ``charset`` option is passed to ``mysql_options(mysql, MYSQL_SET_CHARSET_NAME, charset)`` + before ``mysql_real_connect`` is called. + This avoid extra ``SET NAMES `` query when creating connection. + + ====================== What's new in 1.4.3 ====================== diff --git a/metadata.cfg b/metadata.cfg index 19df5421..96e665f7 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.4.3 -version_info: (1,4,3,'final',0) +version: 1.4.4 +version_info: (1,4,4,'final',0) description: Python interface to MySQL author: Inada Naoki author_email: songofacandy@gmail.com From acf4cdcf896105cfead13f8be7bc9baa38e849d7 Mon Sep 17 00:00:00 2001 From: Mackenzie Boyd Date: Wed, 23 Oct 2019 03:19:23 -0700 Subject: [PATCH 146/177] Add 'auth_plugin' option (#389) --- MySQLdb/_mysql.c | 12 +++++++++--- MySQLdb/connections.py | 5 +++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 2a431dc3..6e4269e3 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -398,6 +398,7 @@ _mysql_ConnectionObject_Initialize( "client_flag", "ssl", "local_infile", "read_timeout", "write_timeout", "charset", + "auth_plugin", NULL } ; int connect_timeout = 0; int read_timeout = 0; @@ -406,13 +407,14 @@ _mysql_ConnectionObject_Initialize( char *init_command=NULL, *read_default_file=NULL, *read_default_group=NULL, - *charset=NULL; + *charset=NULL, + *auth_plugin=NULL; self->converter = NULL; self->open = 0; if (!PyArg_ParseTupleAndKeywords(args, kwargs, - "|ssssisOiiisssiOiiis:connect", + "|ssssisOiiisssiOiiiss:connect", kwlist, &host, &user, &passwd, &db, &port, &unix_socket, &conv, @@ -424,7 +426,8 @@ _mysql_ConnectionObject_Initialize( &local_infile, &read_timeout, &write_timeout, - &charset + &charset, + &auth_plugin )) return -1; @@ -491,6 +494,9 @@ _mysql_ConnectionObject_Initialize( if (charset) { mysql_options(&(self->connection), MYSQL_SET_CHARSET_NAME, charset); } + if (auth_plugin) { + mysql_options(&(self->connection), MYSQL_DEFAULT_AUTH, auth_plugin); + } conn = mysql_real_connect(&(self->connection), host, user, passwd, db, port, unix_socket, client_flag); diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 5c5e6d48..04453fad 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -86,6 +86,11 @@ class object, used to create cursors (keyword only) On Python 2, this option changes default value of `use_unicode` option from False to True. + :param str auth_plugin: + If supplied, the connection default authentication plugin will be + changed to this value. Example values: + `mysql_native_password` or `caching_sha2_password` + :param str sql_mode: If supplied, the session SQL mode will be changed to this setting. From 52d677b6c656582464efd922b0879010813fb191 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 6 Nov 2019 17:02:34 +0900 Subject: [PATCH 147/177] v1.4.5 --- HISTORY.rst | 9 +++++++++ metadata.cfg | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 7a30a0a2..df6dbe7d 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,3 +1,12 @@ +====================== + What's new in 1.4.5 +====================== + +Release: 2019-11-06 + +* The ``auth_plugin`` option is added. (#389) + + ====================== What's new in 1.4.4 ====================== diff --git a/metadata.cfg b/metadata.cfg index 96e665f7..36277f34 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.4.4 -version_info: (1,4,4,'final',0) +version: 1.4.5 +version_info: (1,4,5,'final',0) description: Python interface to MySQL author: Inada Naoki author_email: songofacandy@gmail.com From 6222ba69b0a547be7e0e93161d97ffd28af6e1a5 Mon Sep 17 00:00:00 2001 From: Bastien Vallet Date: Thu, 7 Nov 2019 16:36:40 +0100 Subject: [PATCH 148/177] [CI] Try to fix travis (#397) --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 76952ceb..e633d6b6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -40,7 +40,7 @@ after_success: matrix: include: - &django_py27 - name: "Django test (Python 2.7) + name: "Django test (Python 2.7)" python: "2.7" install: - pip install -U pip From 0da81656e9daa91da61cbdb0c29ab14b507f0c5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kul=C3=ADk?= Date: Thu, 7 Nov 2019 16:59:46 +0100 Subject: [PATCH 149/177] fix PyMemberDef type of two attributes (#395) --- MySQLdb/_mysql.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 6e4269e3..648c7284 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -2302,7 +2302,7 @@ static struct PyMemberDef _mysql_ConnectionObject_memberlist[] = { }, { "server_capabilities", - T_UINT, + T_ULONG, offsetof(_mysql_ConnectionObject,connection.server_capabilities), READONLY, "Capabilities of server; consult MySQLdb.constants.CLIENT" @@ -2316,7 +2316,7 @@ static struct PyMemberDef _mysql_ConnectionObject_memberlist[] = { }, { "client_flag", - T_UINT, + T_ULONG, offsetof(_mysql_ConnectionObject,connection.client_flag), READONLY, "Client flags; refer to MySQLdb.constants.CLIENT" From 9729f251a245978e1b0ac4dd24410ca4fb1992f9 Mon Sep 17 00:00:00 2001 From: Bastien Vallet Date: Fri, 8 Nov 2019 06:05:33 +0100 Subject: [PATCH 150/177] travis: Add Python 3.8 test. (#393) --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index e633d6b6..c8ee49a0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,13 +4,13 @@ language: python # See aws s3 ls s3://travis-python-archives/binaries/ubuntu/16.04/x86_64/ python: - "nightly" - - "pypy3.5" - - "pypy2.7-5.10.0" + - "pypy3" + - "pypy" + - "3.8" - "3.7" - "3.6" - "3.5" - "2.7" - - "3.8-dev" cache: pip From 6c67620bc65728ff269dbb7550fc88b18073a74d Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 18 Nov 2019 21:16:37 +0900 Subject: [PATCH 151/177] Try to fix RTD --- doc/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/conf.py b/doc/conf.py index b9f58bd7..fc7c089d 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -16,7 +16,7 @@ # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. -sys.path.insert(0, os.path.abspath('..')) +#sys.path.insert(0, os.path.abspath('..')) # -- General configuration ----------------------------------------------------- From edeab3040df660af7a02c4258bcbfdaf4af1d4b6 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 18 Nov 2019 21:22:39 +0900 Subject: [PATCH 152/177] Use cp1252 for latin1 charset (#398) --- MySQLdb/_mysql.c | 3 +++ MySQLdb/connections.py | 13 +++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 648c7284..64647bf3 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -217,6 +217,9 @@ _get_encoding(MYSQL *mysql) if (strncmp(utf8, cs.csname, 4) == 0) { // utf8, utf8mb3, utf8mb4 return utf8; } + else if (strncmp("latin1", cs.csname, 6) == 0) { + return "cp1252"; + } else if (strncmp("koi8r", cs.csname, 5) == 0) { return "koi8_r"; } diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 04453fad..4c33ec55 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -15,6 +15,14 @@ NotSupportedError, ProgrammingError, ) +# Mapping from MySQL charset name to Python codec name +_charset_to_encoding = { + "utf8mb4": "utf8", + "utf8mb3": "utf8", + "latin1": "cp1252", + "koi8r": "koi8_r", + "koi8u": "koi8_u", +} re_numeric_part = re.compile(r"^(\d+)") @@ -289,10 +297,7 @@ def set_character_set(self, charset): set can only be changed in MySQL-4.1 and newer. If you try to change the character set from the current value in an older version, NotSupportedError will be raised.""" - if charset in ("utf8mb4", "utf8mb3"): - py_charset = "utf8" - else: - py_charset = charset + py_charset = _charset_to_encoding.get(charset, charset) if self.character_set_name() != charset: try: super(Connection, self).set_character_set(charset) From b080cc2b75451206097895a3359ba4c8242b4481 Mon Sep 17 00:00:00 2001 From: jnozsc Date: Mon, 18 Nov 2019 17:27:50 -0800 Subject: [PATCH 153/177] homebrew migrates mysql-connector-c to mysql-client (#401) --- README.md | 29 +---------------------------- 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/README.md b/README.md index cd91383d..080d5fda 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ You may need to install the Python and MySQL development headers and libraries l * `sudo apt-get install python-dev default-libmysqlclient-dev` # Debian / Ubuntu * `sudo yum install python-devel mysql-devel` # Red Hat / CentOS -* `brew install mysql-connector-c` # macOS (Homebrew) (Currently, it has bug. See below) +* `brew install mysql-client` # macOS (Homebrew) On Windows, there are binary wheels you can install without MySQLConnector/C or MSVC. @@ -25,33 +25,6 @@ On Windows, there are binary wheels you can install without MySQLConnector/C or `sudo yum install python3-devel ` # Red Hat / CentOS -#### **Note about bug of MySQL Connector/C on macOS** - -See also: https://bugs.mysql.com/bug.php?id=86971 - -Versions of MySQL Connector/C may have incorrect default configuration options that cause compilation errors when `mysqlclient-python` is installed. (As of November 2017, this is known to be true for homebrew's `mysql-connector-c` and [official package](https://dev.mysql.com/downloads/connector/c/)) - -Modification of `mysql_config` resolves these issues as follows. - -Change - -``` -# on macOS, on or about line 112: -# Create options -libs="-L$pkglibdir" -libs="$libs -l " -``` - -to - -``` -# Create options -libs="-L$pkglibdir" -libs="$libs -lmysqlclient -lssl -lcrypto" -``` - -An improper ssl configuration may also create issues; see, e.g, `brew info openssl` for details on macOS. - ### Install from PyPI `pip install mysqlclient` From 713d3625f8db73ad4b6e7208b9fe4665a113d8af Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 19 Nov 2019 17:29:03 +0900 Subject: [PATCH 154/177] fixup docs --- Makefile | 5 +++++ MySQLdb/__init__.py | 4 ++-- doc/MySQLdb.rst | 17 +++++++++++++++++ doc/_exceptions.rst | 7 ------- doc/_mysql.rst | 7 ------- doc/modules.rst | 7 ------- doc/user_guide.rst | 2 +- 7 files changed, 25 insertions(+), 24 deletions(-) delete mode 100644 doc/_exceptions.rst delete mode 100644 doc/_mysql.rst delete mode 100644 doc/modules.rst diff --git a/Makefile b/Makefile index 491cc75e..8c8cddc8 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,11 @@ build: python3 setup.py build_ext -if +.PHONY: doc +doc: + pip install . + pip install sphinx + cd doc && make html .PHONY: clean clean: diff --git a/MySQLdb/__init__.py b/MySQLdb/__init__.py index d4c7fa56..cb1bd7c8 100644 --- a/MySQLdb/__init__.py +++ b/MySQLdb/__init__.py @@ -18,8 +18,8 @@ from . import _mysql if version_info != _mysql.version_info: - raise ImportError("this is MySQLdb version %s, but _mysql is version %r" % - (version_info, _mysql.version_info)) + raise ImportError("this is MySQLdb version %s, but _mysql is version %r\n_mysql: %r" % + (version_info, _mysql.version_info, _mysql.__file__)) threadsafety = 1 apilevel = "2.0" diff --git a/doc/MySQLdb.rst b/doc/MySQLdb.rst index ac690900..134a40b6 100644 --- a/doc/MySQLdb.rst +++ b/doc/MySQLdb.rst @@ -41,6 +41,23 @@ MySQLdb Package :undoc-members: :show-inheritance: +:mod:`_mysql` Module +-------------------- + +.. automodule:: MySQLdb._mysql + :members: + :undoc-members: + :show-inheritance: + +:mod:`_exceptions` Module +------------------------- + +.. automodule:: MySQLdb._exceptions + :members: + :undoc-members: + :show-inheritance: + + Subpackages ----------- diff --git a/doc/_exceptions.rst b/doc/_exceptions.rst deleted file mode 100644 index b509338c..00000000 --- a/doc/_exceptions.rst +++ /dev/null @@ -1,7 +0,0 @@ -_exceptions Module -======================== - -.. automodule:: MySQLdb._exceptions - :members: - :undoc-members: - :show-inheritance: diff --git a/doc/_mysql.rst b/doc/_mysql.rst deleted file mode 100644 index cf464b4f..00000000 --- a/doc/_mysql.rst +++ /dev/null @@ -1,7 +0,0 @@ -_mysql Module -============= - -.. automodule:: MySQLdb._mysql - :members: - :undoc-members: - :show-inheritance: diff --git a/doc/modules.rst b/doc/modules.rst deleted file mode 100644 index 998ac460..00000000 --- a/doc/modules.rst +++ /dev/null @@ -1,7 +0,0 @@ -MySQLdb -======= - -.. toctree:: - :maxdepth: 4 - - MySQLdb diff --git a/doc/user_guide.rst b/doc/user_guide.rst index 405b444e..0d317776 100644 --- a/doc/user_guide.rst +++ b/doc/user_guide.rst @@ -546,7 +546,7 @@ nextset() Some examples ............. -The ``connect()`` method works nearly the same as with `_mysql`_:: +The ``connect()`` method works nearly the same as with `MySQLDB._mysql`_:: import MySQLdb db=MySQLdb.connect(passwd="moonpie",db="thangs") From 17a75eae9f97efa1f71581052ad8f497103ed515 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Thu, 21 Nov 2019 21:06:22 +0900 Subject: [PATCH 155/177] v1.4.6 --- HISTORY.rst | 8 ++++++++ metadata.cfg | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index df6dbe7d..8c5399ad 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,3 +1,11 @@ +====================== + What's new in 1.4.6 +====================== + +Release: 2019-11-21 + +* The ``cp1252`` encoding is used when charset is "latin1". (#390) + ====================== What's new in 1.4.5 ====================== diff --git a/metadata.cfg b/metadata.cfg index 36277f34..0b9d55e8 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.4.5 -version_info: (1,4,5,'final',0) +version: 1.4.6 +version_info: (1,4,6,'final',0) description: Python interface to MySQL author: Inada Naoki author_email: songofacandy@gmail.com From 0482702d079f5e00b4a6e038ef13ee7849e70b78 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Thu, 28 Nov 2019 20:52:41 +0900 Subject: [PATCH 156/177] Update README --- README.md | 59 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 080d5fda..8b74dfcf 100644 --- a/README.md +++ b/README.md @@ -7,37 +7,60 @@ This is a fork of [MySQLdb1](https://github.com/farcepest/MySQLdb1). This project adds Python 3 support and bug fixes. I hope this fork is merged back to MySQLdb1 like distribute was merged back to setuptools. -## Install -### Prerequisites +## Support + +**Do Not use Github Issue Tracker to ask help. OSS Maintainer is not free tech support** + +When your question looks relating to Python rather than MySQL: + +* Python mailing list [python-list](https://mail.python.org/mailman/listinfo/python-list) +* Slack [pythondev.slack.com](https://pyslackers.com/web/slack) + +Or when you have question about MySQL: + +* [MySQL Community on Slack](https://lefred.be/mysql-community-on-slack/) + + +## Install -You may need to install the Python and MySQL development headers and libraries like so: +### Windows -* `sudo apt-get install python-dev default-libmysqlclient-dev` # Debian / Ubuntu -* `sudo yum install python-devel mysql-devel` # Red Hat / CentOS -* `brew install mysql-client` # macOS (Homebrew) +Building mysqlclient on Windows is very hard. +But there are some binary wheels you can install easily. -On Windows, there are binary wheels you can install without MySQLConnector/C or MSVC. +### macOS (Homebrew) -#### Note on Python 3 : if you are using python3 then you need to install python3-dev using the following command : +Install MySQL and mysqlclient: -`sudo apt-get install python3-dev` # debian / Ubuntu +``` +# Assume you are activating Python 3 venv +$ brew install mysql +$ pip install mysqlclient +``` -`sudo yum install python3-devel ` # Red Hat / CentOS +If you don't want to install MySQL server, you can use mysql-client instead: -### Install from PyPI +``` +# Assume you are activating Python 3 venv +$ brew install mysql-client +$ echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile +$ export PATH="/usr/local/opt/mysql-client/bin:$PATH" +$ pip install mysqlclient +``` -`pip install mysqlclient` +### Linux -NOTE: Wheels for Windows may be not released with source package. You should pin version -in your `requirements.txt` to avoid trying to install newest source package. +You may need to install the Python 3 and MySQL development headers and libraries like so: +* `$ sudo apt-get install python3-dev default-libmysqlclient-dev` # Debian / Ubuntu +* `% sudo yum install python3-devel mysql-devel` # Red Hat / CentOS -### Install from source +Then you can install mysqlclient via pip now: -1. Download source by `git clone` or [zipfile](https://github.com/PyMySQL/mysqlclient-python/archive/master.zip). -2. Customize `site.cfg` -3. `python setup.py install` +``` +$ pip install mysqlclient +``` ### Documentation From 5811678deacca6599030358254f28f779165d057 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Thu, 28 Nov 2019 21:21:35 +0900 Subject: [PATCH 157/177] Drop Python 2 support from Travis and setup (#405) --- .travis.yml | 13 +++---------- metadata.cfg | 6 ++---- setup.py | 1 + setup_common.py | 12 +++--------- setup_posix.py | 5 +---- 5 files changed, 10 insertions(+), 27 deletions(-) diff --git a/.travis.yml b/.travis.yml index c8ee49a0..f838948b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,12 +5,10 @@ language: python python: - "nightly" - "pypy3" - - "pypy" - "3.8" - "3.7" - "3.6" - "3.5" - - "2.7" cache: pip @@ -39,9 +37,9 @@ after_success: matrix: include: - - &django_py27 - name: "Django test (Python 2.7)" - python: "2.7" + - &django_py3 + name: "Django 1.11 test (Python 3.7)" + python: "3.7" install: - pip install -U pip - wget https://github.com/django/django/archive/1.11.18.tar.gz @@ -59,9 +57,4 @@ matrix: - cd django-1.11.18/tests/ - ./runtests.py --parallel=1 --settings=test_mysql - - &django_py3 - <<: *django_py27 - name: "Django test (Python 3.7)" - python: "3.7" - # vim: sw=2 ts=2 sts=2 diff --git a/metadata.cfg b/metadata.cfg index 0b9d55e8..cafa3496 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -1,6 +1,6 @@ [metadata] -version: 1.4.6 -version_info: (1,4,6,'final',0) +version: 2.0.0dev1 +version_info: (2,0,0,'dev',1) description: Python interface to MySQL author: Inada Naoki author_email: songofacandy@gmail.com @@ -19,8 +19,6 @@ classifiers: Operating System :: Unix Programming Language :: C Programming Language :: Python - Programming Language :: Python :: 2 - Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 diff --git a/setup.py b/setup.py index d1029962..a39e0d12 100644 --- a/setup.py +++ b/setup.py @@ -19,4 +19,5 @@ ] metadata['long_description'] = readme metadata['long_description_content_type'] = "text/markdown" +metadata['python_requires'] = '>=3.5' setuptools.setup(**metadata) diff --git a/setup_common.py b/setup_common.py index 03c39bb7..2274e3a0 100644 --- a/setup_common.py +++ b/setup_common.py @@ -1,9 +1,4 @@ -try: - # Python 2.x - from ConfigParser import SafeConfigParser -except ImportError: - # Python 3.x - from configparser import ConfigParser as SafeConfigParser +from configparser import ConfigParser as SafeConfigParser def get_metadata_and_options(): config = SafeConfigParser() @@ -28,10 +23,9 @@ def enabled(options, option): raise ValueError("Unknown value %s for option %s" % (value, option)) def create_release_file(metadata): - rel = open("MySQLdb/release.py",'w') - rel.write(""" + with open("MySQLdb/release.py",'w') as rel: + rel.write(""" __author__ = "%(author)s <%(author_email)s>" version_info = %(version_info)s __version__ = "%(version)s" """ % metadata) - rel.close() diff --git a/setup_posix.py b/setup_posix.py index c65c045f..dd88fec6 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -1,8 +1,5 @@ import os, sys -try: - from ConfigParser import SafeConfigParser -except ImportError: - from configparser import ConfigParser as SafeConfigParser +from configparser import ConfigParser as SafeConfigParser # This dequote() business is required for some older versions # of mysql_config From 026e87f7b567f9c613a46a9be671c3fe2c76a3b8 Mon Sep 17 00:00:00 2001 From: Bastien Vallet Date: Thu, 28 Nov 2019 14:04:42 +0100 Subject: [PATCH 158/177] Remove py27 support from Python sources. (#394) --- INSTALL.rst | 2 +- MySQLdb/__init__.py | 9 ++------- MySQLdb/_exceptions.py | 4 +--- MySQLdb/compat.py | 14 -------------- MySQLdb/connections.py | 27 +++++++-------------------- MySQLdb/converters.py | 4 +--- MySQLdb/cursors.py | 16 +++++++--------- metadata.cfg | 1 - tests/capabilities.py | 6 ++---- tests/test_MySQLdb_capabilities.py | 1 - tests/test_MySQLdb_nonstandard.py | 4 ++-- 11 files changed, 23 insertions(+), 65 deletions(-) delete mode 100644 MySQLdb/compat.py diff --git a/INSTALL.rst b/INSTALL.rst index f5be3f4e..0b49f3e6 100644 --- a/INSTALL.rst +++ b/INSTALL.rst @@ -8,7 +8,7 @@ MySQLdb Installation Prerequisites ------------- -+ Python 2.7, 3.5 or higher ++ Python 3.5 or higher + setuptools diff --git a/MySQLdb/__init__.py b/MySQLdb/__init__.py index cb1bd7c8..fbb5e41a 100644 --- a/MySQLdb/__init__.py +++ b/MySQLdb/__init__.py @@ -26,7 +26,6 @@ paramstyle = "format" from ._mysql import * -from MySQLdb.compat import PY2 from MySQLdb.constants import FIELD_TYPE from MySQLdb.times import Date, Time, Timestamp, \ DateFromTicks, TimeFromTicks, TimestampFromTicks @@ -71,12 +70,8 @@ def test_DBAPISet_set_equality_membership(): def test_DBAPISet_set_inequality_membership(): assert FIELD_TYPE.DATE != STRING -if PY2: - def Binary(x): - return bytearray(x) -else: - def Binary(x): - return bytes(x) +def Binary(x): + return bytes(x) def Connect(*args, **kwargs): """Factory function for connections.Connection.""" diff --git a/MySQLdb/_exceptions.py b/MySQLdb/_exceptions.py index 0f14f3bd..9cfff57f 100644 --- a/MySQLdb/_exceptions.py +++ b/MySQLdb/_exceptions.py @@ -4,10 +4,8 @@ https://www.python.org/dev/peps/pep-0249/ """ -from .compat import StandardError - -class MySQLError(StandardError): +class MySQLError(Exception): """Exception related to operation with MySQL.""" diff --git a/MySQLdb/compat.py b/MySQLdb/compat.py deleted file mode 100644 index f8d98ac2..00000000 --- a/MySQLdb/compat.py +++ /dev/null @@ -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 diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 4c33ec55..11b996d4 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -8,7 +8,6 @@ import sys from MySQLdb import cursors, _mysql -from MySQLdb.compat import unicode, PY2 from MySQLdb._exceptions import ( Warning, Error, InterfaceError, DataError, DatabaseError, OperationalError, IntegrityError, InternalError, @@ -85,14 +84,11 @@ class object, used to create cursors (keyword only) columns are returned as bytes. Unicode objects will always be encoded to the connection's character set regardless of this setting. - Default to False on Python 2 and True on Python 3 - so that you can always get python `str` object by default. + Default to True. :param str charset: If supplied, the connection character set will be changed to this character set. - On Python 2, this option changes default value of `use_unicode` - option from False to True. :param str auth_plugin: If supplied, the connection default authentication plugin will be @@ -154,13 +150,7 @@ class object, used to create cursors (keyword only) cursorclass = kwargs2.pop('cursorclass', self.default_cursor) charset = kwargs2.get('charset', '') - - if charset or not PY2: - use_unicode = True - else: - use_unicode = False - - use_unicode = kwargs2.pop('use_unicode', use_unicode) + use_unicode = kwargs2.pop('use_unicode', True) sql_mode = kwargs2.pop('sql_mode', '') self._binary_prefix = kwargs2.pop('binary_prefix', False) @@ -209,9 +199,9 @@ def unicode_literal(u, dummy=None): self.converter[t] = _bytes_or_str # Unlike other string/blob types, JSON is always text. # 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 if self._transactional: if autocommit is not None: @@ -256,20 +246,17 @@ def literal(self, o): Non-standard. For internal use; do not use this in your applications. """ - if isinstance(o, unicode): + if isinstance(o, str): s = self.string_literal(o.encode(self.encoding)) elif isinstance(o, bytearray): s = self._bytes_literal(o) elif isinstance(o, bytes): - if PY2: - s = self.string_literal(o) - else: - s = self._bytes_literal(o) + s = self._bytes_literal(o) elif isinstance(o, (tuple, list)): s = self._tuple_literal(o) else: s = self.escape(o, self.encoders) - if isinstance(s, unicode): + if isinstance(s, str): s = s.encode(self.encoding) assert isinstance(s, bytes) return s diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index 645e3780..c460fbd4 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -35,7 +35,6 @@ from MySQLdb._mysql import string_literal, escape from MySQLdb.constants import FIELD_TYPE, FLAG from MySQLdb.times import * -from MySQLdb.compat import PY2, long, unicode from MySQLdb._exceptions import ProgrammingError NoneType = type(None) @@ -85,11 +84,10 @@ def array2Str(o, d): return Thing2Literal(o.tostring(), d) # bytes or str regarding to BINARY_FLAG. -_bytes_or_str = ((FLAG.BINARY, bytes), (None, unicode)) +_bytes_or_str = ((FLAG.BINARY, bytes), (None, str)) conversions = { int: Thing2Str, - long: Thing2Str, float: Float2Str, NoneType: None2NULL, ArrayType: array2Str, diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index ee834e45..056bb542 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -3,12 +3,10 @@ This module implements Cursors of various types for MySQLdb. By default, MySQLdb uses the Cursor class. """ -from __future__ import print_function, absolute_import from functools import partial import re import sys -from .compat import unicode from ._exceptions import ( Warning, Error, InterfaceError, DataError, DatabaseError, OperationalError, IntegrityError, InternalError, @@ -101,7 +99,7 @@ def _escape_args(self, args, conn): literal = conn.literal def ensure_bytes(x): - if isinstance(x, unicode): + if isinstance(x, str): return x.encode(encoding) elif isinstance(x, tuple): return tuple(map(ensure_bytes, x)) @@ -187,14 +185,14 @@ def execute(self, query, args=None): pass db = self._get_db() - if isinstance(query, unicode): + if isinstance(query, str): query = query.encode(db.encoding) if args is not None: if isinstance(args, dict): nargs = {} for key, item in args.items(): - if isinstance(key, unicode): + if isinstance(key, str): key = key.encode(db.encoding) nargs[key] = db.literal(item) args = nargs @@ -242,11 +240,11 @@ def executemany(self, query, args): def _do_execute_many(self, prefix, values, postfix, args, max_stmt_length, encoding): conn = self._get_db() escape = self._escape_args - if isinstance(prefix, unicode): + if isinstance(prefix, str): prefix = prefix.encode(encoding) - if isinstance(values, unicode): + if isinstance(values, str): values = values.encode(encoding) - if isinstance(postfix, unicode): + if isinstance(postfix, str): postfix = postfix.encode(encoding) sql = bytearray(prefix) args = iter(args) @@ -294,7 +292,7 @@ def callproc(self, procname, args=()): disconnected. """ db = self._get_db() - if isinstance(procname, unicode): + if isinstance(procname, str): procname = procname.encode(db.encoding) if args: fmt = b'@_' + procname + b'_%d=%s' diff --git a/metadata.cfg b/metadata.cfg index cafa3496..bae8bd44 100644 --- a/metadata.cfg +++ b/metadata.cfg @@ -28,7 +28,6 @@ classifiers: Topic :: Database :: Database Engines/Servers py_modules: MySQLdb._exceptions - MySQLdb.compat MySQLdb.connections MySQLdb.converters MySQLdb.cursors diff --git a/tests/capabilities.py b/tests/capabilities.py index 5d913790..15db5336 100644 --- a/tests/capabilities.py +++ b/tests/capabilities.py @@ -10,8 +10,6 @@ import unittest from configdb import connection_factory -from MySQLdb.compat import unichr - class DatabaseTest(unittest.TestCase): @@ -27,8 +25,8 @@ def setUp(self): db = connection_factory(**self.connect_kwargs) self.connection = db self.cursor = db.cursor() - self.BLOBUText = u''.join([unichr(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.BLOBUText = u''.join([chr(i) for i in range(16384)]) + self.BLOBBinary = self.db_module.Binary((u''.join([chr(i) for i in range(256)] * 16)).encode('latin1')) leak_test = True diff --git a/tests/test_MySQLdb_capabilities.py b/tests/test_MySQLdb_capabilities.py index 6e39d146..d5be511f 100644 --- a/tests/test_MySQLdb_capabilities.py +++ b/tests/test_MySQLdb_capabilities.py @@ -5,7 +5,6 @@ from contextlib import closing import unittest import MySQLdb -from MySQLdb.compat import unicode from MySQLdb import cursors from configdb import connection_factory import warnings diff --git a/tests/test_MySQLdb_nonstandard.py b/tests/test_MySQLdb_nonstandard.py index ea05079f..fa4692ef 100644 --- a/tests/test_MySQLdb_nonstandard.py +++ b/tests/test_MySQLdb_nonstandard.py @@ -90,9 +90,9 @@ def test_client_flag(self): use_unicode=True, 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) - with self.assertRaises(TypeError if MySQLdb.compat.PY2 else AttributeError): + with self.assertRaises(AttributeError): conn.client_flag = 0 conn.close() From 86491882e669e3bcdeb1bb7dfdd22d72b76673c7 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 29 Nov 2019 14:24:28 +0900 Subject: [PATCH 159/177] Drop Python 2 support from C source (#406) --- MySQLdb/_mysql.c | 126 +++++++++++++++-------------------------------- 1 file changed, 39 insertions(+), 87 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 64647bf3..9a7e25f7 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -36,14 +36,9 @@ PERFORMANCE OF THIS SOFTWARE. #define PY_SSIZE_T_CLEAN 1 #include "Python.h" -#if PY_MAJOR_VERSION >= 3 -#define IS_PY3K -#define PyInt_Type PyLong_Type -#define PyInt_FromString PyLong_FromString -#define PyInt_FromLong(n) PyLong_FromLong(n) -#define PyInt_Check(n) PyLong_Check(n) -#define PyInt_AS_LONG(n) PyLong_AS_LONG(n) -#define PyString_FromString(s) PyUnicode_FromString(s) + +#if PY_MAJOR_VERSION == 2 +#error "Python 2 is not supported" #endif #include "bytesobject.h" @@ -200,8 +195,8 @@ _mysql_Exception(_mysql_ConnectionObject *c) e = _mysql_OperationalError; break; } - PyTuple_SET_ITEM(t, 0, PyInt_FromLong((long)merr)); - PyTuple_SET_ITEM(t, 1, PyString_FromString(mysql_error(&(c->connection)))); + PyTuple_SET_ITEM(t, 0, PyLong_FromLong((long)merr)); + PyTuple_SET_ITEM(t, 1, PyUnicode_FromString(mysql_error(&(c->connection)))); PyErr_SetObject(e, t); Py_DECREF(t); return NULL; @@ -286,7 +281,7 @@ _mysql_ResultObject_Initialize( fields = mysql_fetch_fields(result); for (i=0; iconnection)); if (ret == (my_ulonglong)-1) - return PyInt_FromLong(-1); + return PyLong_FromLong(-1); return PyLong_FromUnsignedLongLong(ret); } @@ -790,7 +779,7 @@ _mysql_ConnectionObject_next_result( err = mysql_next_result(&(self->connection)); Py_END_ALLOW_THREADS if (err > 0) return _mysql_Exception(self); - return PyInt_FromLong(err); + return PyLong_FromLong(err); } @@ -813,7 +802,7 @@ _mysql_ConnectionObject_set_server_option( err = mysql_set_server_option(&(self->connection), flags); Py_END_ALLOW_THREADS if (err) return _mysql_Exception(self); - return PyInt_FromLong(err); + return PyLong_FromLong(err); } static char _mysql_ConnectionObject_sqlstate__doc__[] = @@ -834,7 +823,7 @@ _mysql_ConnectionObject_sqlstate( PyObject *noargs) { check_connection(self); - return PyString_FromString(mysql_sqlstate(&(self->connection))); + return PyUnicode_FromString(mysql_sqlstate(&(self->connection))); } static char _mysql_ConnectionObject_warning_count__doc__[] = @@ -849,7 +838,7 @@ _mysql_ConnectionObject_warning_count( PyObject *noargs) { check_connection(self); - return PyInt_FromLong(mysql_warning_count(&(self->connection))); + return PyLong_FromLong(mysql_warning_count(&(self->connection))); } static char _mysql_ConnectionObject_errno__doc__[] = @@ -864,7 +853,7 @@ _mysql_ConnectionObject_errno( PyObject *noargs) { check_connection(self); - return PyInt_FromLong((long)mysql_errno(&(self->connection))); + return PyLong_FromLong((long)mysql_errno(&(self->connection))); } static char _mysql_ConnectionObject_error__doc__[] = @@ -879,7 +868,7 @@ _mysql_ConnectionObject_error( PyObject *noargs) { check_connection(self); - return PyString_FromString(mysql_error(&(self->connection))); + return PyUnicode_FromString(mysql_error(&(self->connection))); } static char _mysql_escape_string__doc__[] = @@ -948,14 +937,12 @@ _mysql_string_literal( } else { s = PyObject_Str(o); if (!s) return NULL; -#ifdef IS_PY3K { PyObject *t = PyUnicode_AsASCIIString(s); Py_DECREF(s); if (!t) return NULL; s = t; } -#endif } in = PyBytes_AsString(s); size = PyBytes_GET_SIZE(s); @@ -1055,7 +1042,6 @@ _mysql_ResultObject_describe( if (!(d = PyTuple_New(n))) return NULL; for (i=0; iencoding == utf8) { name = PyUnicode_DecodeUTF8(fields[i].name, fields[i].name_length, "replace"); @@ -1068,10 +1054,6 @@ _mysql_ResultObject_describe( t = Py_BuildValue("(Niiiiii)", name, -#else - t = Py_BuildValue("(siiiiii)", - fields[i].name, -#endif (long) fields[i].type, (long) fields[i].max_length, (long) fields[i].length, @@ -1105,7 +1087,7 @@ _mysql_ResultObject_field_flags( if (!(d = PyTuple_New(n))) return NULL; for (i=0; itype) { case FIELD_TYPE_TINY_BLOB: @@ -1174,10 +1155,6 @@ _mysql_field_to_python( return PyObject_CallFunction(converter, binary ? "y#" : "s#", rowitem, (Py_ssize_t)length); -#else - return PyObject_CallFunction(converter, - "s#", rowitem, (Py_ssize_t)length); -#endif } static PyObject * @@ -1444,7 +1421,7 @@ _mysql_ConnectionObject_character_set_name( const char *s; check_connection(self); s = mysql_character_set_name(&(self->connection)); - return PyString_FromString(s); + return PyUnicode_FromString(s); } static char _mysql_ConnectionObject_set_character_set__doc__[] = @@ -1502,15 +1479,15 @@ _mysql_ConnectionObject_get_character_set_info( mysql_get_character_set_info(&(self->connection), &cs); if (!(result = PyDict_New())) return NULL; if (cs.csname) - PyDict_SetItemString(result, "name", PyString_FromString(cs.csname)); + PyDict_SetItemString(result, "name", PyUnicode_FromString(cs.csname)); if (cs.name) - PyDict_SetItemString(result, "collation", PyString_FromString(cs.name)); + PyDict_SetItemString(result, "collation", PyUnicode_FromString(cs.name)); if (cs.comment) - PyDict_SetItemString(result, "comment", PyString_FromString(cs.comment)); + PyDict_SetItemString(result, "comment", PyUnicode_FromString(cs.comment)); if (cs.dir) - PyDict_SetItemString(result, "dir", PyString_FromString(cs.dir)); - PyDict_SetItemString(result, "mbminlen", PyInt_FromLong(cs.mbminlen)); - PyDict_SetItemString(result, "mbmaxlen", PyInt_FromLong(cs.mbmaxlen)); + PyDict_SetItemString(result, "dir", PyUnicode_FromString(cs.dir)); + PyDict_SetItemString(result, "mbminlen", PyLong_FromLong(cs.mbminlen)); + PyDict_SetItemString(result, "mbmaxlen", PyLong_FromLong(cs.mbmaxlen)); return result; } #endif @@ -1545,7 +1522,7 @@ _mysql_get_client_info( PyObject *self, PyObject *noargs) { - return PyString_FromString(mysql_get_client_info()); + return PyUnicode_FromString(mysql_get_client_info()); } static char _mysql_ConnectionObject_get_host_info__doc__[] = @@ -1559,7 +1536,7 @@ _mysql_ConnectionObject_get_host_info( PyObject *noargs) { check_connection(self); - return PyString_FromString(mysql_get_host_info(&(self->connection))); + return PyUnicode_FromString(mysql_get_host_info(&(self->connection))); } static char _mysql_ConnectionObject_get_proto_info__doc__[] = @@ -1573,7 +1550,7 @@ _mysql_ConnectionObject_get_proto_info( PyObject *noargs) { check_connection(self); - return PyInt_FromLong((long)mysql_get_proto_info(&(self->connection))); + return PyLong_FromLong((long)mysql_get_proto_info(&(self->connection))); } static char _mysql_ConnectionObject_get_server_info__doc__[] = @@ -1587,7 +1564,7 @@ _mysql_ConnectionObject_get_server_info( PyObject *noargs) { check_connection(self); - return PyString_FromString(mysql_get_server_info(&(self->connection))); + return PyUnicode_FromString(mysql_get_server_info(&(self->connection))); } static char _mysql_ConnectionObject_info__doc__[] = @@ -1604,7 +1581,7 @@ _mysql_ConnectionObject_info( const char *s; check_connection(self); s = mysql_info(&(self->connection)); - if (s) return PyString_FromString(s); + if (s) return PyUnicode_FromString(s); Py_RETURN_NONE; } @@ -1674,7 +1651,7 @@ _mysql_ConnectionObject_field_count( PyObject *noargs) { check_connection(self); - return PyInt_FromLong((long)mysql_field_count(&(self->connection))); + return PyLong_FromLong((long)mysql_field_count(&(self->connection))); } static char _mysql_ConnectionObject_fileno__doc__[] = @@ -1688,7 +1665,7 @@ _mysql_ConnectionObject_fileno( PyObject *noargs) { check_connection(self); - return PyInt_FromLong(self->connection.net.fd); + return PyLong_FromLong(self->connection.net.fd); } static char _mysql_ResultObject_num_fields__doc__[] = @@ -1700,7 +1677,7 @@ _mysql_ResultObject_num_fields( PyObject *noargs) { check_result_connection(self); - return PyInt_FromLong((long)mysql_num_fields(self->result)); + return PyLong_FromLong((long)mysql_num_fields(self->result)); } static char _mysql_ResultObject_num_rows__doc__[] = @@ -1889,7 +1866,7 @@ _mysql_ConnectionObject_stat( s = mysql_stat(&(self->connection)); Py_END_ALLOW_THREADS if (!s) return _mysql_Exception(self); - return PyString_FromString(s); + return PyUnicode_FromString(s); } static char _mysql_ConnectionObject_store_result__doc__[] = @@ -1950,7 +1927,7 @@ _mysql_ConnectionObject_thread_id( Py_BEGIN_ALLOW_THREADS pid = mysql_thread_id(&(self->connection)); Py_END_ALLOW_THREADS - return PyInt_FromLong((long)pid); + return PyLong_FromLong((long)pid); } static char _mysql_ConnectionObject_use_result__doc__[] = @@ -2013,7 +1990,7 @@ _mysql_ConnectionObject_repr( self->connection.host, self); else snprintf(buf, 300, "<_mysql.connection closed at %p>", self); - return PyString_FromString(buf); + return PyUnicode_FromString(buf); } static char _mysql_ResultObject_data_seek__doc__[] = @@ -2046,7 +2023,7 @@ _mysql_ResultObject_repr( { char buf[300]; snprintf(buf, 300, "<_mysql.result object at %p>", self); - return PyString_FromString(buf); + return PyUnicode_FromString(buf); } static PyMethodDef _mysql_ConnectionObject_methods[] = { @@ -2391,13 +2368,9 @@ _mysql_ConnectionObject_getattro( PyObject *name) { const char *cname; -#ifdef IS_PY3K cname = PyUnicode_AsUTF8(name); -#else - cname = PyString_AsString(name); -#endif if (strcmp(cname, "closed") == 0) - return PyInt_FromLong((long)!(self->open)); + return PyLong_FromLong((long)!(self->open)); return PyObject_GenericGetAttr((PyObject *)self, name); } @@ -2638,7 +2611,6 @@ an argument are now methods of the result object. Deprecated functions\n\ (as of 3.23) are NOT implemented.\n\ "; -#ifdef IS_PY3K static struct PyModuleDef _mysqlmodule = { PyModuleDef_HEAD_INIT, "_mysql", /* name of module */ @@ -2650,23 +2622,14 @@ static struct PyModuleDef _mysqlmodule = { PyMODINIT_FUNC PyInit__mysql(void) -#else -DL_EXPORT(void) -init_mysql(void) -#endif { PyObject *dict, *module, *emod, *edict; if (mysql_library_init(0, NULL, NULL)) { PyErr_SetString(PyExc_ImportError, "_mysql: mysql_library_init failed"); -#ifdef IS_PY3K return NULL; -#else - return; -#endif } -#ifdef IS_PY3K if (PyType_Ready(&_mysql_ConnectionObject_Type) < 0) return NULL; if (PyType_Ready(&_mysql_ResultObject_Type) < 0) @@ -2674,15 +2637,6 @@ init_mysql(void) module = PyModule_Create(&_mysqlmodule); if (!module) return module; /* this really should never happen */ -#else - if (PyType_Ready(&_mysql_ConnectionObject_Type) < 0) - return; - if (PyType_Ready(&_mysql_ResultObject_Type) < 0) - return; - module = Py_InitModule4("_mysql", _mysql_methods, _mysql___doc__, - (PyObject *)NULL, PYTHON_API_VERSION); - if (!module) return; /* this really should never happen */ -#endif if (!(dict = PyModule_GetDict(module))) goto error; if (PyDict_SetItemString(dict, "version_info", @@ -2690,7 +2644,7 @@ init_mysql(void) dict, dict))) goto error; if (PyDict_SetItemString(dict, "__version__", - PyString_FromString(QUOTE(__version__)))) + PyUnicode_FromString(QUOTE(__version__)))) goto error; if (PyDict_SetItemString(dict, "connection", (PyObject *)&_mysql_ConnectionObject_Type)) @@ -2744,9 +2698,7 @@ init_mysql(void) PyErr_SetString(PyExc_ImportError, "_mysql: init failed"); module = NULL; } -#ifdef IS_PY3K return module; -#endif } /* vim: set ts=4 sts=4 sw=4 expandtab : */ From 768ae5e2531c9109db8e4932f47094b244c7e073 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 29 Nov 2019 15:22:18 +0900 Subject: [PATCH 160/177] travis: Add test for Django 2.2 (#407) --- .travis.yml | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index f838948b..4d3e0108 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,15 +37,17 @@ after_success: matrix: include: - - &django_py3 + - &django_1_11 name: "Django 1.11 test (Python 3.7)" - python: "3.7" + env: + - DJANGO_VERSION=1.11.26 + python: "3.5" install: - pip install -U pip - - wget https://github.com/django/django/archive/1.11.18.tar.gz - - tar xf 1.11.18.tar.gz - - pip install django-1.11.18/ - - cp ci/test_mysql.py django-1.11.18/tests/ + - wget https://github.com/django/django/archive/${DJANGO_VERSION}.tar.gz + - tar xf ${DJANGO_VERSION}.tar.gz + - pip install django-${DJANGO_VERSION}/ + - cp ci/test_mysql.py django-${DJANGO_VERSION}/tests/ - pip install . before_script: @@ -54,7 +56,14 @@ matrix: - mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql script: - - cd django-1.11.18/tests/ + - cd django-${DJANGO_VERSION}/tests/ - ./runtests.py --parallel=1 --settings=test_mysql + - &django_2_2 + <<: *django_py27 + name: "Django 2.2 test (Python 3.8)" + python: "3.8" + env: + - DJANGO_VERSION=2.2.7 + # vim: sw=2 ts=2 sts=2 From 4a48401c75303fa04421d0fa3ae8ded529103f49 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 29 Nov 2019 17:03:36 +0900 Subject: [PATCH 161/177] Remove -lcrypto -lssl -lz (#409) --- setup_posix.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/setup_posix.py b/setup_posix.py index dd88fec6..db82b3c6 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -90,6 +90,12 @@ def get_config(): extra_objects.append(os.path.join(library_dirs[0], 'lib%s.a' % client)) if client in libraries: libraries.remove(client) + else: + # mysql_config may have "-lmysqlclient -lz -lssl -lcrypto", but zlib and + # ssl is not used by _mysql. They are needed only for static build. + for L in ('crypto', 'ssl', 'z'): + if L in libraries: + libraries.remove(L) name = "mysqlclient" metadata['name'] = name From 3fb4eb1b11e3e94581020c752981b75518c21c21 Mon Sep 17 00:00:00 2001 From: Jason Fried Date: Fri, 29 Nov 2019 00:07:05 -0800 Subject: [PATCH 162/177] Use binary type by default for converter input. (#351) Before *Most* string fields were passed to custom converter functions as "bytes", everything else was assumed to be valid encoded utf-8 and passed as str. This could cause issues where some unknown field was not valid utf-8 and a decode error is triggered where it can't be fixed or avoided by the end user. Instead we should short list those fields that we know for sure have to be str type because the existing converters expect it to be that way. So all the date conversions, and (NEW)DECIMAL since the python decimal type will not accept bytes object. Everything else stays bytes, which is the least suprising and safest thing to do. --- MySQLdb/_mysql.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 9a7e25f7..f396561f 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -1132,25 +1132,16 @@ _mysql_field_to_python( //fprintf(stderr, "\n"); int binary; switch (field->type) { - case FIELD_TYPE_TINY_BLOB: - case FIELD_TYPE_MEDIUM_BLOB: - case FIELD_TYPE_LONG_BLOB: - case FIELD_TYPE_BLOB: - case FIELD_TYPE_VAR_STRING: - case FIELD_TYPE_STRING: - case FIELD_TYPE_GEOMETRY: - case FIELD_TYPE_BIT: -#ifdef FIELD_TYPE_JSON - case FIELD_TYPE_JSON: -#else - case 245: // JSON -#endif - // Call converter with bytes - binary = 1; + case FIELD_TYPE_DECIMAL: + case FIELD_TYPE_NEWDECIMAL: + case FIELD_TYPE_TIMESTAMP: + case FIELD_TYPE_DATETIME: + case FIELD_TYPE_TIME: + case FIELD_TYPE_DATE: + binary = 0; // pass str, because these converters expect it break; - default: // e.g. FIELD_TYPE_DATETIME, etc. - // Call converter with unicode string - binary = 0; + default: // Default to just passing bytes + binary = 1; } return PyObject_CallFunction(converter, binary ? "y#" : "s#", From 82301a301ae71f27b78d0f5525359553ee497de4 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 29 Nov 2019 17:32:39 +0900 Subject: [PATCH 163/177] Drop Django 1.11 support (#411) --- .travis.yml | 16 +++++++--------- MySQLdb/cursors.py | 7 ------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4d3e0108..865c00f9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,10 +37,10 @@ after_success: matrix: include: - - &django_1_11 - name: "Django 1.11 test (Python 3.7)" + - &django_2_2 + name: "Django 2.2 test (Python 3.5)" env: - - DJANGO_VERSION=1.11.26 + - DJANGO_VERSION=2.2.7 python: "3.5" install: - pip install -U pip @@ -59,11 +59,9 @@ matrix: - cd django-${DJANGO_VERSION}/tests/ - ./runtests.py --parallel=1 --settings=test_mysql - - &django_2_2 - <<: *django_py27 - name: "Django 2.2 test (Python 3.8)" - python: "3.8" - env: - - DJANGO_VERSION=2.2.7 + #- &django_3_0 + # <<: *django_2_2 + # name: "Django 3.0 test (Python 3.8)" + # python: "3.8" # vim: sw=2 ts=2 sts=2 diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 056bb542..c70d0f3d 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -62,13 +62,6 @@ def __init__(self, connection): self.arraysize = 1 self._executed = None - # XXX THIS IS GARBAGE: While this is totally garbage and private, - # Django 1.11 depends on it. And they don't fix it because - # they are in security-only fix mode. - # So keep this garbage for now. This will be removed in 1.5. - # See PyMySQL/mysqlclient-python#303 - self._last_executed = None - self.lastrowid = None self.messages = [] self._result = None From 00016e096147a9d5c1727bf1ea4a7d17f64ec4ef Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 29 Nov 2019 17:33:06 +0900 Subject: [PATCH 164/177] Fix some flake8 errors (#410) --- MySQLdb/connections.py | 5 ++--- MySQLdb/cursors.py | 7 +------ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 11b996d4..27a04770 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -5,10 +5,9 @@ override Connection.default_cursor with a non-standard Cursor class. """ import re -import sys -from MySQLdb import cursors, _mysql -from MySQLdb._exceptions import ( +from . import cursors, _mysql +from ._exceptions import ( Warning, Error, InterfaceError, DataError, DatabaseError, OperationalError, IntegrityError, InternalError, NotSupportedError, ProgrammingError, diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index c70d0f3d..9d1bda29 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -3,14 +3,9 @@ This module implements Cursors of various types for MySQLdb. By default, MySQLdb uses the Cursor class. """ -from functools import partial import re -import sys -from ._exceptions import ( - Warning, Error, InterfaceError, DataError, - DatabaseError, OperationalError, IntegrityError, InternalError, - NotSupportedError, ProgrammingError) +from ._exceptions import ProgrammingError #: Regular expression for :meth:`Cursor.executemany`. From 8fd628ef0ff57b37eeb6ac021521c1d020f62781 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 29 Nov 2019 18:33:27 +0900 Subject: [PATCH 165/177] travis: Fix Django test (#412) --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 865c00f9..22041b24 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,7 +38,7 @@ after_success: matrix: include: - &django_2_2 - name: "Django 2.2 test (Python 3.5)" + name: "Django 2.2 test" env: - DJANGO_VERSION=2.2.7 python: "3.5" @@ -46,7 +46,7 @@ matrix: - pip install -U pip - wget https://github.com/django/django/archive/${DJANGO_VERSION}.tar.gz - tar xf ${DJANGO_VERSION}.tar.gz - - pip install django-${DJANGO_VERSION}/ + - pip install -e django-${DJANGO_VERSION}/ - cp ci/test_mysql.py django-${DJANGO_VERSION}/tests/ - pip install . From 643220972e0164aaeaf018220ff738fb0e31bfa8 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 29 Nov 2019 21:15:17 +0900 Subject: [PATCH 166/177] Add Context Manager Interface to Connection. (#413) Fixes #400. --- MySQLdb/connections.py | 6 ++++++ tests/test_MySQLdb_nonstandard.py | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 27a04770..020b4a99 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -207,6 +207,12 @@ def unicode_literal(u, dummy=None): self.autocommit(autocommit) self.messages = [] + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + self.close() + def autocommit(self, on): on = bool(on) if self.get_autocommit() != on: diff --git a/tests/test_MySQLdb_nonstandard.py b/tests/test_MySQLdb_nonstandard.py index fa4692ef..c5cacbec 100644 --- a/tests/test_MySQLdb_nonstandard.py +++ b/tests/test_MySQLdb_nonstandard.py @@ -99,3 +99,8 @@ def test_client_flag(self): def test_fileno(self): self.assertGreaterEqual(self.conn.fileno(), 0) + + def test_context_manager(self): + with connection_factory() as conn: + self.assertFalse(conn.closed) + self.assertTrue(conn.closed) From eebe7e9b987b70b90ec974153bdb09bd07ab7782 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 29 Nov 2019 21:37:20 +0900 Subject: [PATCH 167/177] travis: fast_finish: true --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 22041b24..df95e89d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,7 +35,8 @@ script: after_success: - codecov -matrix: +jobs: + fast_finish: true include: - &django_2_2 name: "Django 2.2 test" From 3a700b29407679a91e5a24fc86a669c4386595d7 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 29 Nov 2019 21:39:53 +0900 Subject: [PATCH 168/177] Update HISTORY --- HISTORY.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/HISTORY.rst b/HISTORY.rst index 8c5399ad..25678e55 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,3 +1,14 @@ +====================== + What's new in 2.0.0 +====================== + +Release: TBD + +* Dropped Python 2 support +* Dropped Django 1.11 support +* Add context manager interface to Connection which closes the connection on ``__exit__``. + + ====================== What's new in 1.4.6 ====================== From 102fe7b990657b6ea0a90df259fc2ece06eb39ec Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sun, 1 Dec 2019 12:06:17 +0900 Subject: [PATCH 169/177] Update README --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8b74dfcf..5b280490 100644 --- a/README.md +++ b/README.md @@ -51,9 +51,13 @@ $ pip install mysqlclient ### Linux +**Note that this is a basic step. I can not support complete step for build for all +environment. If you can see some error, you should fix it by yourself, or ask for +support in some user forum. Don't file a issue on the issue tracker.** + You may need to install the Python 3 and MySQL development headers and libraries like so: -* `$ sudo apt-get install python3-dev default-libmysqlclient-dev` # Debian / Ubuntu +* `$ sudo apt-get install python3-dev default-libmysqlclient-dev build-essentials` # Debian / Ubuntu * `% sudo yum install python3-devel mysql-devel` # Red Hat / CentOS Then you can install mysqlclient via pip now: From 1a5ae1daf87c4a8135f72d6c870d9edafabd1185 Mon Sep 17 00:00:00 2001 From: migimigi Date: Tue, 3 Dec 2019 19:58:32 +0900 Subject: [PATCH 170/177] Added to pass ssl_mode in configuration. (#347) --- MySQLdb/_mysql.c | 49 ++++++++++++++++++++++++++++++++++++--- MySQLdb/connections.py | 7 ++++++ doc/MySQLdb.constants.rst | 1 - doc/user_guide.rst | 12 ++++++++++ 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index f396561f..13280ace 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -34,6 +34,14 @@ PERFORMANCE OF THIS SOFTWARE. #define my_bool _Bool #endif +#if ((MYSQL_VERSION_ID >= 50555 && MYSQL_VERSION_ID <= 50599) || \ +(MYSQL_VERSION_ID >= 50636 && MYSQL_VERSION_ID <= 50699) || \ +(MYSQL_VERSION_ID >= 50711 && MYSQL_VERSION_ID <= 50799) || \ +(MYSQL_VERSION_ID >= 80000)) && \ +!defined(MARIADB_BASE_VERSION) && !defined(MARIADB_VERSION_ID) +#define HAVE_ENUM_MYSQL_OPT_SSL_MODE +#endif + #define PY_SSIZE_T_CLEAN 1 #include "Python.h" @@ -371,6 +379,23 @@ static int _mysql_ResultObject_clear(_mysql_ResultObject *self) return 0; } +#ifdef HAVE_ENUM_MYSQL_OPT_SSL_MODE +static int +_get_ssl_mode_num(char *ssl_mode) +{ + static char *ssl_mode_list[] = { "DISABLED", "PREFERRED", + "REQUIRED", "VERIFY_CA", "VERIFY_IDENTITY" }; + unsigned int i; + for (i=0; i < sizeof(ssl_mode_list)/sizeof(ssl_mode_list[0]); i++) { + if (strcmp(ssl_mode, ssl_mode_list[i]) == 0) { + // SSL_MODE one-based + return i + 1; + } + } + return -1; +} +#endif + static int _mysql_ConnectionObject_Initialize( _mysql_ConnectionObject *self, @@ -380,6 +405,7 @@ _mysql_ConnectionObject_Initialize( MYSQL *conn = NULL; PyObject *conv = NULL; PyObject *ssl = NULL; + char *ssl_mode = NULL; const char *key = NULL, *cert = NULL, *ca = NULL, *capath = NULL, *cipher = NULL; PyObject *ssl_keepref[5] = {NULL}; @@ -393,7 +419,7 @@ _mysql_ConnectionObject_Initialize( "connect_timeout", "compress", "named_pipe", "init_command", "read_default_file", "read_default_group", - "client_flag", "ssl", + "client_flag", "ssl", "ssl_mode", "local_infile", "read_timeout", "write_timeout", "charset", "auth_plugin", @@ -412,7 +438,7 @@ _mysql_ConnectionObject_Initialize( self->open = 0; if (!PyArg_ParseTupleAndKeywords(args, kwargs, - "|ssssisOiiisssiOiiiss:connect", + "|ssssisOiiisssiOsiiiss:connect", kwlist, &host, &user, &passwd, &db, &port, &unix_socket, &conv, @@ -420,7 +446,7 @@ _mysql_ConnectionObject_Initialize( &compress, &named_pipe, &init_command, &read_default_file, &read_default_group, - &client_flag, &ssl, + &client_flag, &ssl, &ssl_mode, &local_infile, &read_timeout, &write_timeout, @@ -441,6 +467,17 @@ _mysql_ConnectionObject_Initialize( _stringsuck(key, value, ssl); _stringsuck(cipher, value, ssl); } + if (ssl_mode) { +#ifdef HAVE_ENUM_MYSQL_OPT_SSL_MODE + if (_get_ssl_mode_num(ssl_mode) <= 0) { + PyErr_SetString(_mysql_NotSupportedError, "Unknown ssl_mode specification"); + return -1; + } +#else + PyErr_SetString(_mysql_NotSupportedError, "MySQL client library does not support ssl_mode specification"); + return -1; +#endif + } conn = mysql_init(&(self->connection)); if (!conn) { @@ -483,6 +520,12 @@ _mysql_ConnectionObject_Initialize( if (ssl) { mysql_ssl_set(&(self->connection), key, cert, ca, capath, cipher); } +#ifdef HAVE_ENUM_MYSQL_OPT_SSL_MODE + if (ssl_mode) { + int ssl_mode_num = _get_ssl_mode_num(ssl_mode); + mysql_options(&(self->connection), MYSQL_OPT_SSL_MODE, &ssl_mode_num); + } +#endif if (charset) { mysql_options(&(self->connection), MYSQL_SET_CHARSET_NAME, charset); } diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 020b4a99..1d67daa7 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -102,6 +102,13 @@ class object, used to create cursors (keyword only) :param int client_flag: flags to use or 0 (see MySQL docs or constants/CLIENTS.py) + :param str ssl_mode: + specify the security settings for connection to the server; + see the MySQL documentation for more details + (mysql_option(), MYSQL_OPT_SSL_MODE). + Only one of 'DISABLED', 'PREFERRED', 'REQUIRED', + 'VERIFY_CA', 'VERIFY_IDENTITY' can be specified. + :param dict ssl: dictionary or mapping contains SSL connection parameters; see the MySQL documentation for more details diff --git a/doc/MySQLdb.constants.rst b/doc/MySQLdb.constants.rst index 5c9a5389..a803e3e5 100644 --- a/doc/MySQLdb.constants.rst +++ b/doc/MySQLdb.constants.rst @@ -48,4 +48,3 @@ constants Package :members: :undoc-members: :show-inheritance: - diff --git a/doc/user_guide.rst b/doc/user_guide.rst index 0d317776..e52d0f79 100644 --- a/doc/user_guide.rst +++ b/doc/user_guide.rst @@ -357,6 +357,18 @@ connect(parameters...) *This must be a keyword parameter.* + ssl_mode + If present, specify the security settings for the + connection to the server. For more information on ssl_mode, + see the MySQL documentation. Only one of 'DISABLED', + 'PREFERRED', 'REQUIRED', 'VERIFY_CA', 'VERIFY_IDENTITY' + can be specified. + + If not present, the session ssl_mode will be unchanged, + but in version 5.7 and later, the default is PREFERRED. + + *This must be a keyword parameter.* + ssl This parameter takes a dictionary or mapping, where the keys are parameter names used by the mysql_ssl_set_ MySQL From 96ab899bf3cfc77f80bba709fefa9f506eed5af4 Mon Sep 17 00:00:00 2001 From: Mike Zheng Date: Wed, 4 Dec 2019 17:04:32 +0800 Subject: [PATCH 171/177] Fix typo (#415) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5b280490..79af2573 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ support in some user forum. Don't file a issue on the issue tracker.** You may need to install the Python 3 and MySQL development headers and libraries like so: -* `$ sudo apt-get install python3-dev default-libmysqlclient-dev build-essentials` # Debian / Ubuntu +* `$ sudo apt-get install python3-dev default-libmysqlclient-dev build-essential` # Debian / Ubuntu * `% sudo yum install python3-devel mysql-devel` # Red Hat / CentOS Then you can install mysqlclient via pip now: From b3a5e286e272275568da95d78e32d4f77d553f11 Mon Sep 17 00:00:00 2001 From: Tim Gates Date: Sun, 8 Dec 2019 01:05:39 +1100 Subject: [PATCH 172/177] Fix typo. (#417) Closes #416 --- MySQLdb/cursors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 9d1bda29..d5ff03bf 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -35,7 +35,7 @@ class BaseCursor(object): default number of rows fetchmany() will fetch """ - #: Max stetement size which :meth:`executemany` generates. + #: Max statement size which :meth:`executemany` generates. #: #: Max size of allowed statement is max_allowed_packet - packet_header_size. #: Default value of max_allowed_packet is 1048576. From 88c187a75a9e2b11dda1bda9e8a5de86eba19386 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 9 Dec 2019 17:38:51 +0900 Subject: [PATCH 173/177] Update issue templates --- .github/ISSUE_TEMPLATE/bug_report.md | 58 ++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 00000000..650b04b6 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,58 @@ +--- +name: Bug report +about: Report an issue of this project +title: '' +labels: '' +assignees: '' + +--- + +### Read this first + +We don't use this issue tracker to help users. If you had trouble, please ask it on some user community. +Please use this tracker only when you are sure about it is an issue of this software. + +And please provide full information from first. I don't want to ask questions like "What is your Python version?", "Do you confirm MySQL error log?". If the issue report looks incomplete, I will just close it. + +Are you ready? Please remove until here and make a good issue report!! + + +### Describe the bug + +A clear and concise description of what the bug is. + +### To Reproduce + +**Schema** + +``` +create table .... +``` + +**Code** + +```py +con = MySQLdb.connect(...) +cur = con.cursor() +cur.execute(...) +``` + +### Environment + +**MySQL Server** + +- Server OS (e.g. Windows 10, Ubuntu 20.04): +- Server Version (e.g. MariaDB 10.3.16): + +**MySQL Client** + +- OS (e.g. Windows 10, Ubuntu 20.04): + +- Python (e.g. Homebrew Python 3.7.5): + +- Connector/C (e.g. Homebrew mysql-client 8.0.18): + + +### Additional context + +Add any other context about the problem here. From 0f1ff4c9c01695cee071315008b62c86fd57e22d Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 9 Dec 2019 17:39:19 +0900 Subject: [PATCH 174/177] Delete ISSUE_TEMPLATE.md --- .github/ISSUE_TEMPLATE.md | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 .github/ISSUE_TEMPLATE.md diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md deleted file mode 100644 index 4b7cf394..00000000 --- a/.github/ISSUE_TEMPLATE.md +++ /dev/null @@ -1,13 +0,0 @@ -**IF YOU HAVE SOME TROUBLE, IT'S MAY NOT ISSUE OF THIS PROJECT. GO STACKOVERFLOW!!!** - -If you failed to build, go Stackoverflow. -If you failed to install, go Stackoverflow. -If you failed to connect to database, go Stackoverflow. - -FYI, MySQL Connector/C 6.1.10 has bug. see https://github.com/PyMySQL/mysqlclient-python/issues/169#issuecomment-299778504 - -Only when If you're sure it's PyMySQL's issue, report the complete steps to reproduce, from creating database. - -I don't have time to investigate your issue from an incomplete code snippet. - -See also: https://medium.com/@methane/why-you-must-not-ask-questions-on-github-issues-51d741d83fde From 18163d7022e30e9e15a3ae0e04b49c11c51d4ed3 Mon Sep 17 00:00:00 2001 From: jnozsc Date: Mon, 20 Jan 2020 02:47:22 -0800 Subject: [PATCH 175/177] code cleanup and reformat (#423) --- .travis.yml | 15 +- MySQLdb/__init__.py | 160 ++++-- MySQLdb/_exceptions.py | 1 + MySQLdb/connections.py | 88 ++-- MySQLdb/constants/CLIENT.py | 4 +- MySQLdb/constants/CR.py | 14 +- MySQLdb/constants/ER.py | 16 +- MySQLdb/constants/__init__.py | 2 +- MySQLdb/converters.py | 39 +- MySQLdb/cursors.py | 108 ++-- MySQLdb/times.py | 65 ++- ci/test_mysql.py | 38 +- doc/conf.py | 80 +-- setup.py | 13 +- setup_common.py | 28 +- setup_posix.py | 113 +++-- setup_windows.py | 54 +- tests/capabilities.py | 271 +++++----- tests/configdb.py | 8 +- tests/dbapi20.py | 765 +++++++++++++++-------------- tests/test_MySQLdb_capabilities.py | 129 +++-- tests/test_MySQLdb_dbapi20.py | 201 ++++---- tests/test_MySQLdb_nonstandard.py | 60 ++- tests/test_MySQLdb_times.py | 147 +++--- tests/test_cursor.py | 81 +-- 25 files changed, 1421 insertions(+), 1079 deletions(-) diff --git a/.travis.yml b/.travis.yml index df95e89d..ea1c62ea 100644 --- a/.travis.yml +++ b/.travis.yml @@ -59,7 +59,20 @@ jobs: script: - cd django-${DJANGO_VERSION}/tests/ - ./runtests.py --parallel=1 --settings=test_mysql - + - name: flake8 + python: "3.8" + install: + - pip install -U pip + - pip install flake8 + script: + - flake8 --ignore=E203,E501,W503 --max-line-length=88 . + - name: black + python: "3.8" + install: + - pip install -U pip + - pip install black + script: + - black --check --exclude=doc/ . #- &django_3_0 # <<: *django_2_2 # name: "Django 3.0 test (Python 3.8)" diff --git a/MySQLdb/__init__.py b/MySQLdb/__init__.py index fbb5e41a..824acace 100644 --- a/MySQLdb/__init__.py +++ b/MySQLdb/__init__.py @@ -13,28 +13,57 @@ MySQLdb.converters module. """ -from MySQLdb.release import __version__, version_info, __author__ - -from . import _mysql - -if version_info != _mysql.version_info: - raise ImportError("this is MySQLdb version %s, but _mysql is version %r\n_mysql: %r" % - (version_info, _mysql.version_info, _mysql.__file__)) - -threadsafety = 1 -apilevel = "2.0" -paramstyle = "format" - -from ._mysql import * +try: + from MySQLdb.release import version_info + from . import _mysql + + assert version_info == _mysql.version_info +except Exception: + raise ImportError( + "this is MySQLdb version {}, but _mysql is version {!r}\n_mysql: {!r}".format( + version_info, _mysql.version_info, _mysql.__file__ + ) + ) + + +from ._mysql import ( + NotSupportedError, + OperationalError, + get_client_info, + ProgrammingError, + Error, + InterfaceError, + debug, + IntegrityError, + string_literal, + MySQLError, + DataError, + escape, + escape_string, + DatabaseError, + InternalError, + Warning, +) from MySQLdb.constants import FIELD_TYPE -from MySQLdb.times import Date, Time, Timestamp, \ - DateFromTicks, TimeFromTicks, TimestampFromTicks +from MySQLdb.times import ( + Date, + Time, + Timestamp, + DateFromTicks, + TimeFromTicks, + TimestampFromTicks, +) try: frozenset except NameError: from sets import ImmutableSet as frozenset +threadsafety = 1 +apilevel = "2.0" +paramstyle = "format" + + class DBAPISet(frozenset): """A special type of set for which A == x is true if A is a DBAPISet and x is a member of that set.""" @@ -45,49 +74,106 @@ def __eq__(self, other): return other in self -STRING = DBAPISet([FIELD_TYPE.ENUM, FIELD_TYPE.STRING, - FIELD_TYPE.VAR_STRING]) -BINARY = DBAPISet([FIELD_TYPE.BLOB, FIELD_TYPE.LONG_BLOB, - FIELD_TYPE.MEDIUM_BLOB, FIELD_TYPE.TINY_BLOB]) -NUMBER = DBAPISet([FIELD_TYPE.DECIMAL, FIELD_TYPE.DOUBLE, FIELD_TYPE.FLOAT, - FIELD_TYPE.INT24, FIELD_TYPE.LONG, FIELD_TYPE.LONGLONG, - FIELD_TYPE.TINY, FIELD_TYPE.YEAR, FIELD_TYPE.NEWDECIMAL]) -DATE = DBAPISet([FIELD_TYPE.DATE]) -TIME = DBAPISet([FIELD_TYPE.TIME]) +STRING = DBAPISet([FIELD_TYPE.ENUM, FIELD_TYPE.STRING, FIELD_TYPE.VAR_STRING]) +BINARY = DBAPISet( + [ + FIELD_TYPE.BLOB, + FIELD_TYPE.LONG_BLOB, + FIELD_TYPE.MEDIUM_BLOB, + FIELD_TYPE.TINY_BLOB, + ] +) +NUMBER = DBAPISet( + [ + FIELD_TYPE.DECIMAL, + FIELD_TYPE.DOUBLE, + FIELD_TYPE.FLOAT, + FIELD_TYPE.INT24, + FIELD_TYPE.LONG, + FIELD_TYPE.LONGLONG, + FIELD_TYPE.TINY, + FIELD_TYPE.YEAR, + FIELD_TYPE.NEWDECIMAL, + ] +) +DATE = DBAPISet([FIELD_TYPE.DATE]) +TIME = DBAPISet([FIELD_TYPE.TIME]) TIMESTAMP = DBAPISet([FIELD_TYPE.TIMESTAMP, FIELD_TYPE.DATETIME]) -DATETIME = TIMESTAMP -ROWID = DBAPISet() +DATETIME = TIMESTAMP +ROWID = DBAPISet() + def test_DBAPISet_set_equality(): assert STRING == STRING + def test_DBAPISet_set_inequality(): assert STRING != NUMBER + def test_DBAPISet_set_equality_membership(): assert FIELD_TYPE.VAR_STRING == STRING + def test_DBAPISet_set_inequality_membership(): assert FIELD_TYPE.DATE != STRING + def Binary(x): return bytes(x) + def Connect(*args, **kwargs): """Factory function for connections.Connection.""" from MySQLdb.connections import Connection + return Connection(*args, **kwargs) -connect = Connection = Connect -__all__ = [ 'BINARY', 'Binary', 'Connect', 'Connection', 'DATE', - 'Date', 'Time', 'Timestamp', 'DateFromTicks', 'TimeFromTicks', - 'TimestampFromTicks', 'DataError', 'DatabaseError', 'Error', - 'FIELD_TYPE', 'IntegrityError', 'InterfaceError', 'InternalError', - 'MySQLError', 'NUMBER', 'NotSupportedError', 'DBAPISet', - 'OperationalError', 'ProgrammingError', 'ROWID', 'STRING', 'TIME', - 'TIMESTAMP', 'Warning', 'apilevel', 'connect', 'connections', - 'constants', 'converters', 'cursors', 'debug', 'escape', - 'escape_string', 'get_client_info', - 'paramstyle', 'string_literal', 'threadsafety', 'version_info'] +connect = Connection = Connect +__all__ = [ + "BINARY", + "Binary", + "Connect", + "Connection", + "DATE", + "Date", + "Time", + "Timestamp", + "DateFromTicks", + "TimeFromTicks", + "TimestampFromTicks", + "DataError", + "DatabaseError", + "Error", + "FIELD_TYPE", + "IntegrityError", + "InterfaceError", + "InternalError", + "MySQLError", + "NUMBER", + "NotSupportedError", + "DBAPISet", + "OperationalError", + "ProgrammingError", + "ROWID", + "STRING", + "TIME", + "TIMESTAMP", + "Warning", + "apilevel", + "connect", + "connections", + "constants", + "converters", + "cursors", + "debug", + "escape", + "escape_string", + "get_client_info", + "paramstyle", + "string_literal", + "threadsafety", + "version_info", +] diff --git a/MySQLdb/_exceptions.py b/MySQLdb/_exceptions.py index 9cfff57f..ba35deaf 100644 --- a/MySQLdb/_exceptions.py +++ b/MySQLdb/_exceptions.py @@ -5,6 +5,7 @@ https://www.python.org/dev/peps/pep-0249/ """ + class MySQLError(Exception): """Exception related to operation with MySQL.""" diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 1d67daa7..8e226ffe 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -8,9 +8,16 @@ from . import cursors, _mysql from ._exceptions import ( - Warning, Error, InterfaceError, DataError, - DatabaseError, OperationalError, IntegrityError, InternalError, - NotSupportedError, ProgrammingError, + Warning, + Error, + InterfaceError, + DataError, + DatabaseError, + OperationalError, + IntegrityError, + InternalError, + NotSupportedError, + ProgrammingError, ) # Mapping from MySQL charset name to Python codec name @@ -24,6 +31,7 @@ re_numeric_part = re.compile(r"^(\d+)") + def numeric_part(s): """Returns the leading numeric part of a string. @@ -136,13 +144,13 @@ class object, used to create cursors (keyword only) kwargs2 = kwargs.copy() - if 'database' in kwargs2: - kwargs2['db'] = kwargs2.pop('database') - if 'password' in kwargs2: - kwargs2['passwd'] = kwargs2.pop('password') + if "database" in kwargs2: + kwargs2["db"] = kwargs2.pop("database") + if "password" in kwargs2: + kwargs2["passwd"] = kwargs2.pop("password") - if 'conv' in kwargs: - conv = kwargs['conv'] + if "conv" in kwargs: + conv = kwargs["conv"] else: conv = conversions @@ -152,30 +160,31 @@ class object, used to create cursors (keyword only) conv2[k] = v[:] else: conv2[k] = v - kwargs2['conv'] = conv2 - - cursorclass = kwargs2.pop('cursorclass', self.default_cursor) - charset = kwargs2.get('charset', '') - use_unicode = kwargs2.pop('use_unicode', True) - sql_mode = kwargs2.pop('sql_mode', '') - self._binary_prefix = kwargs2.pop('binary_prefix', False) - - client_flag = kwargs.get('client_flag', 0) - client_version = tuple([ numeric_part(n) for n in _mysql.get_client_info().split('.')[:2] ]) + kwargs2["conv"] = conv2 + + cursorclass = kwargs2.pop("cursorclass", self.default_cursor) + charset = kwargs2.get("charset", "") + use_unicode = kwargs2.pop("use_unicode", True) + sql_mode = kwargs2.pop("sql_mode", "") + self._binary_prefix = kwargs2.pop("binary_prefix", False) + + client_flag = kwargs.get("client_flag", 0) + client_version = tuple( + [numeric_part(n) for n in _mysql.get_client_info().split(".")[:2]] + ) if client_version >= (4, 1): client_flag |= CLIENT.MULTI_STATEMENTS if client_version >= (5, 0): client_flag |= CLIENT.MULTI_RESULTS - kwargs2['client_flag'] = client_flag + kwargs2["client_flag"] = client_flag # PEP-249 requires autocommit to be initially off - autocommit = kwargs2.pop('autocommit', False) + autocommit = kwargs2.pop("autocommit", False) - super(Connection, self).__init__(*args, **kwargs2) + super().__init__(*args, **kwargs2) self.cursorclass = cursorclass - self.encoders = dict([ (k, v) for k, v in conv.items() - if type(k) is not int ]) + self.encoders = {k: v for k, v in conv.items() if type(k) is not int} # XXX THIS IS GARBAGE: While this is just a garbage and undocumented, # Django 1.11 depends on it. And they don't fix it because @@ -184,9 +193,11 @@ class object, used to create cursors (keyword only) # See PyMySQL/mysqlclient-python#306 self.encoders[bytes] = bytes - self._server_version = tuple([ numeric_part(n) for n in self.get_server_info().split('.')[:2] ]) + self._server_version = tuple( + [numeric_part(n) for n in self.get_server_info().split(".")[:2]] + ) - self.encoding = 'ascii' # overridden in set_character_set() + self.encoding = "ascii" # overridden in set_character_set() db = proxy(self) def unicode_literal(u, dummy=None): @@ -200,8 +211,15 @@ def unicode_literal(u, dummy=None): self.set_sql_mode(sql_mode) if use_unicode: - for t in (FIELD_TYPE.STRING, FIELD_TYPE.VAR_STRING, FIELD_TYPE.VARCHAR, FIELD_TYPE.TINY_BLOB, - FIELD_TYPE.MEDIUM_BLOB, FIELD_TYPE.LONG_BLOB, FIELD_TYPE.BLOB): + for t in ( + FIELD_TYPE.STRING, + FIELD_TYPE.VAR_STRING, + FIELD_TYPE.VARCHAR, + FIELD_TYPE.TINY_BLOB, + FIELD_TYPE.MEDIUM_BLOB, + FIELD_TYPE.LONG_BLOB, + FIELD_TYPE.BLOB, + ): self.converter[t] = _bytes_or_str # Unlike other string/blob types, JSON is always text. # MySQL may return JSON with charset==binary. @@ -244,11 +262,11 @@ def _bytes_literal(self, bs): assert isinstance(bs, (bytes, bytearray)) x = self.string_literal(bs) # x is escaped and quoted bytes if self._binary_prefix: - return b'_binary' + x + return b"_binary" + x return x def _tuple_literal(self, t): - return b"(%s)" % (b','.join(map(self.literal, t))) + return b"(%s)" % (b",".join(map(self.literal, t))) def literal(self, o): """If o is a single object, returns an SQL literal as a string. @@ -280,7 +298,7 @@ def begin(self): """ self.query(b"BEGIN") - if not hasattr(_mysql.connection, 'warning_count'): + if not hasattr(_mysql.connection, "warning_count"): def warning_count(self): """Return the number of warnings generated from the @@ -299,11 +317,11 @@ def set_character_set(self, charset): py_charset = _charset_to_encoding.get(charset, charset) if self.character_set_name() != charset: try: - super(Connection, self).set_character_set(charset) + super().set_character_set(charset) except AttributeError: if self._server_version < (4, 1): raise NotSupportedError("server is too old to set charset") - self.query('SET NAMES %s' % charset) + self.query("SET NAMES %s" % charset) self.store_result() self.encoding = py_charset @@ -320,7 +338,8 @@ def show_warnings(self): sequence of tuples of (Level, Code, Message). This is only supported in MySQL-4.1 and up. If your server is an earlier version, an empty sequence is returned.""" - if self._server_version < (4,1): return () + if self._server_version < (4, 1): + return () self.query("SHOW WARNINGS") r = self.store_result() warnings = r.fetch_row(0) @@ -337,4 +356,5 @@ def show_warnings(self): ProgrammingError = ProgrammingError NotSupportedError = NotSupportedError + # vim: colorcolumn=100 diff --git a/MySQLdb/constants/CLIENT.py b/MySQLdb/constants/CLIENT.py index 6559917b..35f578cc 100644 --- a/MySQLdb/constants/CLIENT.py +++ b/MySQLdb/constants/CLIENT.py @@ -20,10 +20,8 @@ INTERACTIVE = 1024 SSL = 2048 IGNORE_SIGPIPE = 4096 -TRANSACTIONS = 8192 # mysql_com.h was WRONG prior to 3.23.35 +TRANSACTIONS = 8192 # mysql_com.h was WRONG prior to 3.23.35 RESERVED = 16384 SECURE_CONNECTION = 32768 MULTI_STATEMENTS = 65536 MULTI_RESULTS = 131072 - - diff --git a/MySQLdb/constants/CR.py b/MySQLdb/constants/CR.py index 753408ee..9d33cf65 100644 --- a/MySQLdb/constants/CR.py +++ b/MySQLdb/constants/CR.py @@ -9,16 +9,18 @@ """ Usage: python CR.py [/path/to/mysql/errmsg.h ...] >> CR.py """ - import fileinput, re + import fileinput + import re + data = {} error_last = None for line in fileinput.input(): - line = re.sub(r'/\*.*?\*/', '', line) - m = re.match(r'^\s*#define\s+CR_([A-Z0-9_]+)\s+(\d+)(\s.*|$)', line) + line = re.sub(r"/\*.*?\*/", "", line) + m = re.match(r"^\s*#define\s+CR_([A-Z0-9_]+)\s+(\d+)(\s.*|$)", line) if m: name = m.group(1) value = int(m.group(2)) - if name == 'ERROR_LAST': + if name == "ERROR_LAST": if error_last is None or error_last < value: error_last = value continue @@ -27,9 +29,9 @@ data[value].add(name) for value, names in sorted(data.items()): for name in sorted(names): - print('%s = %s' % (name, value)) + print("{} = {}".format(name, value)) if error_last is not None: - print('ERROR_LAST = %s' % error_last) + print("ERROR_LAST = %s" % error_last) ERROR_FIRST = 2000 diff --git a/MySQLdb/constants/ER.py b/MySQLdb/constants/ER.py index 2e623b51..fcd5bf2e 100644 --- a/MySQLdb/constants/ER.py +++ b/MySQLdb/constants/ER.py @@ -8,18 +8,20 @@ """ Usage: python ER.py [/path/to/mysql/mysqld_error.h ...] >> ER.py """ - import fileinput, re + import fileinput + import re + data = {} error_last = None for line in fileinput.input(): - line = re.sub(r'/\*.*?\*/', '', line) - m = re.match(r'^\s*#define\s+((ER|WARN)_[A-Z0-9_]+)\s+(\d+)\s*', line) + line = re.sub(r"/\*.*?\*/", "", line) + m = re.match(r"^\s*#define\s+((ER|WARN)_[A-Z0-9_]+)\s+(\d+)\s*", line) if m: name = m.group(1) - if name.startswith('ER_'): + if name.startswith("ER_"): name = name[3:] value = int(m.group(3)) - if name == 'ERROR_LAST': + if name == "ERROR_LAST": if error_last is None or error_last < value: error_last = value continue @@ -28,9 +30,9 @@ data[value].add(name) for value, names in sorted(data.items()): for name in sorted(names): - print('%s = %s' % (name, value)) + print("{} = {}".format(name, value)) if error_last is not None: - print('ERROR_LAST = %s' % error_last) + print("ERROR_LAST = %s" % error_last) ERROR_FIRST = 1000 diff --git a/MySQLdb/constants/__init__.py b/MySQLdb/constants/__init__.py index 3e774cd9..0372265b 100644 --- a/MySQLdb/constants/__init__.py +++ b/MySQLdb/constants/__init__.py @@ -1 +1 @@ -__all__ = ['CR', 'FIELD_TYPE','CLIENT','ER','FLAG'] +__all__ = ["CR", "FIELD_TYPE", "CLIENT", "ER", "FLAG"] diff --git a/MySQLdb/converters.py b/MySQLdb/converters.py index c460fbd4..33f22f74 100644 --- a/MySQLdb/converters.py +++ b/MySQLdb/converters.py @@ -32,15 +32,24 @@ """ from decimal import Decimal -from MySQLdb._mysql import string_literal, escape +from MySQLdb._mysql import string_literal from MySQLdb.constants import FIELD_TYPE, FLAG -from MySQLdb.times import * +from MySQLdb.times import ( + Date, + DateTimeType, + DateTime2literal, + DateTimeDeltaType, + DateTimeDelta2literal, + DateTime_or_None, + TimeDelta_or_None, + Date_or_None, +) from MySQLdb._exceptions import ProgrammingError -NoneType = type(None) - import array +NoneType = type(None) + try: ArrayType = array.ArrayType except AttributeError: @@ -48,28 +57,33 @@ def Bool2Str(s, d): - return b'1' if s else b'0' + return b"1" if s else b"0" + def Set2Str(s, d): # Only support ascii string. Not tested. - return string_literal(','.join(s)) + return string_literal(",".join(s)) + def Thing2Str(s, d): """Convert something into a string via str().""" return str(s) + def Float2Str(o, d): s = repr(o) - if s in ('inf', 'nan'): + if s in ("inf", "nan"): raise ProgrammingError("%s can not be used with MySQL" % s) - if 'e' not in s: - s += 'e0' + if "e" not in s: + s += "e0" return s + def None2NULL(o, d): """Convert None to NULL.""" return b"NULL" + def Thing2Literal(o, d): """Convert something into a SQL string literal. If using MySQL-3.23 or newer, string_literal() is a method of the @@ -77,12 +91,15 @@ def Thing2Literal(o, d): that method when the connection is created.""" return string_literal(o) + def Decimal2Literal(o, d): - return format(o, 'f') + return format(o, "f") + def array2Str(o, d): return Thing2Literal(o.tostring(), d) + # bytes or str regarding to BINARY_FLAG. _bytes_or_str = ((FLAG.BINARY, bytes), (None, str)) @@ -97,7 +114,6 @@ def array2Str(o, d): DateTimeDeltaType: DateTimeDelta2literal, set: Set2Str, Decimal: Decimal2Literal, - FIELD_TYPE.TINY: int, FIELD_TYPE.SHORT: int, FIELD_TYPE.LONG: int, @@ -112,7 +128,6 @@ def array2Str(o, d): FIELD_TYPE.DATETIME: DateTime_or_None, FIELD_TYPE.TIME: TimeDelta_or_None, FIELD_TYPE.DATE: Date_or_None, - FIELD_TYPE.TINY_BLOB: bytes, FIELD_TYPE.MEDIUM_BLOB: bytes, FIELD_TYPE.LONG_BLOB: bytes, diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index d5ff03bf..1d2ee460 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -12,13 +12,18 @@ #: executemany only supports simple bulk insert. #: You can use it to load large dataset. RE_INSERT_VALUES = re.compile( - r"\s*((?:INSERT|REPLACE)\b.+\bVALUES?\s*)" + - r"(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))" + - r"(\s*(?:ON DUPLICATE.*)?);?\s*\Z", - re.IGNORECASE | re.DOTALL) - - -class BaseCursor(object): + "".join( + [ + r"\s*((?:INSERT|REPLACE)\b.+\bVALUES?\s*)", + r"(\(\s*(?:%s|%\(.+\)s)\s*(?:,\s*(?:%s|%\(.+\)s)\s*)*\))", + r"(\s*(?:ON DUPLICATE.*)?);?\s*\Z", + ] + ), + re.IGNORECASE | re.DOTALL, +) + + +class BaseCursor: """A base for Cursor classes. Useful attributes: description @@ -39,12 +44,20 @@ class BaseCursor(object): #: #: Max size of allowed statement is max_allowed_packet - packet_header_size. #: Default value of max_allowed_packet is 1048576. - max_stmt_length = 64*1024 + max_stmt_length = 64 * 1024 from ._exceptions import ( - MySQLError, Warning, Error, InterfaceError, - DatabaseError, DataError, OperationalError, IntegrityError, - InternalError, ProgrammingError, NotSupportedError, + MySQLError, + Warning, + Error, + InterfaceError, + DatabaseError, + DataError, + OperationalError, + IntegrityError, + InternalError, + ProgrammingError, + NotSupportedError, ) connection = None @@ -98,8 +111,10 @@ def ensure_bytes(x): if isinstance(args, (tuple, list)): ret = tuple(literal(ensure_bytes(arg)) for arg in args) elif isinstance(args, dict): - ret = {ensure_bytes(key): literal(ensure_bytes(val)) - for (key, val) in args.items()} + ret = { + ensure_bytes(key): literal(ensure_bytes(val)) + for (key, val) in args.items() + } else: # If it's not a dictionary let's try escaping it anyways. # Worst case it will throw a Value error @@ -216,16 +231,23 @@ def executemany(self, query, args): if m: q_prefix = m.group(1) % () q_values = m.group(2).rstrip() - q_postfix = m.group(3) or '' - assert q_values[0] == '(' and q_values[-1] == ')' - return self._do_execute_many(q_prefix, q_values, q_postfix, args, - self.max_stmt_length, - self._get_db().encoding) + q_postfix = m.group(3) or "" + assert q_values[0] == "(" and q_values[-1] == ")" + return self._do_execute_many( + q_prefix, + q_values, + q_postfix, + args, + self.max_stmt_length, + self._get_db().encoding, + ) self.rowcount = sum(self.execute(query, arg) for arg in args) return self.rowcount - 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() escape = self._escape_args if isinstance(prefix, str): @@ -245,7 +267,7 @@ def _do_execute_many(self, prefix, values, postfix, args, max_stmt_length, encod rows += self.execute(sql + postfix) sql = bytearray(prefix) else: - sql += b',' + sql += b"," sql += v rows += self.execute(sql + postfix) self.rowcount = rows @@ -283,15 +305,17 @@ def callproc(self, procname, args=()): if isinstance(procname, str): procname = procname.encode(db.encoding) if args: - fmt = b'@_' + procname + b'_%d=%s' - q = b'SET %s' % b','.join(fmt % (index, db.literal(arg)) - for index, arg in enumerate(args)) + fmt = b"@_" + procname + b"_%d=%s" + q = b"SET %s" % b",".join( + fmt % (index, db.literal(arg)) for index, arg in enumerate(args) + ) self._query(q) self.nextset() - q = b"CALL %s(%s)" % (procname, - b','.join([b'@_%s_%d' % (procname, i) - for i in range(len(args))])) + q = b"CALL %s(%s)" % ( + procname, + b",".join([b"@_%s_%d" % (procname, i) for i in range(len(args))]), + ) self._query(q) return args @@ -325,7 +349,7 @@ def __iter__(self): NotSupportedError = NotSupportedError -class CursorStoreResultMixIn(object): +class CursorStoreResultMixIn: """This is a MixIn class which causes the entire result set to be stored on the client side, i.e. it uses mysql_store_result(). If the result set can be very large, consider adding a LIMIT clause to your @@ -353,7 +377,7 @@ def fetchmany(self, size=None): than size. If size is not defined, cursor.arraysize is used.""" self._check_executed() end = self.rownumber + (size or self.arraysize) - result = self._rows[self.rownumber:end] + result = self._rows[self.rownumber : end] self.rownumber = min(end, len(self._rows)) return result @@ -361,13 +385,13 @@ def fetchall(self): """Fetchs all available rows from the cursor.""" self._check_executed() if self.rownumber: - result = self._rows[self.rownumber:] + result = self._rows[self.rownumber :] else: result = self._rows self.rownumber = len(self._rows) return result - def scroll(self, value, mode='relative'): + def scroll(self, value, mode="relative"): """Scroll the cursor in the result set to a new position according to mode. @@ -375,9 +399,9 @@ def scroll(self, value, mode='relative'): the current position in the result set, if set to 'absolute', value states an absolute target position.""" self._check_executed() - if mode == 'relative': + if mode == "relative": r = self.rownumber + value - elif mode == 'absolute': + elif mode == "absolute": r = value else: raise ProgrammingError("unknown scroll mode %s" % repr(mode)) @@ -387,11 +411,11 @@ def scroll(self, value, mode='relative'): def __iter__(self): self._check_executed() - result = self.rownumber and self._rows[self.rownumber:] or self._rows + result = self.rownumber and self._rows[self.rownumber :] or self._rows return iter(result) -class CursorUseResultMixIn(object): +class CursorUseResultMixIn: """This is a MixIn class which causes the result set to be stored in the server and sent row-by-row to client side, i.e. it uses @@ -438,39 +462,35 @@ def next(self): __next__ = next -class CursorTupleRowsMixIn(object): +class CursorTupleRowsMixIn: """This is a MixIn class that causes all rows to be returned as tuples, which is the standard form required by DB API.""" _fetch_type = 0 -class CursorDictRowsMixIn(object): +class CursorDictRowsMixIn: """This is a MixIn class that causes all rows to be returned as dictionaries. This is a non-standard feature.""" _fetch_type = 1 -class Cursor(CursorStoreResultMixIn, CursorTupleRowsMixIn, - BaseCursor): +class Cursor(CursorStoreResultMixIn, CursorTupleRowsMixIn, BaseCursor): """This is the standard Cursor class that returns rows as tuples and stores the result set in the client.""" -class DictCursor(CursorStoreResultMixIn, CursorDictRowsMixIn, - BaseCursor): +class DictCursor(CursorStoreResultMixIn, CursorDictRowsMixIn, BaseCursor): """This is a Cursor class that returns rows as dictionaries and stores the result set in the client.""" -class SSCursor(CursorUseResultMixIn, CursorTupleRowsMixIn, - BaseCursor): +class SSCursor(CursorUseResultMixIn, CursorTupleRowsMixIn, BaseCursor): """This is a Cursor class that returns rows as tuples and stores the result set in the server.""" -class SSDictCursor(CursorUseResultMixIn, CursorDictRowsMixIn, - BaseCursor): +class SSDictCursor(CursorUseResultMixIn, CursorDictRowsMixIn, BaseCursor): """This is a Cursor class that returns rows as dictionaries and stores the result set in the server.""" diff --git a/MySQLdb/times.py b/MySQLdb/times.py index d47c8fb8..f0e9384c 100644 --- a/MySQLdb/times.py +++ b/MySQLdb/times.py @@ -16,34 +16,50 @@ DateTimeDeltaType = timedelta DateTimeType = datetime + def DateFromTicks(ticks): """Convert UNIX ticks into a date instance.""" return date(*localtime(ticks)[:3]) + def TimeFromTicks(ticks): """Convert UNIX ticks into a time instance.""" return time(*localtime(ticks)[3:6]) + def TimestampFromTicks(ticks): """Convert UNIX ticks into a datetime instance.""" return datetime(*localtime(ticks)[:6]) + format_TIME = format_DATE = str + def format_TIMEDELTA(v): seconds = int(v.seconds) % 60 minutes = int(v.seconds // 60) % 60 hours = int(v.seconds // 3600) % 24 - return '%d %d:%d:%d' % (v.days, hours, minutes, seconds) + return "%d %d:%d:%d" % (v.days, hours, minutes, seconds) + def format_TIMESTAMP(d): """ :type d: datetime.datetime """ if d.microsecond: - fmt = "{0.year:04}-{0.month:02}-{0.day:02} {0.hour:02}:{0.minute:02}:{0.second:02}.{0.microsecond:06}" + fmt = " ".join( + [ + "{0.year:04}-{0.month:02}-{0.day:02}", + "{0.hour:02}:{0.minute:02}:{0.second:02}.{0.microsecond:06}", + ] + ) else: - fmt = "{0.year:04}-{0.month:02}-{0.day:02} {0.hour:02}:{0.minute:02}:{0.second:02}" + fmt = " ".join( + [ + "{0.year:04}-{0.month:02}-{0.day:02}", + "{0.hour:02}:{0.minute:02}:{0.second:02}", + ] + ) return fmt.format(d) @@ -64,32 +80,32 @@ def DateTime_or_None(s): return None return datetime( - int(s[:4]), # year - int(s[5:7]), # month - int(s[8:10]), # day + int(s[:4]), # year + int(s[5:7]), # month + int(s[8:10]), # day int(s[11:13] or 0), # hour int(s[14:16] or 0), # minute int(s[17:19] or 0), # second - micros, # microsecond + micros, # microsecond ) except ValueError: return None + def TimeDelta_or_None(s): try: - h, m, s = s.split(':') - if '.' in s: - s, ms = s.split('.') - ms = ms.ljust(6, '0') + h, m, s = s.split(":") + if "." in s: + s, ms = s.split(".") + ms = ms.ljust(6, "0") else: ms = 0 - if h[0] == '-': + if h[0] == "-": negative = True else: negative = False h, m, s, ms = abs(int(h)), int(m), int(s), int(ms) - td = timedelta(hours=h, minutes=m, seconds=s, - microseconds=ms) + td = timedelta(hours=h, minutes=m, seconds=s, microseconds=ms) if negative: return -td else: @@ -98,34 +114,33 @@ def TimeDelta_or_None(s): # unpacking or int/float conversion failed return None + def Time_or_None(s): try: - h, m, s = s.split(':') - if '.' in s: - s, ms = s.split('.') - ms = ms.ljust(6, '0') + h, m, s = s.split(":") + if "." in s: + s, ms = s.split(".") + ms = ms.ljust(6, "0") else: ms = 0 h, m, s, ms = int(h), int(m), int(s), int(ms) - return time(hour=h, minute=m, second=s, - microsecond=ms) + return time(hour=h, minute=m, second=s, microsecond=ms) except ValueError: return None + def Date_or_None(s): try: - return date( - int(s[:4]), # year - int(s[5:7]), # month - int(s[8:10]), # day - ) + return date(int(s[:4]), int(s[5:7]), int(s[8:10]),) # year # month # day except ValueError: return None + def DateTime2literal(d, c): """Format a DateTime object as an ISO timestamp.""" return string_literal(format_TIMESTAMP(d)) + def DateTimeDelta2literal(d, c): """Format a DateTimeDelta object as a time.""" return string_literal(format_TIMEDELTA(d)) diff --git a/ci/test_mysql.py b/ci/test_mysql.py index d24f30f1..88a747a6 100644 --- a/ci/test_mysql.py +++ b/ci/test_mysql.py @@ -13,33 +13,27 @@ # file for each of the backends you test against. DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.mysql', - 'NAME': 'django_default', - 'USER': 'django', - 'HOST': '127.0.0.1', - 'PASSWORD': 'secret', - 'TEST': { - 'CHARSET': 'utf8mb4', - 'COLLATION': 'utf8mb4_general_ci', - }, + "default": { + "ENGINE": "django.db.backends.mysql", + "NAME": "django_default", + "USER": "django", + "HOST": "127.0.0.1", + "PASSWORD": "secret", + "TEST": {"CHARSET": "utf8mb4", "COLLATION": "utf8mb4_general_ci"}, + }, + "other": { + "ENGINE": "django.db.backends.mysql", + "NAME": "django_other", + "USER": "django", + "HOST": "127.0.0.1", + "PASSWORD": "secret", + "TEST": {"CHARSET": "utf8mb4", "COLLATION": "utf8mb4_general_ci"}, }, - 'other': { - 'ENGINE': 'django.db.backends.mysql', - 'NAME': 'django_other', - 'USER': 'django', - 'HOST': '127.0.0.1', - 'PASSWORD': 'secret', - 'TEST': { - 'CHARSET': 'utf8mb4', - 'COLLATION': 'utf8mb4_general_ci', - }, - } } SECRET_KEY = "django_tests_secret_key" # Use a fast hasher to speed up tests. PASSWORD_HASHERS = [ - 'django.contrib.auth.hashers.MD5PasswordHasher', + "django.contrib.auth.hashers.MD5PasswordHasher", ] diff --git a/doc/conf.py b/doc/conf.py index fc7c089d..33f9781c 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # MySQLdb documentation build configuration file, created by # sphinx-quickstart on Sun Oct 07 19:36:17 2012. @@ -11,46 +10,49 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys, os +# skip flake8 and black for this file +# flake8: noqa +import sys +import os # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. -#sys.path.insert(0, os.path.abspath('..')) +#sys.path.insert(0, os.path.abspath("..")) # -- General configuration ----------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. -#needs_sphinx = '1.0' +#needs_sphinx = "1.0" # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. -extensions = ['sphinx.ext.autodoc'] +extensions = ["sphinx.ext.autodoc"] # Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] +templates_path = ["_templates"] # The suffix of source filenames. -source_suffix = '.rst' +source_suffix = ".rst" # The encoding of source files. -#source_encoding = 'utf-8-sig' +#source_encoding = "utf-8-sig" # The master toctree document. -master_doc = 'index' +master_doc = "index" # General information about the project. -project = u'MySQLdb' -copyright = u'2012, Andy Dustman' +project = "MySQLdb" +copyright = "2012, Andy Dustman" # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. -version = '1.2' +version = "1.2" # The full version, including alpha/beta/rc tags. -release = '1.2.4b4' +release = "1.2.4b4" # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. @@ -58,13 +60,13 @@ # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: -#today = '' +#today = "" # Else, today_fmt is used as the format for a strftime call. -#today_fmt = '%B %d, %Y' +#today_fmt = "%B %d, %Y" # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. -exclude_patterns = ['_build'] +exclude_patterns = ["_build"] # The reST default role (used for this markup: `text`) to use for all documents. #default_role = None @@ -81,7 +83,7 @@ #show_authors = False # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' +pygments_style = "sphinx" # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] @@ -91,7 +93,7 @@ # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. -html_theme = 'default' +html_theme = "default" # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -120,11 +122,11 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +html_static_path = ["_static"] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. -#html_last_updated_fmt = '%b %d, %Y' +#html_last_updated_fmt = "%b %d, %Y" # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. @@ -158,33 +160,30 @@ # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. -#html_use_opensearch = '' +#html_use_opensearch = "" # This is the file name suffix for HTML files (e.g. ".xhtml"). #html_file_suffix = None # Output file base name for HTML help builder. -htmlhelp_basename = 'MySQLdbdoc' +htmlhelp_basename = "MySQLdbdoc" # -- Options for LaTeX output -------------------------------------------------- latex_elements = { -# The paper size ('letterpaper' or 'a4paper'). -#'papersize': 'letterpaper', - -# The font size ('10pt', '11pt' or '12pt'). -#'pointsize': '10pt', - -# Additional stuff for the LaTeX preamble. -#'preamble': '', + # The paper size ('letterpaper' or 'a4paper'). + #'papersize': 'letterpaper', + # The font size ('10pt', '11pt' or '12pt'). + #'pointsize': '10pt', + # Additional stuff for the LaTeX preamble. + #'preamble': '', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ - ('index', 'MySQLdb.tex', u'MySQLdb Documentation', - u'Andy Dustman', 'manual'), + ("index", "MySQLdb.tex", "MySQLdb Documentation", "Andy Dustman", "manual"), ] # The name of an image file (relative to this directory) to place at the top of @@ -212,10 +211,7 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). -man_pages = [ - ('index', 'mysqldb', u'MySQLdb Documentation', - [u'Andy Dustman'], 1) -] +man_pages = [("index", "mysqldb", "MySQLdb Documentation", ["Andy Dustman"], 1)] # If true, show URL addresses after external links. #man_show_urls = False @@ -227,9 +223,15 @@ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - ('index', 'MySQLdb', u'MySQLdb Documentation', - u'Andy Dustman', 'MySQLdb', 'One line description of project.', - 'Miscellaneous'), + ( + "index", + "MySQLdb", + "MySQLdb Documentation", + "Andy Dustman", + "MySQLdb", + "One line description of project.", + "Miscellaneous", + ), ] # Documents to append as an appendix to all manuals. diff --git a/setup.py b/setup.py index a39e0d12..dfa661c1 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,6 @@ #!/usr/bin/env python import os -import io import setuptools @@ -10,14 +9,14 @@ else: # assume windows from setup_windows import get_config -with io.open('README.md', encoding='utf-8') as f: +with open("README.md", encoding="utf-8") as f: readme = f.read() metadata, options = get_config() -metadata['ext_modules'] = [ - setuptools.Extension("MySQLdb._mysql", sources=['MySQLdb/_mysql.c'], **options) +metadata["ext_modules"] = [ + setuptools.Extension("MySQLdb._mysql", sources=["MySQLdb/_mysql.c"], **options) ] -metadata['long_description'] = readme -metadata['long_description_content_type'] = "text/markdown" -metadata['python_requires'] = '>=3.5' +metadata["long_description"] = readme +metadata["long_description_content_type"] = "text/markdown" +metadata["python_requires"] = ">=3.5" setuptools.setup(**metadata) diff --git a/setup_common.py b/setup_common.py index 2274e3a0..28c51829 100644 --- a/setup_common.py +++ b/setup_common.py @@ -1,31 +1,37 @@ from configparser import ConfigParser as SafeConfigParser + def get_metadata_and_options(): config = SafeConfigParser() - config.read(['metadata.cfg', 'site.cfg']) + config.read(["metadata.cfg", "site.cfg"]) - metadata = dict(config.items('metadata')) - options = dict(config.items('options')) + metadata = dict(config.items("metadata")) + options = dict(config.items("options")) - metadata['py_modules'] = list(filter(None, metadata['py_modules'].split('\n'))) - metadata['classifiers'] = list(filter(None, metadata['classifiers'].split('\n'))) + metadata["py_modules"] = list(filter(None, metadata["py_modules"].split("\n"))) + metadata["classifiers"] = list(filter(None, metadata["classifiers"].split("\n"))) return metadata, options + def enabled(options, option): value = options[option] s = value.lower() - if s in ('yes','true','1','y'): + if s in ("yes", "true", "1", "y"): return True - elif s in ('no', 'false', '0', 'n'): + elif s in ("no", "false", "0", "n"): return False else: - raise ValueError("Unknown value %s for option %s" % (value, option)) + raise ValueError("Unknown value {} for option {}".format(value, option)) + def create_release_file(metadata): - with open("MySQLdb/release.py",'w') as rel: - rel.write(""" + with open("MySQLdb/release.py", "w") as rel: + rel.write( + """ __author__ = "%(author)s <%(author_email)s>" version_info = %(version_info)s __version__ = "%(version)s" -""" % metadata) +""" + % metadata + ) diff --git a/setup_posix.py b/setup_posix.py index db82b3c6..5602be84 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -1,126 +1,147 @@ -import os, sys -from configparser import ConfigParser as SafeConfigParser +import os +import sys # This dequote() business is required for some older versions # of mysql_config + def dequote(s): if not s: - raise Exception("Wrong MySQL configuration: maybe https://bugs.mysql.com/bug.php?id=86971 ?") + raise Exception( + "Wrong MySQL configuration: maybe https://bugs.mysql.com/bug.php?id=86971 ?" + ) if s[0] in "\"'" and s[0] == s[-1]: s = s[1:-1] return s + _mysql_config_path = "mysql_config" + def mysql_config(what): from os import popen - f = popen("%s --%s" % (_mysql_config_path, what)) + f = popen("{} --{}".format(_mysql_config_path, what)) data = f.read().strip().split() ret = f.close() if ret: - if ret/256: + if ret / 256: data = [] - if ret/256 > 1: - raise EnvironmentError("%s not found" % (_mysql_config_path,)) + if ret / 256 > 1: + raise OSError("{} not found".format(_mysql_config_path)) return data + def get_config(): from setup_common import get_metadata_and_options, enabled, create_release_file + global _mysql_config_path metadata, options = get_metadata_and_options() - if 'mysql_config' in options: - _mysql_config_path = options['mysql_config'] + if "mysql_config" in options: + _mysql_config_path = options["mysql_config"] else: try: - mysql_config('version') - except EnvironmentError: + mysql_config("version") + except OSError: # try mariadb_config _mysql_config_path = "mariadb_config" try: - mysql_config('version') - except EnvironmentError: + mysql_config("version") + except OSError: _mysql_config_path = "mysql_config" extra_objects = [] - static = enabled(options, 'static') + static = enabled(options, "static") # allow a command-line option to override the base config file to permit # a static build to be created via requirements.txt # - if '--static' in sys.argv: + if "--static" in sys.argv: static = True - sys.argv.remove('--static') + sys.argv.remove("--static") libs = mysql_config("libs") - library_dirs = [dequote(i[2:]) for i in libs if i.startswith('-L')] - libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')] - extra_link_args = [x for x in libs if not x.startswith(('-l', '-L'))] + library_dirs = [dequote(i[2:]) for i in libs if i.startswith("-L")] + libraries = [dequote(i[2:]) for i in libs if i.startswith("-l")] + extra_link_args = [x for x in libs if not x.startswith(("-l", "-L"))] - removable_compile_args = ('-I', '-L', '-l') - extra_compile_args = [i.replace("%", "%%") for i in mysql_config("cflags") - if i[:2] not in removable_compile_args] + removable_compile_args = ("-I", "-L", "-l") + extra_compile_args = [ + i.replace("%", "%%") + for i in mysql_config("cflags") + if i[:2] not in removable_compile_args + ] # Copy the arch flags for linking as well for i in range(len(extra_compile_args)): - if extra_compile_args[i] == '-arch': - extra_link_args += ['-arch', extra_compile_args[i + 1]] + if extra_compile_args[i] == "-arch": + extra_link_args += ["-arch", extra_compile_args[i + 1]] - include_dirs = [dequote(i[2:]) - for i in mysql_config('include') if i.startswith('-I')] + include_dirs = [ + dequote(i[2:]) for i in mysql_config("include") if i.startswith("-I") + ] if static: # properly handle mysql client libraries that are not called libmysqlclient client = None - CLIENT_LIST = ['mysqlclient', 'mysqlclient_r', 'mysqld', 'mariadb', - 'mariadbclient', 'perconaserverclient', 'perconaserverclient_r'] + CLIENT_LIST = [ + "mysqlclient", + "mysqlclient_r", + "mysqld", + "mariadb", + "mariadbclient", + "perconaserverclient", + "perconaserverclient_r", + ] for c in CLIENT_LIST: if c in libraries: client = c break - if client == 'mariadb': - client = 'mariadbclient' + if client == "mariadb": + client = "mariadbclient" if client is None: raise ValueError("Couldn't identify mysql client library") - extra_objects.append(os.path.join(library_dirs[0], 'lib%s.a' % client)) + extra_objects.append(os.path.join(library_dirs[0], "lib%s.a" % client)) if client in libraries: libraries.remove(client) else: # mysql_config may have "-lmysqlclient -lz -lssl -lcrypto", but zlib and # ssl is not used by _mysql. They are needed only for static build. - for L in ('crypto', 'ssl', 'z'): + for L in ("crypto", "ssl", "z"): if L in libraries: libraries.remove(L) name = "mysqlclient" - metadata['name'] = name + metadata["name"] = name define_macros = [ - ('version_info', metadata['version_info']), - ('__version__', metadata['version']), - ] + ("version_info", metadata["version_info"]), + ("__version__", metadata["version"]), + ] create_release_file(metadata) - del metadata['version_info'] + del metadata["version_info"] ext_options = dict( - library_dirs = library_dirs, - libraries = libraries, - extra_compile_args = extra_compile_args, - extra_link_args = extra_link_args, - include_dirs = include_dirs, - extra_objects = extra_objects, - define_macros = define_macros, + library_dirs=library_dirs, + libraries=libraries, + extra_compile_args=extra_compile_args, + extra_link_args=extra_link_args, + include_dirs=include_dirs, + extra_objects=extra_objects, + define_macros=define_macros, ) # newer versions of gcc require libstdc++ if doing a static build if static: - ext_options['language'] = 'c++' + ext_options["language"] = "c++" return metadata, ext_options + if __name__ == "__main__": - sys.stderr.write("""You shouldn't be running this directly; it is used by setup.py.""") + sys.stderr.write( + """You shouldn't be running this directly; it is used by setup.py.""" + ) diff --git a/setup_windows.py b/setup_windows.py index cb2cbab0..917eb49d 100644 --- a/setup_windows.py +++ b/setup_windows.py @@ -1,9 +1,10 @@ -import os, sys +import os +import sys from distutils.msvccompiler import get_build_version def get_config(): - from setup_common import get_metadata_and_options, enabled, create_release_file + from setup_common import get_metadata_and_options, create_release_file metadata, options = get_metadata_and_options() @@ -16,37 +17,42 @@ def get_config(): vcversion = int(get_build_version()) if client == "mariadbclient": - library_dirs = [os.path.join(connector, 'lib', 'mariadb')] - libraries = ['kernel32', 'advapi32', 'wsock32', 'shlwapi', 'Ws2_32', client ] - include_dirs = [os.path.join(connector, 'include', 'mariadb')] + library_dirs = [os.path.join(connector, "lib", "mariadb")] + libraries = ["kernel32", "advapi32", "wsock32", "shlwapi", "Ws2_32", client] + include_dirs = [os.path.join(connector, "include", "mariadb")] else: - library_dirs = [os.path.join(connector, r'lib\vs%d' % vcversion), - os.path.join(connector, "lib")] - libraries = ['kernel32', 'advapi32', 'wsock32', client ] - include_dirs = [os.path.join(connector, r'include')] + library_dirs = [ + os.path.join(connector, r"lib\vs%d" % vcversion), + os.path.join(connector, "lib"), + ] + libraries = ["kernel32", "advapi32", "wsock32", client] + include_dirs = [os.path.join(connector, r"include")] - extra_compile_args = ['/Zl', '/D_CRT_SECURE_NO_WARNINGS' ] - extra_link_args = ['/MANIFEST'] + extra_compile_args = ["/Zl", "/D_CRT_SECURE_NO_WARNINGS"] + extra_link_args = ["/MANIFEST"] name = "mysqlclient" - metadata['name'] = name + metadata["name"] = name define_macros = [ - ('version_info', metadata['version_info']), - ('__version__', metadata['version']), - ] + ("version_info", metadata["version_info"]), + ("__version__", metadata["version"]), + ] create_release_file(metadata) - del metadata['version_info'] + del metadata["version_info"] ext_options = dict( - library_dirs = library_dirs, - libraries = libraries, - extra_compile_args = extra_compile_args, - extra_link_args = extra_link_args, - include_dirs = include_dirs, - extra_objects = extra_objects, - define_macros = define_macros, + library_dirs=library_dirs, + libraries=libraries, + extra_compile_args=extra_compile_args, + extra_link_args=extra_link_args, + include_dirs=include_dirs, + extra_objects=extra_objects, + define_macros=define_macros, ) return metadata, ext_options + if __name__ == "__main__": - sys.stderr.write("""You shouldn't be running this directly; it is used by setup.py.""") + sys.stderr.write( + """You shouldn't be running this directly; it is used by setup.py.""" + ) diff --git a/tests/capabilities.py b/tests/capabilities.py index 15db5336..cafe1e61 100644 --- a/tests/capabilities.py +++ b/tests/capabilities.py @@ -6,7 +6,6 @@ """ from time import time -import array import unittest from configdb import connection_factory @@ -16,35 +15,42 @@ class DatabaseTest(unittest.TestCase): db_module = None connect_args = () connect_kwargs = dict() - create_table_extra = '' + create_table_extra = "" rows = 10 debug = False def setUp(self): - import gc + db = connection_factory(**self.connect_kwargs) self.connection = db self.cursor = db.cursor() - self.BLOBUText = u''.join([chr(i) for i in range(16384)]) - self.BLOBBinary = self.db_module.Binary((u''.join([chr(i) for i in range(256)] * 16)).encode('latin1')) + self.BLOBUText = "".join([chr(i) for i in range(16384)]) + self.BLOBBinary = self.db_module.Binary( + ("".join([chr(i) for i in range(256)] * 16)).encode("latin1") + ) leak_test = True def tearDown(self): if self.leak_test: import gc + del self.cursor orphans = gc.collect() - self.failIf(orphans, "%d orphaned objects found after deleting cursor" % orphans) + self.failIf( + orphans, "%d orphaned objects found after deleting cursor" % orphans + ) del self.connection orphans = gc.collect() - self.failIf(orphans, "%d orphaned objects found after deleting connection" % orphans) + self.failIf( + orphans, "%d orphaned objects found after deleting connection" % orphans + ) def table_exists(self, name): try: - self.cursor.execute('select * from %s where 1=0' % name) - except: + self.cursor.execute("select * from %s where 1=0" % name) + except Exception: return False else: return True @@ -55,7 +61,7 @@ def quote_identifier(self, ident): def new_table_name(self): i = id(self.cursor) while True: - name = self.quote_identifier('tb%08x' % i) + name = self.quote_identifier("tb%08x" % i) if not self.table_exists(name): return name i = i + 1 @@ -71,82 +77,95 @@ def create_table(self, columndefs): """ self.table = self.new_table_name() - self.cursor.execute('CREATE TABLE %s (%s) %s' % - (self.table, - ',\n'.join(columndefs), - self.create_table_extra)) + self.cursor.execute( + "CREATE TABLE %s (%s) %s" + % (self.table, ",\n".join(columndefs), self.create_table_extra) + ) def check_data_integrity(self, columndefs, generator): # insert self.create_table(columndefs) - insert_statement = ('INSERT INTO %s VALUES (%s)' % - (self.table, - ','.join(['%s'] * len(columndefs)))) - data = [ [ generator(i,j) for j in range(len(columndefs)) ] - for i in range(self.rows) ] + insert_statement = "INSERT INTO %s VALUES (%s)" % ( + self.table, + ",".join(["%s"] * len(columndefs)), + ) + data = [ + [generator(i, j) for j in range(len(columndefs))] for i in range(self.rows) + ] self.cursor.executemany(insert_statement, data) self.connection.commit() # verify - self.cursor.execute('select * from %s' % self.table) - l = self.cursor.fetchall() - self.assertEqual(len(l), self.rows) + self.cursor.execute("select * from %s" % self.table) + res = self.cursor.fetchall() + self.assertEqual(len(res), self.rows) try: for i in range(self.rows): for j in range(len(columndefs)): - self.assertEqual(l[i][j], generator(i,j)) + self.assertEqual(res[i][j], generator(i, j)) finally: if not self.debug: - self.cursor.execute('drop table %s' % (self.table)) + self.cursor.execute("drop table %s" % (self.table)) def test_transactions(self): - columndefs = ( 'col1 INT', 'col2 VARCHAR(255)') + columndefs = ("col1 INT", "col2 VARCHAR(255)") + def generator(row, col): - if col == 0: return row - else: return ('%i' % (row%10))*255 + if col == 0: + return row + else: + return ("%i" % (row % 10)) * 255 + self.create_table(columndefs) - insert_statement = ('INSERT INTO %s VALUES (%s)' % - (self.table, - ','.join(['%s'] * len(columndefs)))) - data = [ [ generator(i,j) for j in range(len(columndefs)) ] - for i in range(self.rows) ] + insert_statement = "INSERT INTO %s VALUES (%s)" % ( + self.table, + ",".join(["%s"] * len(columndefs)), + ) + data = [ + [generator(i, j) for j in range(len(columndefs))] for i in range(self.rows) + ] self.cursor.executemany(insert_statement, data) # verify self.connection.commit() - self.cursor.execute('select * from %s' % self.table) - l = self.cursor.fetchall() - self.assertEqual(len(l), self.rows) + self.cursor.execute("select * from %s" % self.table) + res = self.cursor.fetchall() + self.assertEqual(len(res), self.rows) for i in range(self.rows): for j in range(len(columndefs)): - self.assertEqual(l[i][j], generator(i,j)) - delete_statement = 'delete from %s where col1=%%s' % self.table + self.assertEqual(res[i][j], generator(i, j)) + delete_statement = "delete from %s where col1=%%s" % self.table self.cursor.execute(delete_statement, (0,)) - self.cursor.execute('select col1 from %s where col1=%s' % \ - (self.table, 0)) - l = self.cursor.fetchall() - self.assertFalse(l, "DELETE didn't work") + self.cursor.execute("select col1 from %s where col1=%s" % (self.table, 0)) + res = self.cursor.fetchall() + self.assertFalse(res, "DELETE didn't work") self.connection.rollback() - self.cursor.execute('select col1 from %s where col1=%s' % \ - (self.table, 0)) - l = self.cursor.fetchall() - self.assertTrue(len(l) == 1, "ROLLBACK didn't work") - self.cursor.execute('drop table %s' % (self.table)) + self.cursor.execute("select col1 from %s where col1=%s" % (self.table, 0)) + res = self.cursor.fetchall() + self.assertTrue(len(res) == 1, "ROLLBACK didn't work") + self.cursor.execute("drop table %s" % (self.table)) def test_truncation(self): - columndefs = ( 'col1 INT', 'col2 VARCHAR(255)') + columndefs = ("col1 INT", "col2 VARCHAR(255)") + def generator(row, col): - if col == 0: return row - else: return ('%i' % (row%10))*((255-self.rows//2)+row) + if col == 0: + return row + else: + return ("%i" % (row % 10)) * ((255 - self.rows // 2) + row) + self.create_table(columndefs) - insert_statement = ('INSERT INTO %s VALUES (%s)' % - (self.table, - ','.join(['%s'] * len(columndefs)))) + insert_statement = "INSERT INTO %s VALUES (%s)" % ( + self.table, + ",".join(["%s"] * len(columndefs)), + ) try: - self.cursor.execute(insert_statement, (0, '0'*256)) + self.cursor.execute(insert_statement, (0, "0" * 256)) except self.connection.DataError: pass else: - self.fail("Over-long column did not generate warnings/exception with single insert") + self.fail( + "Over-long column did not generate warnings/exception with single insert" # noqa: E501 + ) self.connection.rollback() @@ -154,143 +173,145 @@ def generator(row, col): for i in range(self.rows): data = [] for j in range(len(columndefs)): - data.append(generator(i,j)) - self.cursor.execute(insert_statement,tuple(data)) + data.append(generator(i, j)) + self.cursor.execute(insert_statement, tuple(data)) except self.connection.DataError: pass else: - self.fail("Over-long columns did not generate warnings/exception with execute()") + self.fail( + "Over-long columns did not generate warnings/exception with execute()" # noqa: E501 + ) self.connection.rollback() try: - data = [ [ generator(i,j) for j in range(len(columndefs)) ] - for i in range(self.rows) ] + data = [ + [generator(i, j) for j in range(len(columndefs))] + for i in range(self.rows) + ] self.cursor.executemany(insert_statement, data) except self.connection.DataError: pass else: - self.fail("Over-long columns did not generate warnings/exception with executemany()") + self.fail( + "Over-long columns did not generate warnings/exception with executemany()" # noqa: E501 + ) self.connection.rollback() - self.cursor.execute('drop table %s' % (self.table)) + self.cursor.execute("drop table %s" % (self.table)) def test_CHAR(self): # Character data - def generator(row,col): - return ('%i' % ((row+col) % 10)) * 255 - self.check_data_integrity( - ('col1 char(255)','col2 char(255)'), - generator) + def generator(row, col): + return ("%i" % ((row + col) % 10)) * 255 + + self.check_data_integrity(("col1 char(255)", "col2 char(255)"), generator) def test_INT(self): # Number data - def generator(row,col): - return row*row - self.check_data_integrity( - ('col1 INT',), - generator) + def generator(row, col): + return row * row + + self.check_data_integrity(("col1 INT",), generator) def test_DECIMAL(self): # DECIMAL from decimal import Decimal - def generator(row,col): + + def generator(row, col): return Decimal("%d.%02d" % (row, col)) - self.check_data_integrity( - ('col1 DECIMAL(5,2)',), - generator) - val = Decimal('1.11111111111111119E-7') - self.cursor.execute('SELECT %s', (val,)) + self.check_data_integrity(("col1 DECIMAL(5,2)",), generator) + + val = Decimal("1.11111111111111119E-7") + self.cursor.execute("SELECT %s", (val,)) result = self.cursor.fetchone()[0] self.assertEqual(result, val) self.assertIsInstance(result, Decimal) - self.cursor.execute('SELECT %s + %s', (Decimal('0.1'), Decimal('0.2'))) + self.cursor.execute("SELECT %s + %s", (Decimal("0.1"), Decimal("0.2"))) result = self.cursor.fetchone()[0] - self.assertEqual(result, Decimal('0.3')) + self.assertEqual(result, Decimal("0.3")) self.assertIsInstance(result, Decimal) def test_DATE(self): ticks = time() - def generator(row,col): - return self.db_module.DateFromTicks(ticks+row*86400-col*1313) - self.check_data_integrity( - ('col1 DATE',), - generator) + + def generator(row, col): + return self.db_module.DateFromTicks(ticks + row * 86400 - col * 1313) + + self.check_data_integrity(("col1 DATE",), generator) def test_TIME(self): ticks = time() - def generator(row,col): - return self.db_module.TimeFromTicks(ticks+row*86400-col*1313) - self.check_data_integrity( - ('col1 TIME',), - generator) + + def generator(row, col): + return self.db_module.TimeFromTicks(ticks + row * 86400 - col * 1313) + + self.check_data_integrity(("col1 TIME",), generator) def test_DATETIME(self): ticks = time() - def generator(row,col): - return self.db_module.TimestampFromTicks(ticks+row*86400-col*1313) - self.check_data_integrity( - ('col1 DATETIME',), - generator) + + def generator(row, col): + return self.db_module.TimestampFromTicks(ticks + row * 86400 - col * 1313) + + self.check_data_integrity(("col1 DATETIME",), generator) def test_TIMESTAMP(self): ticks = time() - def generator(row,col): - return self.db_module.TimestampFromTicks(ticks+row*86400-col*1313) - self.check_data_integrity( - ('col1 TIMESTAMP',), - generator) + + def generator(row, col): + return self.db_module.TimestampFromTicks(ticks + row * 86400 - col * 1313) + + self.check_data_integrity(("col1 TIMESTAMP",), generator) def test_fractional_TIMESTAMP(self): ticks = time() - def generator(row,col): - return self.db_module.TimestampFromTicks(ticks+row*86400-col*1313+row*0.7*col/3.0) - self.check_data_integrity( - ('col1 TIMESTAMP',), - generator) + + def generator(row, col): + return self.db_module.TimestampFromTicks( + ticks + row * 86400 - col * 1313 + row * 0.7 * col / 3.0 + ) + + self.check_data_integrity(("col1 TIMESTAMP",), generator) def test_LONG(self): - def generator(row,col): + def generator(row, col): if col == 0: return row else: - return self.BLOBUText # 'BLOB Text ' * 1024 - self.check_data_integrity( - ('col1 INT','col2 LONG'), - generator) + return self.BLOBUText # 'BLOB Text ' * 1024 + + self.check_data_integrity(("col1 INT", "col2 LONG"), generator) def test_TEXT(self): - def generator(row,col): - return self.BLOBUText # 'BLOB Text ' * 1024 - self.check_data_integrity( - ('col2 TEXT',), - generator) + def generator(row, col): + return self.BLOBUText # 'BLOB Text ' * 1024 + + self.check_data_integrity(("col2 TEXT",), generator) def test_LONG_BYTE(self): - def generator(row,col): + def generator(row, col): if col == 0: return row else: - return self.BLOBBinary # 'BLOB\000Binary ' * 1024 - self.check_data_integrity( - ('col1 INT','col2 LONG BYTE'), - generator) + return self.BLOBBinary # 'BLOB\000Binary ' * 1024 + + self.check_data_integrity(("col1 INT", "col2 LONG BYTE"), generator) def test_BLOB(self): - def generator(row,col): + def generator(row, col): if col == 0: return row else: - return self.BLOBBinary # 'BLOB\000Binary ' * 1024 - self.check_data_integrity( - ('col1 INT','col2 BLOB'), - generator) + return self.BLOBBinary # 'BLOB\000Binary ' * 1024 + + self.check_data_integrity(("col1 INT", "col2 BLOB"), generator) def test_DOUBLE(self): for val in (18014398509481982.0, 0.1): - self.cursor.execute('SELECT %s', (val,)); + self.cursor.execute("SELECT %s", (val,)) result = self.cursor.fetchone()[0] self.assertEqual(result, val) self.assertIsInstance(result, float) diff --git a/tests/configdb.py b/tests/configdb.py index 307cc3f4..f3a56e24 100644 --- a/tests/configdb.py +++ b/tests/configdb.py @@ -3,12 +3,9 @@ from os import environ, path tests_path = path.dirname(__file__) -conf_file = environ.get('TESTDB', 'default.cnf') +conf_file = environ.get("TESTDB", "default.cnf") conf_path = path.join(tests_path, conf_file) -connect_kwargs = dict( - read_default_file = conf_path, - read_default_group = "MySQLdb-tests", -) +connect_kwargs = dict(read_default_file=conf_path, read_default_group="MySQLdb-tests",) def connection_kwargs(kwargs): @@ -19,6 +16,7 @@ def connection_kwargs(kwargs): def connection_factory(**kwargs): import MySQLdb + db_kwargs = connection_kwargs(kwargs) db = MySQLdb.connect(**db_kwargs) return db diff --git a/tests/dbapi20.py b/tests/dbapi20.py index 79c188a5..0ca8bce6 100644 --- a/tests/dbapi20.py +++ b/tests/dbapi20.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -''' Python DB API 2.0 driver compliance unit test suite. +""" Python DB API 2.0 driver compliance unit test suite. This software is Public Domain and may be used without restrictions. @@ -9,11 +9,11 @@ this is turning out to be a thoroughly unwholesome unit test." -- Ian Bicking -''' +""" -__rcs_id__ = '$Id$' -__version__ = '$Revision$'[11:-2] -__author__ = 'Stuart Bishop ' +__rcs_id__ = "$Id$" +__version__ = "$Revision$"[11:-2] +__author__ = "Stuart Bishop " import unittest import time @@ -64,8 +64,9 @@ # - Fix bugs in test_setoutputsize_basic and test_setinputsizes # + class DatabaseAPI20Test(unittest.TestCase): - ''' Test a database self.driver for DB API 2.0 compatibility. + """ Test a database self.driver for DB API 2.0 compatibility. This implementation tests Gadfly, but the TestCase is structured so that other self.drivers can subclass this test case to ensure compiliance with the DB-API. It is @@ -84,45 +85,45 @@ class mytest(dbapi20.DatabaseAPI20Test): Don't 'import DatabaseAPI20Test from dbapi20', or you will confuse the unit tester - just 'import dbapi20'. - ''' + """ # The self.driver module. This should be the module where the 'connect' # method is to be found driver = None - connect_args = () # List of arguments to pass to connect - connect_kw_args = {} # Keyword arguments for connect - table_prefix = 'dbapi20test_' # If you need to specify a prefix for tables + connect_args = () # List of arguments to pass to connect + connect_kw_args = {} # Keyword arguments for connect + table_prefix = "dbapi20test_" # If you need to specify a prefix for tables - ddl1 = 'create table %sbooze (name varchar(20))' % table_prefix - ddl2 = 'create table %sbarflys (name varchar(20))' % table_prefix - xddl1 = 'drop table %sbooze' % table_prefix - xddl2 = 'drop table %sbarflys' % table_prefix + ddl1 = "create table %sbooze (name varchar(20))" % table_prefix + ddl2 = "create table %sbarflys (name varchar(20))" % table_prefix + xddl1 = "drop table %sbooze" % table_prefix + xddl2 = "drop table %sbarflys" % table_prefix - lowerfunc = 'lower' # Name of stored procedure to convert string->lowercase + lowerfunc = "lower" # Name of stored procedure to convert string->lowercase # Some drivers may need to override these helpers, for example adding # a 'commit' after the execute. - def executeDDL1(self,cursor): + def executeDDL1(self, cursor): cursor.execute(self.ddl1) - def executeDDL2(self,cursor): + def executeDDL2(self, cursor): cursor.execute(self.ddl2) def setUp(self): - ''' self.drivers should override this method to perform required setup + """ self.drivers should override this method to perform required setup if any is necessary, such as creating the database. - ''' + """ pass def tearDown(self): - ''' self.drivers should override this method to perform required cleanup + """ self.drivers should override this method to perform required cleanup if any is necessary, such as deleting the test database. The default drops the tables that may be created. - ''' + """ con = self._connect() try: cur = con.cursor() - for ddl in (self.xddl1,self.xddl2): + for ddl in (self.xddl1, self.xddl2): try: cur.execute(ddl) con.commit() @@ -135,9 +136,7 @@ def tearDown(self): def _connect(self): try: - return self.driver.connect( - *self.connect_args,**self.connect_kw_args - ) + return self.driver.connect(*self.connect_args, **self.connect_kw_args) except AttributeError: self.fail("No connect method found in self.driver module") @@ -150,7 +149,7 @@ def test_apilevel(self): # Must exist apilevel = self.driver.apilevel # Must equal 2.0 - self.assertEqual(apilevel,'2.0') + self.assertEqual(apilevel, "2.0") except AttributeError: self.fail("Driver doesn't define apilevel") @@ -159,7 +158,7 @@ def test_threadsafety(self): # Must exist threadsafety = self.driver.threadsafety # Must be a valid value - self.assertTrue(threadsafety in (0,1,2,3)) + self.assertTrue(threadsafety in (0, 1, 2, 3)) except AttributeError: self.fail("Driver doesn't define threadsafety") @@ -168,38 +167,24 @@ def test_paramstyle(self): # Must exist paramstyle = self.driver.paramstyle # Must be a valid value - self.assertTrue(paramstyle in ( - 'qmark','numeric','named','format','pyformat' - )) + self.assertTrue( + paramstyle in ("qmark", "numeric", "named", "format", "pyformat") + ) except AttributeError: self.fail("Driver doesn't define paramstyle") def test_Exceptions(self): # Make sure required exceptions exist, and are in the # defined hierarchy. - self.assertTrue(issubclass(self.driver.Warning,Exception)) - self.assertTrue(issubclass(self.driver.Error,Exception)) - self.assertTrue( - issubclass(self.driver.InterfaceError,self.driver.Error) - ) - self.assertTrue( - issubclass(self.driver.DatabaseError,self.driver.Error) - ) - self.assertTrue( - issubclass(self.driver.OperationalError,self.driver.Error) - ) - self.assertTrue( - issubclass(self.driver.IntegrityError,self.driver.Error) - ) - self.assertTrue( - issubclass(self.driver.InternalError,self.driver.Error) - ) - self.assertTrue( - issubclass(self.driver.ProgrammingError,self.driver.Error) - ) - self.assertTrue( - issubclass(self.driver.NotSupportedError,self.driver.Error) - ) + self.assertTrue(issubclass(self.driver.Warning, Exception)) + self.assertTrue(issubclass(self.driver.Error, Exception)) + self.assertTrue(issubclass(self.driver.InterfaceError, self.driver.Error)) + self.assertTrue(issubclass(self.driver.DatabaseError, self.driver.Error)) + self.assertTrue(issubclass(self.driver.OperationalError, self.driver.Error)) + self.assertTrue(issubclass(self.driver.IntegrityError, self.driver.Error)) + self.assertTrue(issubclass(self.driver.InternalError, self.driver.Error)) + self.assertTrue(issubclass(self.driver.ProgrammingError, self.driver.Error)) + self.assertTrue(issubclass(self.driver.NotSupportedError, self.driver.Error)) def test_ExceptionsAsConnectionAttributes(self): # OPTIONAL EXTENSION @@ -220,7 +205,6 @@ def test_ExceptionsAsConnectionAttributes(self): self.assertTrue(con.ProgrammingError is drv.ProgrammingError) self.assertTrue(con.NotSupportedError is drv.NotSupportedError) - def test_commit(self): con = self._connect() try: @@ -233,7 +217,7 @@ def test_rollback(self): con = self._connect() # If rollback is defined, it should either work or throw # the documented exception - if hasattr(con,'rollback'): + if hasattr(con, "rollback"): try: con.rollback() except self.driver.NotSupportedError: @@ -242,7 +226,7 @@ def test_rollback(self): def test_cursor(self): con = self._connect() try: - cur = con.cursor() + _ = con.cursor() finally: con.close() @@ -254,14 +238,14 @@ def test_cursor_isolation(self): cur1 = con.cursor() cur2 = con.cursor() self.executeDDL1(cur1) - cur1.execute("insert into %sbooze values ('Victoria Bitter')" % ( - self.table_prefix - )) + cur1.execute( + "insert into %sbooze values ('Victoria Bitter')" % (self.table_prefix) + ) cur2.execute("select name from %sbooze" % self.table_prefix) booze = cur2.fetchall() - self.assertEqual(len(booze),1) - self.assertEqual(len(booze[0]),1) - self.assertEqual(booze[0][0],'Victoria Bitter') + self.assertEqual(len(booze), 1) + self.assertEqual(len(booze[0]), 1) + self.assertEqual(booze[0][0], "Victoria Bitter") finally: con.close() @@ -270,31 +254,41 @@ def test_description(self): try: cur = con.cursor() self.executeDDL1(cur) - self.assertEqual(cur.description,None, - 'cursor.description should be none after executing a ' - 'statement that can return no rows (such as DDL)' - ) - cur.execute('select name from %sbooze' % self.table_prefix) - self.assertEqual(len(cur.description),1, - 'cursor.description describes too many columns' - ) - self.assertEqual(len(cur.description[0]),7, - 'cursor.description[x] tuples must have 7 elements' - ) - self.assertEqual(cur.description[0][0].lower(),'name', - 'cursor.description[x][0] must return column name' - ) - self.assertEqual(cur.description[0][1],self.driver.STRING, - 'cursor.description[x][1] must return column type. Got %r' - % cur.description[0][1] - ) + self.assertEqual( + cur.description, + None, + "cursor.description should be none after executing a " + "statement that can return no rows (such as DDL)", + ) + cur.execute("select name from %sbooze" % self.table_prefix) + self.assertEqual( + len(cur.description), 1, "cursor.description describes too many columns" + ) + self.assertEqual( + len(cur.description[0]), + 7, + "cursor.description[x] tuples must have 7 elements", + ) + self.assertEqual( + cur.description[0][0].lower(), + "name", + "cursor.description[x][0] must return column name", + ) + self.assertEqual( + cur.description[0][1], + self.driver.STRING, + "cursor.description[x][1] must return column type. Got %r" + % cur.description[0][1], + ) # Make sure self.description gets reset self.executeDDL2(cur) - self.assertEqual(cur.description,None, - 'cursor.description not being set to None when executing ' - 'no-result statements (eg. DDL)' - ) + self.assertEqual( + cur.description, + None, + "cursor.description not being set to None when executing " + "no-result statements (eg. DDL)", + ) finally: con.close() @@ -303,47 +297,49 @@ def test_rowcount(self): try: cur = con.cursor() self.executeDDL1(cur) - self.assertEqual(cur.rowcount,-1, - 'cursor.rowcount should be -1 after executing no-result ' - 'statements' - ) - cur.execute("insert into %sbooze values ('Victoria Bitter')" % ( - self.table_prefix - )) - self.assertTrue(cur.rowcount in (-1,1), - 'cursor.rowcount should == number or rows inserted, or ' - 'set to -1 after executing an insert statement' - ) + self.assertEqual( + cur.rowcount, + -1, + "cursor.rowcount should be -1 after executing no-result " "statements", + ) + cur.execute( + "insert into %sbooze values ('Victoria Bitter')" % (self.table_prefix) + ) + self.assertTrue( + cur.rowcount in (-1, 1), + "cursor.rowcount should == number or rows inserted, or " + "set to -1 after executing an insert statement", + ) cur.execute("select name from %sbooze" % self.table_prefix) - self.assertTrue(cur.rowcount in (-1,1), - 'cursor.rowcount should == number of rows returned, or ' - 'set to -1 after executing a select statement' - ) + self.assertTrue( + cur.rowcount in (-1, 1), + "cursor.rowcount should == number of rows returned, or " + "set to -1 after executing a select statement", + ) self.executeDDL2(cur) - self.assertEqual(cur.rowcount,-1, - 'cursor.rowcount not being reset to -1 after executing ' - 'no-result statements' - ) + self.assertEqual( + cur.rowcount, + -1, + "cursor.rowcount not being reset to -1 after executing " + "no-result statements", + ) finally: con.close() - lower_func = 'lower' + lower_func = "lower" + def test_callproc(self): con = self._connect() try: cur = con.cursor() - if self.lower_func and hasattr(cur,'callproc'): - r = cur.callproc(self.lower_func,('FOO',)) - self.assertEqual(len(r),1) - self.assertEqual(r[0],'FOO') + if self.lower_func and hasattr(cur, "callproc"): + r = cur.callproc(self.lower_func, ("FOO",)) + self.assertEqual(len(r), 1) + self.assertEqual(r[0], "FOO") r = cur.fetchall() - self.assertEqual(len(r),1,'callproc produced no result set') - self.assertEqual(len(r[0]),1, - 'callproc produced invalid result set' - ) - self.assertEqual(r[0][0],'foo', - 'callproc produced invalid results' - ) + self.assertEqual(len(r), 1, "callproc produced no result set") + self.assertEqual(len(r[0]), 1, "callproc produced invalid result set") + self.assertEqual(r[0][0], "foo", "callproc produced invalid results") finally: con.close() @@ -356,14 +352,14 @@ def test_close(self): # cursor.execute should raise an Error if called after connection # closed - self.assertRaises(self.driver.Error,self.executeDDL1,cur) + self.assertRaises(self.driver.Error, self.executeDDL1, cur) # connection.commit should raise an Error if called after connection' # closed.' - self.assertRaises(self.driver.Error,con.commit) + self.assertRaises(self.driver.Error, con.commit) # connection.close should raise an Error if called more than once - self.assertRaises(self.driver.Error,con.close) + self.assertRaises(self.driver.Error, con.close) def test_execute(self): con = self._connect() @@ -373,105 +369,99 @@ def test_execute(self): finally: con.close() - def _paraminsert(self,cur): + def _paraminsert(self, cur): self.executeDDL1(cur) - cur.execute("insert into %sbooze values ('Victoria Bitter')" % ( - self.table_prefix - )) - self.assertTrue(cur.rowcount in (-1,1)) + cur.execute( + "insert into %sbooze values ('Victoria Bitter')" % (self.table_prefix) + ) + self.assertTrue(cur.rowcount in (-1, 1)) - if self.driver.paramstyle == 'qmark': + if self.driver.paramstyle == "qmark": cur.execute( - 'insert into %sbooze values (?)' % self.table_prefix, - ("Cooper's",) - ) - elif self.driver.paramstyle == 'numeric': + "insert into %sbooze values (?)" % self.table_prefix, ("Cooper's",) + ) + elif self.driver.paramstyle == "numeric": cur.execute( - 'insert into %sbooze values (:1)' % self.table_prefix, - ("Cooper's",) - ) - elif self.driver.paramstyle == 'named': + "insert into %sbooze values (:1)" % self.table_prefix, ("Cooper's",) + ) + elif self.driver.paramstyle == "named": cur.execute( - 'insert into %sbooze values (:beer)' % self.table_prefix, - {'beer':"Cooper's"} - ) - elif self.driver.paramstyle == 'format': + "insert into %sbooze values (:beer)" % self.table_prefix, + {"beer": "Cooper's"}, + ) + elif self.driver.paramstyle == "format": cur.execute( - 'insert into %sbooze values (%%s)' % self.table_prefix, - ("Cooper's",) - ) - elif self.driver.paramstyle == 'pyformat': + "insert into %sbooze values (%%s)" % self.table_prefix, ("Cooper's",) + ) + elif self.driver.paramstyle == "pyformat": cur.execute( - 'insert into %sbooze values (%%(beer)s)' % self.table_prefix, - {'beer':"Cooper's"} - ) + "insert into %sbooze values (%%(beer)s)" % self.table_prefix, + {"beer": "Cooper's"}, + ) else: - self.fail('Invalid paramstyle') - self.assertTrue(cur.rowcount in (-1,1)) + self.fail("Invalid paramstyle") + self.assertTrue(cur.rowcount in (-1, 1)) - cur.execute('select name from %sbooze' % self.table_prefix) + cur.execute("select name from %sbooze" % self.table_prefix) res = cur.fetchall() - self.assertEqual(len(res),2,'cursor.fetchall returned too few rows') - beers = [res[0][0],res[1][0]] + self.assertEqual(len(res), 2, "cursor.fetchall returned too few rows") + beers = [res[0][0], res[1][0]] beers.sort() - self.assertEqual(beers[0],"Cooper's", - 'cursor.fetchall retrieved incorrect data, or data inserted ' - 'incorrectly' - ) - self.assertEqual(beers[1],"Victoria Bitter", - 'cursor.fetchall retrieved incorrect data, or data inserted ' - 'incorrectly' - ) + self.assertEqual( + beers[0], + "Cooper's", + "cursor.fetchall retrieved incorrect data, or data inserted " "incorrectly", + ) + self.assertEqual( + beers[1], + "Victoria Bitter", + "cursor.fetchall retrieved incorrect data, or data inserted " "incorrectly", + ) def test_executemany(self): con = self._connect() try: cur = con.cursor() self.executeDDL1(cur) - largs = [ ("Cooper's",) , ("Boag's",) ] - margs = [ {'beer': "Cooper's"}, {'beer': "Boag's"} ] - if self.driver.paramstyle == 'qmark': + largs = [("Cooper's",), ("Boag's",)] + margs = [{"beer": "Cooper's"}, {"beer": "Boag's"}] + if self.driver.paramstyle == "qmark": cur.executemany( - 'insert into %sbooze values (?)' % self.table_prefix, - largs - ) - elif self.driver.paramstyle == 'numeric': + "insert into %sbooze values (?)" % self.table_prefix, largs + ) + elif self.driver.paramstyle == "numeric": cur.executemany( - 'insert into %sbooze values (:1)' % self.table_prefix, - largs - ) - elif self.driver.paramstyle == 'named': + "insert into %sbooze values (:1)" % self.table_prefix, largs + ) + elif self.driver.paramstyle == "named": cur.executemany( - 'insert into %sbooze values (:beer)' % self.table_prefix, - margs - ) - elif self.driver.paramstyle == 'format': + "insert into %sbooze values (:beer)" % self.table_prefix, margs + ) + elif self.driver.paramstyle == "format": cur.executemany( - 'insert into %sbooze values (%%s)' % self.table_prefix, - largs - ) - elif self.driver.paramstyle == 'pyformat': + "insert into %sbooze values (%%s)" % self.table_prefix, largs + ) + elif self.driver.paramstyle == "pyformat": cur.executemany( - 'insert into %sbooze values (%%(beer)s)' % ( - self.table_prefix - ), - margs - ) - else: - self.fail('Unknown paramstyle') - self.assertTrue(cur.rowcount in (-1,2), - 'insert using cursor.executemany set cursor.rowcount to ' - 'incorrect value %r' % cur.rowcount + "insert into %sbooze values (%%(beer)s)" % (self.table_prefix), + margs, ) - cur.execute('select name from %sbooze' % self.table_prefix) + else: + self.fail("Unknown paramstyle") + self.assertTrue( + cur.rowcount in (-1, 2), + "insert using cursor.executemany set cursor.rowcount to " + "incorrect value %r" % cur.rowcount, + ) + cur.execute("select name from %sbooze" % self.table_prefix) res = cur.fetchall() - self.assertEqual(len(res),2, - 'cursor.fetchall retrieved incorrect number of rows' - ) - beers = [res[0][0],res[1][0]] + self.assertEqual( + len(res), 2, "cursor.fetchall retrieved incorrect number of rows" + ) + beers = [res[0][0], res[1][0]] beers.sort() - self.assertEqual(beers[0],"Boag's",'incorrect data retrieved') - self.assertEqual(beers[1],"Cooper's",'incorrect data retrieved') + self.assertEqual(beers[0], "Boag's", "incorrect data retrieved") + self.assertEqual(beers[1], "Cooper's", "incorrect data retrieved") finally: con.close() @@ -482,59 +472,62 @@ def test_fetchone(self): # cursor.fetchone should raise an Error if called before # executing a select-type query - self.assertRaises(self.driver.Error,cur.fetchone) + self.assertRaises(self.driver.Error, cur.fetchone) # cursor.fetchone should raise an Error if called after # executing a query that cannot return rows self.executeDDL1(cur) - self.assertRaises(self.driver.Error,cur.fetchone) + self.assertRaises(self.driver.Error, cur.fetchone) - cur.execute('select name from %sbooze' % self.table_prefix) - self.assertEqual(cur.fetchone(),None, - 'cursor.fetchone should return None if a query retrieves ' - 'no rows' - ) - self.assertTrue(cur.rowcount in (-1,0)) + cur.execute("select name from %sbooze" % self.table_prefix) + self.assertEqual( + cur.fetchone(), + None, + "cursor.fetchone should return None if a query retrieves " "no rows", + ) + self.assertTrue(cur.rowcount in (-1, 0)) # cursor.fetchone should raise an Error if called after # executing a query that cannot return rows - cur.execute("insert into %sbooze values ('Victoria Bitter')" % ( - self.table_prefix - )) - self.assertRaises(self.driver.Error,cur.fetchone) + cur.execute( + "insert into %sbooze values ('Victoria Bitter')" % (self.table_prefix) + ) + self.assertRaises(self.driver.Error, cur.fetchone) - cur.execute('select name from %sbooze' % self.table_prefix) + cur.execute("select name from %sbooze" % self.table_prefix) r = cur.fetchone() - self.assertEqual(len(r),1, - 'cursor.fetchone should have retrieved a single row' - ) - self.assertEqual(r[0],'Victoria Bitter', - 'cursor.fetchone retrieved incorrect data' - ) - self.assertEqual(cur.fetchone(),None, - 'cursor.fetchone should return None if no more rows available' - ) - self.assertTrue(cur.rowcount in (-1,1)) + self.assertEqual( + len(r), 1, "cursor.fetchone should have retrieved a single row" + ) + self.assertEqual( + r[0], "Victoria Bitter", "cursor.fetchone retrieved incorrect data" + ) + self.assertEqual( + cur.fetchone(), + None, + "cursor.fetchone should return None if no more rows available", + ) + self.assertTrue(cur.rowcount in (-1, 1)) finally: con.close() samples = [ - 'Carlton Cold', - 'Carlton Draft', - 'Mountain Goat', - 'Redback', - 'Victoria Bitter', - 'XXXX' - ] + "Carlton Cold", + "Carlton Draft", + "Mountain Goat", + "Redback", + "Victoria Bitter", + "XXXX", + ] def _populate(self): - ''' Return a list of sql commands to setup the DB for the fetch + """ Return a list of sql commands to setup the DB for the fetch tests. - ''' + """ populate = [ - "insert into %sbooze values ('%s')" % (self.table_prefix,s) - for s in self.samples - ] + "insert into {}booze values ('{}')".format(self.table_prefix, s) + for s in self.samples + ] return populate def test_fetchmany(self): @@ -543,78 +536,88 @@ def test_fetchmany(self): cur = con.cursor() # cursor.fetchmany should raise an Error if called without - #issuing a query - self.assertRaises(self.driver.Error,cur.fetchmany,4) + # issuing a query + self.assertRaises(self.driver.Error, cur.fetchmany, 4) self.executeDDL1(cur) for sql in self._populate(): cur.execute(sql) - cur.execute('select name from %sbooze' % self.table_prefix) + cur.execute("select name from %sbooze" % self.table_prefix) r = cur.fetchmany() - self.assertEqual(len(r),1, - 'cursor.fetchmany retrieved incorrect number of rows, ' - 'default of arraysize is one.' - ) - cur.arraysize=10 - r = cur.fetchmany(3) # Should get 3 rows - self.assertEqual(len(r),3, - 'cursor.fetchmany retrieved incorrect number of rows' - ) - r = cur.fetchmany(4) # Should get 2 more - self.assertEqual(len(r),2, - 'cursor.fetchmany retrieved incorrect number of rows' - ) - r = cur.fetchmany(4) # Should be an empty sequence - self.assertEqual(len(r),0, - 'cursor.fetchmany should return an empty sequence after ' - 'results are exhausted' + self.assertEqual( + len(r), + 1, + "cursor.fetchmany retrieved incorrect number of rows, " + "default of arraysize is one.", + ) + cur.arraysize = 10 + r = cur.fetchmany(3) # Should get 3 rows + self.assertEqual( + len(r), 3, "cursor.fetchmany retrieved incorrect number of rows" + ) + r = cur.fetchmany(4) # Should get 2 more + self.assertEqual( + len(r), 2, "cursor.fetchmany retrieved incorrect number of rows" ) - self.assertTrue(cur.rowcount in (-1,6)) + r = cur.fetchmany(4) # Should be an empty sequence + self.assertEqual( + len(r), + 0, + "cursor.fetchmany should return an empty sequence after " + "results are exhausted", + ) + self.assertTrue(cur.rowcount in (-1, 6)) # Same as above, using cursor.arraysize - cur.arraysize=4 - cur.execute('select name from %sbooze' % self.table_prefix) - r = cur.fetchmany() # Should get 4 rows - self.assertEqual(len(r),4, - 'cursor.arraysize not being honoured by fetchmany' - ) - r = cur.fetchmany() # Should get 2 more - self.assertEqual(len(r),2) - r = cur.fetchmany() # Should be an empty sequence - self.assertEqual(len(r),0) - self.assertTrue(cur.rowcount in (-1,6)) - - cur.arraysize=6 - cur.execute('select name from %sbooze' % self.table_prefix) - rows = cur.fetchmany() # Should get all rows - self.assertTrue(cur.rowcount in (-1,6)) - self.assertEqual(len(rows),6) - self.assertEqual(len(rows),6) + cur.arraysize = 4 + cur.execute("select name from %sbooze" % self.table_prefix) + r = cur.fetchmany() # Should get 4 rows + self.assertEqual( + len(r), 4, "cursor.arraysize not being honoured by fetchmany" + ) + r = cur.fetchmany() # Should get 2 more + self.assertEqual(len(r), 2) + r = cur.fetchmany() # Should be an empty sequence + self.assertEqual(len(r), 0) + self.assertTrue(cur.rowcount in (-1, 6)) + + cur.arraysize = 6 + cur.execute("select name from %sbooze" % self.table_prefix) + rows = cur.fetchmany() # Should get all rows + self.assertTrue(cur.rowcount in (-1, 6)) + self.assertEqual(len(rows), 6) + self.assertEqual(len(rows), 6) rows = [r[0] for r in rows] rows.sort() # Make sure we get the right data back out - for i in range(0,6): - self.assertEqual(rows[i],self.samples[i], - 'incorrect data retrieved by cursor.fetchmany' - ) - - rows = cur.fetchmany() # Should return an empty list - self.assertEqual(len(rows),0, - 'cursor.fetchmany should return an empty sequence if ' - 'called after the whole result set has been fetched' + for i in range(0, 6): + self.assertEqual( + rows[i], + self.samples[i], + "incorrect data retrieved by cursor.fetchmany", ) - self.assertTrue(cur.rowcount in (-1,6)) + + rows = cur.fetchmany() # Should return an empty list + self.assertEqual( + len(rows), + 0, + "cursor.fetchmany should return an empty sequence if " + "called after the whole result set has been fetched", + ) + self.assertTrue(cur.rowcount in (-1, 6)) self.executeDDL2(cur) - cur.execute('select name from %sbarflys' % self.table_prefix) - r = cur.fetchmany() # Should get empty sequence - self.assertEqual(len(r),0, - 'cursor.fetchmany should return an empty sequence if ' - 'query retrieved no rows' - ) - self.assertTrue(cur.rowcount in (-1,0)) + cur.execute("select name from %sbarflys" % self.table_prefix) + r = cur.fetchmany() # Should get empty sequence + self.assertEqual( + len(r), + 0, + "cursor.fetchmany should return an empty sequence if " + "query retrieved no rows", + ) + self.assertTrue(cur.rowcount in (-1, 0)) finally: con.close() @@ -634,36 +637,41 @@ def test_fetchall(self): # cursor.fetchall should raise an Error if called # after executing a statement that cannot return rows - self.assertRaises(self.driver.Error,cur.fetchall) + self.assertRaises(self.driver.Error, cur.fetchall) - cur.execute('select name from %sbooze' % self.table_prefix) + cur.execute("select name from %sbooze" % self.table_prefix) rows = cur.fetchall() - self.assertTrue(cur.rowcount in (-1,len(self.samples))) - self.assertEqual(len(rows),len(self.samples), - 'cursor.fetchall did not retrieve all rows' - ) + self.assertTrue(cur.rowcount in (-1, len(self.samples))) + self.assertEqual( + len(rows), + len(self.samples), + "cursor.fetchall did not retrieve all rows", + ) rows = [r[0] for r in rows] rows.sort() - for i in range(0,len(self.samples)): - self.assertEqual(rows[i],self.samples[i], - 'cursor.fetchall retrieved incorrect rows' + for i in range(0, len(self.samples)): + self.assertEqual( + rows[i], self.samples[i], "cursor.fetchall retrieved incorrect rows" ) rows = cur.fetchall() self.assertEqual( - len(rows),0, - 'cursor.fetchall should return an empty list if called ' - 'after the whole result set has been fetched' - ) - self.assertTrue(cur.rowcount in (-1,len(self.samples))) + len(rows), + 0, + "cursor.fetchall should return an empty list if called " + "after the whole result set has been fetched", + ) + self.assertTrue(cur.rowcount in (-1, len(self.samples))) self.executeDDL2(cur) - cur.execute('select name from %sbarflys' % self.table_prefix) + cur.execute("select name from %sbarflys" % self.table_prefix) rows = cur.fetchall() - self.assertTrue(cur.rowcount in (-1,0)) - self.assertEqual(len(rows),0, - 'cursor.fetchall should return an empty list if ' - 'a select query returns no rows' - ) + self.assertTrue(cur.rowcount in (-1, 0)) + self.assertEqual( + len(rows), + 0, + "cursor.fetchall should return an empty list if " + "a select query returns no rows", + ) finally: con.close() @@ -676,91 +684,91 @@ def test_mixedfetch(self): for sql in self._populate(): cur.execute(sql) - cur.execute('select name from %sbooze' % self.table_prefix) - rows1 = cur.fetchone() + cur.execute("select name from %sbooze" % self.table_prefix) + rows1 = cur.fetchone() rows23 = cur.fetchmany(2) - rows4 = cur.fetchone() + rows4 = cur.fetchone() rows56 = cur.fetchall() - self.assertTrue(cur.rowcount in (-1,6)) - self.assertEqual(len(rows23),2, - 'fetchmany returned incorrect number of rows' - ) - self.assertEqual(len(rows56),2, - 'fetchall returned incorrect number of rows' - ) + self.assertTrue(cur.rowcount in (-1, 6)) + self.assertEqual( + len(rows23), 2, "fetchmany returned incorrect number of rows" + ) + self.assertEqual( + len(rows56), 2, "fetchall returned incorrect number of rows" + ) rows = [rows1[0]] - rows.extend([rows23[0][0],rows23[1][0]]) + rows.extend([rows23[0][0], rows23[1][0]]) rows.append(rows4[0]) - rows.extend([rows56[0][0],rows56[1][0]]) + rows.extend([rows56[0][0], rows56[1][0]]) rows.sort() - for i in range(0,len(self.samples)): - self.assertEqual(rows[i],self.samples[i], - 'incorrect data retrieved or inserted' - ) + for i in range(0, len(self.samples)): + self.assertEqual( + rows[i], self.samples[i], "incorrect data retrieved or inserted" + ) finally: con.close() - def help_nextset_setUp(self,cur): - ''' Should create a procedure called deleteme + def help_nextset_setUp(self, cur): + """ Should create a procedure called deleteme that returns two result sets, first the number of rows in booze then "name from booze" - ''' - raise NotImplementedError('Helper not implemented') - #sql=""" + """ + raise NotImplementedError("Helper not implemented") + # sql=""" # create procedure deleteme as # begin # select count(*) from booze # select name from booze # end - #""" - #cur.execute(sql) + # """ + # cur.execute(sql) - def help_nextset_tearDown(self,cur): - 'If cleaning up is needed after nextSetTest' - raise NotImplementedError('Helper not implemented') - #cur.execute("drop procedure deleteme") + def help_nextset_tearDown(self, cur): + "If cleaning up is needed after nextSetTest" + raise NotImplementedError("Helper not implemented") + # cur.execute("drop procedure deleteme") def test_nextset(self): con = self._connect() try: cur = con.cursor() - if not hasattr(cur,'nextset'): + if not hasattr(cur, "nextset"): return try: self.executeDDL1(cur) - sql=self._populate() + sql = self._populate() for sql in self._populate(): cur.execute(sql) self.help_nextset_setUp(cur) - cur.callproc('deleteme') - numberofrows=cur.fetchone() - assert numberofrows[0]== len(self.samples) + cur.callproc("deleteme") + numberofrows = cur.fetchone() + assert numberofrows[0] == len(self.samples) assert cur.nextset() - names=cur.fetchall() + names = cur.fetchall() assert len(names) == len(self.samples) - s=cur.nextset() - assert s == None,'No more return sets, should return None' + s = cur.nextset() + assert s is None, "No more return sets, should return None" finally: self.help_nextset_tearDown(cur) finally: con.close() - def test_nextset(self): - raise NotImplementedError('Drivers need to override this test') + def test_nextset(self): # noqa: F811 + raise NotImplementedError("Drivers need to override this test") def test_arraysize(self): # Not much here - rest of the tests for this are in test_fetchmany con = self._connect() try: cur = con.cursor() - self.assertTrue(hasattr(cur,'arraysize'), - 'cursor.arraysize must be defined' - ) + self.assertTrue( + hasattr(cur, "arraysize"), "cursor.arraysize must be defined" + ) finally: con.close() @@ -768,8 +776,8 @@ def test_setinputsizes(self): con = self._connect() try: cur = con.cursor() - cur.setinputsizes( (25,) ) - self._paraminsert(cur) # Make sure cursor still works + cur.setinputsizes((25,)) + self._paraminsert(cur) # Make sure cursor still works finally: con.close() @@ -779,75 +787,74 @@ def test_setoutputsize_basic(self): try: cur = con.cursor() cur.setoutputsize(1000) - cur.setoutputsize(2000,0) - self._paraminsert(cur) # Make sure the cursor still works + cur.setoutputsize(2000, 0) + self._paraminsert(cur) # Make sure the cursor still works finally: con.close() def test_setoutputsize(self): # Real test for setoutputsize is driver dependant - raise NotImplementedError('Driver need to override this test') + raise NotImplementedError("Driver need to override this test") def test_None(self): con = self._connect() try: cur = con.cursor() self.executeDDL1(cur) - cur.execute('insert into %sbooze values (NULL)' % self.table_prefix) - cur.execute('select name from %sbooze' % self.table_prefix) + cur.execute("insert into %sbooze values (NULL)" % self.table_prefix) + cur.execute("select name from %sbooze" % self.table_prefix) r = cur.fetchall() - self.assertEqual(len(r),1) - self.assertEqual(len(r[0]),1) - self.assertEqual(r[0][0],None,'NULL value not returned as None') + self.assertEqual(len(r), 1) + self.assertEqual(len(r[0]), 1) + self.assertEqual(r[0][0], None, "NULL value not returned as None") finally: con.close() def test_Date(self): - d1 = self.driver.Date(2002,12,25) - d2 = self.driver.DateFromTicks(time.mktime((2002,12,25,0,0,0,0,0,0))) + d1 = self.driver.Date(2002, 12, 25) # noqa F841 + d2 = self.driver.DateFromTicks( # noqa F841 + time.mktime((2002, 12, 25, 0, 0, 0, 0, 0, 0)) + ) # Can we assume this? API doesn't specify, but it seems implied # self.assertEqual(str(d1),str(d2)) def test_Time(self): - t1 = self.driver.Time(13,45,30) - t2 = self.driver.TimeFromTicks(time.mktime((2001,1,1,13,45,30,0,0,0))) + t1 = self.driver.Time(13, 45, 30) # noqa F841 + t2 = self.driver.TimeFromTicks( # noqa F841 + time.mktime((2001, 1, 1, 13, 45, 30, 0, 0, 0)) + ) # Can we assume this? API doesn't specify, but it seems implied # self.assertEqual(str(t1),str(t2)) def test_Timestamp(self): - t1 = self.driver.Timestamp(2002,12,25,13,45,30) - t2 = self.driver.TimestampFromTicks( - time.mktime((2002,12,25,13,45,30,0,0,0)) - ) + t1 = self.driver.Timestamp(2002, 12, 25, 13, 45, 30) # noqa F841 + t2 = self.driver.TimestampFromTicks( # noqa F841 + time.mktime((2002, 12, 25, 13, 45, 30, 0, 0, 0)) + ) # Can we assume this? API doesn't specify, but it seems implied # self.assertEqual(str(t1),str(t2)) def test_Binary(self): - b = self.driver.Binary(b'Something') - b = self.driver.Binary(b'') + b = self.driver.Binary(b"Something") + b = self.driver.Binary(b"") # noqa F841 def test_STRING(self): - self.assertTrue(hasattr(self.driver,'STRING'), - 'module.STRING must be defined' - ) + self.assertTrue(hasattr(self.driver, "STRING"), "module.STRING must be defined") def test_BINARY(self): - self.assertTrue(hasattr(self.driver,'BINARY'), - 'module.BINARY must be defined.' - ) + self.assertTrue( + hasattr(self.driver, "BINARY"), "module.BINARY must be defined." + ) def test_NUMBER(self): - self.assertTrue(hasattr(self.driver,'NUMBER'), - 'module.NUMBER must be defined.' - ) + self.assertTrue( + hasattr(self.driver, "NUMBER"), "module.NUMBER must be defined." + ) def test_DATETIME(self): - self.assertTrue(hasattr(self.driver,'DATETIME'), - 'module.DATETIME must be defined.' - ) + self.assertTrue( + hasattr(self.driver, "DATETIME"), "module.DATETIME must be defined." + ) def test_ROWID(self): - self.assertTrue(hasattr(self.driver,'ROWID'), - 'module.ROWID must be defined.' - ) - + self.assertTrue(hasattr(self.driver, "ROWID"), "module.ROWID must be defined.") diff --git a/tests/test_MySQLdb_capabilities.py b/tests/test_MySQLdb_capabilities.py index d5be511f..fe9ef03e 100644 --- a/tests/test_MySQLdb_capabilities.py +++ b/tests/test_MySQLdb_capabilities.py @@ -1,23 +1,23 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- import capabilities from datetime import timedelta from contextlib import closing import unittest import MySQLdb -from MySQLdb import cursors from configdb import connection_factory import warnings -warnings.filterwarnings('ignore') +warnings.filterwarnings("ignore") class test_MySQLdb(capabilities.DatabaseTest): db_module = MySQLdb connect_args = () - connect_kwargs = dict(use_unicode=True, sql_mode="ANSI,STRICT_TRANS_TABLES,TRADITIONAL") + connect_kwargs = dict( + use_unicode=True, sql_mode="ANSI,STRICT_TRANS_TABLES,TRADITIONAL" + ) create_table_extra = "ENGINE=INNODB CHARACTER SET UTF8" leak_test = False @@ -25,97 +25,113 @@ def quote_identifier(self, ident): return "`%s`" % ident def test_TIME(self): - def generator(row,col): - return timedelta(0, row*8000) - self.check_data_integrity( - ('col1 TIME',), - generator) + def generator(row, col): + return timedelta(0, row * 8000) + + self.check_data_integrity(("col1 TIME",), generator) def test_TINYINT(self): # Number data def generator(row, col): - v = (row*row) % 256 + v = (row * row) % 256 if v > 127: - v = v-256 + v = v - 256 return v - self.check_data_integrity( - ('col1 TINYINT',), - generator) + + self.check_data_integrity(("col1 TINYINT",), generator) def test_stored_procedures(self): db = self.connection c = self.cursor - self.create_table(('pos INT', 'tree CHAR(20)')) - c.executemany("INSERT INTO %s (pos,tree) VALUES (%%s,%%s)" % self.table, - list(enumerate('ash birch cedar Lärche pine'.split()))) + self.create_table(("pos INT", "tree CHAR(20)")) + c.executemany( + "INSERT INTO %s (pos,tree) VALUES (%%s,%%s)" % self.table, + list(enumerate("ash birch cedar Lärche pine".split())), + ) db.commit() - c.execute(""" + c.execute( + """ CREATE PROCEDURE test_sp(IN t VARCHAR(255)) BEGIN SELECT pos FROM %s WHERE tree = t; END - """ % self.table) + """ + % self.table + ) db.commit() - c.callproc('test_sp', ('Lärche',)) + c.callproc("test_sp", ("Lärche",)) rows = c.fetchall() self.assertEqual(len(rows), 1) self.assertEqual(rows[0][0], 3) c.nextset() c.execute("DROP PROCEDURE test_sp") - c.execute('drop table %s' % (self.table)) + c.execute("drop table %s" % (self.table)) def test_small_CHAR(self): # Character data - def generator(row,col): - i = (row*col+62)%256 - if i == 62: return '' - if i == 63: return None + def generator(row, col): + i = (row * col + 62) % 256 + if i == 62: + return "" + if i == 63: + return None return chr(i) - self.check_data_integrity( - ('col1 char(1)','col2 char(1)'), - generator) + + self.check_data_integrity(("col1 char(1)", "col2 char(1)"), generator) def test_BIT(self): c = self.cursor try: - c.execute("""create table test_BIT ( + c.execute( + """create table test_BIT ( b3 BIT(3), b7 BIT(10), - b64 BIT(64))""") + b64 BIT(64))""" + ) - one64 = '1'*64 + one64 = "1" * 64 c.execute( "insert into test_BIT (b3, b7, b64)" - " VALUES (b'011', b'1111111111', b'%s')" - % one64) + " VALUES (b'011', b'1111111111', b'%s')" % one64 + ) c.execute("SELECT b3, b7, b64 FROM test_BIT") row = c.fetchone() - self.assertEqual(row[0], b'\x03') - self.assertEqual(row[1], b'\x03\xff') - self.assertEqual(row[2], b'\xff'*8) + self.assertEqual(row[0], b"\x03") + self.assertEqual(row[1], b"\x03\xff") + self.assertEqual(row[2], b"\xff" * 8) finally: c.execute("drop table if exists test_BIT") def test_MULTIPOLYGON(self): c = self.cursor try: - c.execute("""create table test_MULTIPOLYGON ( + c.execute( + """create table test_MULTIPOLYGON ( id INTEGER PRIMARY KEY, - border MULTIPOLYGON)""") + border MULTIPOLYGON)""" + ) c.execute( - "insert into test_MULTIPOLYGON (id, border)" - " VALUES (1, GeomFromText('MULTIPOLYGON(((1 1, 1 -1, -1 -1, -1 1, 1 1)),((1 1, 3 1, 3 3, 1 3, 1 1)))'))" + """ +INSERT INTO test_MULTIPOLYGON + (id, border) +VALUES (1, + Geomfromtext( +'MULTIPOLYGON(((1 1, 1 -1, -1 -1, -1 1, 1 1)),((1 1, 3 1, 3 3, 1 3, 1 1)))')) +""" ) c.execute("SELECT id, AsText(border) FROM test_MULTIPOLYGON") row = c.fetchone() self.assertEqual(row[0], 1) - self.assertEqual(row[1], 'MULTIPOLYGON(((1 1,1 -1,-1 -1,-1 1,1 1)),((1 1,3 1,3 3,1 3,1 1)))') + self.assertEqual( + row[1], + "MULTIPOLYGON(((1 1,1 -1,-1 -1,-1 1,1 1)),((1 1,3 1,3 3,1 3,1 1)))", + ) c.execute("SELECT id, AsWKB(border) FROM test_MULTIPOLYGON") row = c.fetchone() @@ -131,19 +147,21 @@ def test_MULTIPOLYGON(self): def test_bug_2671682(self): from MySQLdb.constants import ER + try: - self.cursor.execute("describe some_non_existent_table"); + self.cursor.execute("describe some_non_existent_table") except self.connection.ProgrammingError as msg: self.assertTrue(str(ER.NO_SUCH_TABLE) in str(msg)) def test_bug_3514287(self): c = self.cursor try: - c.execute("""create table bug_3541287 ( + c.execute( + """create table bug_3541287 ( c1 CHAR(10), - t1 TIMESTAMP)""") - c.execute("insert into bug_3541287 (c1,t1) values (%s, NOW())", - ("blah",)) + t1 TIMESTAMP)""" + ) + c.execute("insert into bug_3541287 (c1,t1) values (%s, NOW())", ("blah",)) finally: c.execute("drop table if exists bug_3541287") @@ -164,22 +182,25 @@ def test_binary_prefix(self): for binary_prefix in (True, False, None): kwargs = self.connect_kwargs.copy() # needs to be set to can guarantee CHARSET response for normal strings - kwargs['charset'] = 'utf8' - if binary_prefix != None: - kwargs['binary_prefix'] = binary_prefix + kwargs["charset"] = "utf8" + if binary_prefix is not None: + kwargs["binary_prefix"] = binary_prefix with closing(connection_factory(**kwargs)) as conn: with closing(conn.cursor()) as c: - c.execute('SELECT CHARSET(%s)', (MySQLdb.Binary(b'raw bytes'),)) - self.assertEqual(c.fetchall()[0][0], 'binary' if binary_prefix else 'utf8') + c.execute("SELECT CHARSET(%s)", (MySQLdb.Binary(b"raw bytes"),)) + self.assertEqual( + c.fetchall()[0][0], "binary" if binary_prefix else "utf8" + ) # normal strings should not get prefix - c.execute('SELECT CHARSET(%s)', ('str',)) - self.assertEqual(c.fetchall()[0][0], 'utf8') + c.execute("SELECT CHARSET(%s)", ("str",)) + self.assertEqual(c.fetchall()[0][0], "utf8") -if __name__ == '__main__': +if __name__ == "__main__": if test_MySQLdb.leak_test: import gc + gc.enable() gc.set_debug(gc.DEBUG_LEAK) unittest.main() diff --git a/tests/test_MySQLdb_dbapi20.py b/tests/test_MySQLdb_dbapi20.py index 1e808bd1..6b3a3787 100644 --- a/tests/test_MySQLdb_dbapi20.py +++ b/tests/test_MySQLdb_dbapi20.py @@ -4,17 +4,22 @@ import MySQLdb from configdb import connection_kwargs import warnings + warnings.simplefilter("ignore") class test_MySQLdb(dbapi20.DatabaseAPI20Test): driver = MySQLdb connect_args = () - connect_kw_args = connection_kwargs(dict(sql_mode="ANSI,STRICT_TRANS_TABLES,TRADITIONAL")) + connect_kw_args = connection_kwargs( + dict(sql_mode="ANSI,STRICT_TRANS_TABLES,TRADITIONAL") + ) + + def test_setoutputsize(self): + pass - def test_setoutputsize(self): pass - def test_setoutputsize_basic(self): pass - def test_nextset(self): pass + def test_setoutputsize_basic(self): + pass """The tests on fetchone and fetchall and rowcount bogusly test for an exception if the statement cannot return a @@ -36,36 +41,41 @@ def test_fetchall(self): # cursor.fetchall should raise an Error if called # after executing a statement that cannot return rows - #self.assertRaises(self.driver.Error,cur.fetchall) + # self.assertRaises(self.driver.Error,cur.fetchall) - cur.execute('select name from %sbooze' % self.table_prefix) + cur.execute("select name from %sbooze" % self.table_prefix) rows = cur.fetchall() - self.assertTrue(cur.rowcount in (-1,len(self.samples))) - self.assertEqual(len(rows),len(self.samples), - 'cursor.fetchall did not retrieve all rows' - ) + self.assertTrue(cur.rowcount in (-1, len(self.samples))) + self.assertEqual( + len(rows), + len(self.samples), + "cursor.fetchall did not retrieve all rows", + ) rows = [r[0] for r in rows] rows.sort() - for i in range(0,len(self.samples)): - self.assertEqual(rows[i],self.samples[i], - 'cursor.fetchall retrieved incorrect rows' + for i in range(0, len(self.samples)): + self.assertEqual( + rows[i], self.samples[i], "cursor.fetchall retrieved incorrect rows" ) rows = cur.fetchall() self.assertEqual( - len(rows),0, - 'cursor.fetchall should return an empty list if called ' - 'after the whole result set has been fetched' - ) - self.assertTrue(cur.rowcount in (-1,len(self.samples))) + len(rows), + 0, + "cursor.fetchall should return an empty list if called " + "after the whole result set has been fetched", + ) + self.assertTrue(cur.rowcount in (-1, len(self.samples))) self.executeDDL2(cur) - cur.execute('select name from %sbarflys' % self.table_prefix) + cur.execute("select name from %sbarflys" % self.table_prefix) rows = cur.fetchall() - self.assertTrue(cur.rowcount in (-1,0)) - self.assertEqual(len(rows),0, - 'cursor.fetchall should return an empty list if ' - 'a select query returns no rows' - ) + self.assertTrue(cur.rowcount in (-1, 0)) + self.assertEqual( + len(rows), + 0, + "cursor.fetchall should return an empty list if " + "a select query returns no rows", + ) finally: con.close() @@ -77,39 +87,42 @@ def test_fetchone(self): # cursor.fetchone should raise an Error if called before # executing a select-type query - self.assertRaises(self.driver.Error,cur.fetchone) + self.assertRaises(self.driver.Error, cur.fetchone) # cursor.fetchone should raise an Error if called after # executing a query that cannot return rows self.executeDDL1(cur) -## self.assertRaises(self.driver.Error,cur.fetchone) + # self.assertRaises(self.driver.Error,cur.fetchone) - cur.execute('select name from %sbooze' % self.table_prefix) - self.assertEqual(cur.fetchone(),None, - 'cursor.fetchone should return None if a query retrieves ' - 'no rows' - ) - self.assertTrue(cur.rowcount in (-1,0)) + cur.execute("select name from %sbooze" % self.table_prefix) + self.assertEqual( + cur.fetchone(), + None, + "cursor.fetchone should return None if a query retrieves " "no rows", + ) + self.assertTrue(cur.rowcount in (-1, 0)) # cursor.fetchone should raise an Error if called after # executing a query that cannot return rows - cur.execute("insert into %sbooze values ('Victoria Bitter')" % ( - self.table_prefix - )) -## self.assertRaises(self.driver.Error,cur.fetchone) + cur.execute( + "insert into %sbooze values ('Victoria Bitter')" % (self.table_prefix) + ) + # self.assertRaises(self.driver.Error,cur.fetchone) - cur.execute('select name from %sbooze' % self.table_prefix) + cur.execute("select name from %sbooze" % self.table_prefix) r = cur.fetchone() - self.assertEqual(len(r),1, - 'cursor.fetchone should have retrieved a single row' - ) - self.assertEqual(r[0],'Victoria Bitter', - 'cursor.fetchone retrieved incorrect data' - ) -## self.assertEqual(cur.fetchone(),None, -## 'cursor.fetchone should return None if no more rows available' -## ) - self.assertTrue(cur.rowcount in (-1,1)) + self.assertEqual( + len(r), 1, "cursor.fetchone should have retrieved a single row" + ) + self.assertEqual( + r[0], "Victoria Bitter", "cursor.fetchone retrieved incorrect data" + ) + # self.assertEqual( + # cur.fetchone(), + # None, + # "cursor.fetchone should return None if no more rows available", + # ) + self.assertTrue(cur.rowcount in (-1, 1)) finally: con.close() @@ -119,81 +132,93 @@ def test_rowcount(self): try: cur = con.cursor() self.executeDDL1(cur) -## self.assertEqual(cur.rowcount,-1, -## 'cursor.rowcount should be -1 after executing no-result ' -## 'statements' -## ) - cur.execute("insert into %sbooze values ('Victoria Bitter')" % ( - self.table_prefix - )) -## self.assertTrue(cur.rowcount in (-1,1), -## 'cursor.rowcount should == number or rows inserted, or ' -## 'set to -1 after executing an insert statement' -## ) + # self.assertEqual(cur.rowcount,-1, + # 'cursor.rowcount should be -1 after executing no-result ' + # 'statements' + # ) + cur.execute( + "insert into %sbooze values ('Victoria Bitter')" % (self.table_prefix) + ) + # self.assertTrue(cur.rowcount in (-1,1), + # 'cursor.rowcount should == number or rows inserted, or ' + # 'set to -1 after executing an insert statement' + # ) cur.execute("select name from %sbooze" % self.table_prefix) - self.assertTrue(cur.rowcount in (-1,1), - 'cursor.rowcount should == number of rows returned, or ' - 'set to -1 after executing a select statement' - ) + self.assertTrue( + cur.rowcount in (-1, 1), + "cursor.rowcount should == number of rows returned, or " + "set to -1 after executing a select statement", + ) self.executeDDL2(cur) -## self.assertEqual(cur.rowcount,-1, -## 'cursor.rowcount not being reset to -1 after executing ' -## 'no-result statements' -## ) + # self.assertEqual(cur.rowcount,-1, + # 'cursor.rowcount not being reset to -1 after executing ' + # 'no-result statements' + # ) finally: con.close() def test_callproc(self): - pass # performed in test_MySQL_capabilities + pass # performed in test_MySQL_capabilities - def help_nextset_setUp(self,cur): - ''' Should create a procedure called deleteme + def help_nextset_setUp(self, cur): + """ Should create a procedure called deleteme that returns two result sets, first the number of rows in booze then "name from booze" - ''' - sql=""" + """ + sql = """ create procedure deleteme() begin select count(*) from %(tp)sbooze; select name from %(tp)sbooze; end - """ % dict(tp=self.table_prefix) + """ % dict( + tp=self.table_prefix + ) cur.execute(sql) - def help_nextset_tearDown(self,cur): - 'If cleaning up is needed after nextSetTest' + def help_nextset_tearDown(self, cur): + "If cleaning up is needed after nextSetTest" cur.execute("drop procedure deleteme") def test_nextset(self): - #from warnings import warn + # from warnings import warn + con = self._connect() try: cur = con.cursor() - if not hasattr(cur, 'nextset'): + if not hasattr(cur, "nextset"): return try: self.executeDDL1(cur) - sql=self._populate() + sql = self._populate() for sql in self._populate(): cur.execute(sql) self.help_nextset_setUp(cur) - cur.callproc('deleteme') - numberofrows=cur.fetchone() - assert numberofrows[0]== len(self.samples) + cur.callproc("deleteme") + numberofrows = cur.fetchone() + assert numberofrows[0] == len(self.samples) assert cur.nextset() - names=cur.fetchall() + names = cur.fetchall() assert len(names) == len(self.samples) - s=cur.nextset() + s = cur.nextset() if s: empty = cur.fetchall() - self.assertEqual(len(empty), 0, - "non-empty result set after other result sets") - #warn("Incompatibility: MySQL returns an empty result set for the CALL itself", - # Warning) - #assert s == None,'No more return sets, should return None' + self.assertEqual( + len(empty), 0, "non-empty result set after other result sets" + ) + # warn( + # ": ".join( + # [ + # "Incompatibility", + # "MySQL returns an empty result set for the CALL itself" + # ] + # ), + # Warning, + # ) + # assert s == None, "No more return sets, should return None" finally: self.help_nextset_tearDown(cur) @@ -201,5 +226,5 @@ def test_nextset(self): con.close() -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/test_MySQLdb_nonstandard.py b/tests/test_MySQLdb_nonstandard.py index c5cacbec..c517dad3 100644 --- a/tests/test_MySQLdb_nonstandard.py +++ b/tests/test_MySQLdb_nonstandard.py @@ -5,6 +5,7 @@ from MySQLdb.constants import FIELD_TYPE from configdb import connection_factory import warnings + warnings.simplefilter("ignore") @@ -36,10 +37,12 @@ def test_client_info(self): self.assertTrue(isinstance(_mysql.get_client_info(), str)) def test_escape_string(self): - self.assertEqual(_mysql.escape_string(b'foo"bar'), - b'foo\\"bar', "escape byte string") - self.assertEqual(_mysql.escape_string(u'foo"bar'), - b'foo\\"bar', "escape unicode string") + self.assertEqual( + _mysql.escape_string(b'foo"bar'), b'foo\\"bar', "escape byte string" + ) + self.assertEqual( + _mysql.escape_string('foo"bar'), b'foo\\"bar', "escape unicode string" + ) class CoreAPI(unittest.TestCase): @@ -53,42 +56,49 @@ def tearDown(self): def test_thread_id(self): tid = self.conn.thread_id() - self.assertTrue(isinstance(tid, int), - "thread_id didn't return an int.") + self.assertTrue(isinstance(tid, int), "thread_id didn't return an int.") - self.assertRaises(TypeError, self.conn.thread_id, ('evil',), - "thread_id shouldn't accept arguments.") + self.assertRaises( + TypeError, + self.conn.thread_id, + ("evil",), + "thread_id shouldn't accept arguments.", + ) def test_affected_rows(self): - self.assertEqual(self.conn.affected_rows(), 0, - "Should return 0 before we do anything.") - + self.assertEqual( + self.conn.affected_rows(), 0, "Should return 0 before we do anything." + ) - #def test_debug(self): - ## FIXME Only actually tests if you lack SUPER - #self.assertRaises(MySQLdb.OperationalError, - #self.conn.dump_debug_info) + # def test_debug(self): + # (FIXME) Only actually tests if you lack SUPER + # self.assertRaises(MySQLdb.OperationalError, + # self.conn.dump_debug_info) def test_charset_name(self): - self.assertTrue(isinstance(self.conn.character_set_name(), str), - "Should return a string.") + self.assertTrue( + isinstance(self.conn.character_set_name(), str), "Should return a string." + ) def test_host_info(self): - self.assertTrue(isinstance(self.conn.get_host_info(), str), - "Should return a string.") + self.assertTrue( + isinstance(self.conn.get_host_info(), str), "Should return a string." + ) def test_proto_info(self): - self.assertTrue(isinstance(self.conn.get_proto_info(), int), - "Should return an int.") + self.assertTrue( + isinstance(self.conn.get_proto_info(), int), "Should return an int." + ) def test_server_info(self): - self.assertTrue(isinstance(self.conn.get_server_info(), str), - "Should return a string.") + self.assertTrue( + isinstance(self.conn.get_server_info(), str), "Should return a string." + ) def test_client_flag(self): conn = connection_factory( - use_unicode=True, - client_flag=MySQLdb.constants.CLIENT.FOUND_ROWS) + use_unicode=True, client_flag=MySQLdb.constants.CLIENT.FOUND_ROWS + ) self.assertIsInstance(conn.client_flag, int) self.assertTrue(conn.client_flag & MySQLdb.constants.CLIENT.FOUND_ROWS) diff --git a/tests/test_MySQLdb_times.py b/tests/test_MySQLdb_times.py index d9d3e027..fdc35ff9 100644 --- a/tests/test_MySQLdb_times.py +++ b/tests/test_MySQLdb_times.py @@ -6,104 +6,141 @@ from MySQLdb import times import warnings + warnings.simplefilter("ignore") class TestX_or_None(unittest.TestCase): def test_date_or_none(self): - assert times.Date_or_None('1969-01-01') == date(1969, 1, 1) - assert times.Date_or_None('2015-01-01') == date(2015, 1, 1) - assert times.Date_or_None('2015-12-13') == date(2015, 12, 13) + assert times.Date_or_None("1969-01-01") == date(1969, 1, 1) + assert times.Date_or_None("2015-01-01") == date(2015, 1, 1) + assert times.Date_or_None("2015-12-13") == date(2015, 12, 13) - assert times.Date_or_None('') is None - assert times.Date_or_None('fail') is None - assert times.Date_or_None('2015-12') is None - assert times.Date_or_None('2015-12-40') is None - assert times.Date_or_None('0000-00-00') is None + assert times.Date_or_None("") is None + assert times.Date_or_None("fail") is None + assert times.Date_or_None("2015-12") is None + assert times.Date_or_None("2015-12-40") is None + assert times.Date_or_None("0000-00-00") is None def test_time_or_none(self): - assert times.Time_or_None('00:00:00') == time(0, 0) - assert times.Time_or_None('01:02:03') == time(1, 2, 3) - assert times.Time_or_None('01:02:03.123456') == time(1, 2, 3, 123456) + assert times.Time_or_None("00:00:00") == time(0, 0) + assert times.Time_or_None("01:02:03") == time(1, 2, 3) + assert times.Time_or_None("01:02:03.123456") == time(1, 2, 3, 123456) - assert times.Time_or_None('') is None - assert times.Time_or_None('fail') is None - assert times.Time_or_None('24:00:00') is None - assert times.Time_or_None('01:02:03.123456789') is None + assert times.Time_or_None("") is None + assert times.Time_or_None("fail") is None + assert times.Time_or_None("24:00:00") is None + assert times.Time_or_None("01:02:03.123456789") is None def test_datetime_or_none(self): - assert times.DateTime_or_None('1000-01-01') == date(1000, 1, 1) - assert times.DateTime_or_None('2015-12-13') == date(2015, 12, 13) - assert times.DateTime_or_None('2015-12-13 01:02') == datetime(2015, 12, 13, 1, 2) - assert times.DateTime_or_None('2015-12-13T01:02') == datetime(2015, 12, 13, 1, 2) - assert times.DateTime_or_None('2015-12-13 01:02:03') == datetime(2015, 12, 13, 1, 2, 3) - assert times.DateTime_or_None('2015-12-13T01:02:03') == datetime(2015, 12, 13, 1, 2, 3) - assert times.DateTime_or_None('2015-12-13 01:02:03.123') == datetime(2015, 12, 13, 1, 2, 3, 123000) - assert times.DateTime_or_None('2015-12-13 01:02:03.000123') == datetime(2015, 12, 13, 1, 2, 3, 123) - assert times.DateTime_or_None('2015-12-13 01:02:03.123456') == datetime(2015, 12, 13, 1, 2, 3, 123456) - assert times.DateTime_or_None('2015-12-13T01:02:03.123456') == datetime(2015, 12, 13, 1, 2, 3, 123456) - - assert times.DateTime_or_None('') is None - assert times.DateTime_or_None('fail') is None - assert times.DateTime_or_None('0000-00-00 00:00:00') is None - assert times.DateTime_or_None('0000-00-00 00:00:00.000000') is None - assert times.DateTime_or_None('2015-12-13T01:02:03.123456789') is None + assert times.DateTime_or_None("1000-01-01") == date(1000, 1, 1) + assert times.DateTime_or_None("2015-12-13") == date(2015, 12, 13) + assert times.DateTime_or_None("2015-12-13 01:02") == datetime( + 2015, 12, 13, 1, 2 + ) + assert times.DateTime_or_None("2015-12-13T01:02") == datetime( + 2015, 12, 13, 1, 2 + ) + assert times.DateTime_or_None("2015-12-13 01:02:03") == datetime( + 2015, 12, 13, 1, 2, 3 + ) + assert times.DateTime_or_None("2015-12-13T01:02:03") == datetime( + 2015, 12, 13, 1, 2, 3 + ) + assert times.DateTime_or_None("2015-12-13 01:02:03.123") == datetime( + 2015, 12, 13, 1, 2, 3, 123000 + ) + assert times.DateTime_or_None("2015-12-13 01:02:03.000123") == datetime( + 2015, 12, 13, 1, 2, 3, 123 + ) + assert times.DateTime_or_None("2015-12-13 01:02:03.123456") == datetime( + 2015, 12, 13, 1, 2, 3, 123456 + ) + assert times.DateTime_or_None("2015-12-13T01:02:03.123456") == datetime( + 2015, 12, 13, 1, 2, 3, 123456 + ) + + assert times.DateTime_or_None("") is None + assert times.DateTime_or_None("fail") is None + assert times.DateTime_or_None("0000-00-00 00:00:00") is None + assert times.DateTime_or_None("0000-00-00 00:00:00.000000") is None + assert times.DateTime_or_None("2015-12-13T01:02:03.123456789") is None def test_timedelta_or_none(self): - assert times.TimeDelta_or_None('-1:0:0') == timedelta(0, -3600) - assert times.TimeDelta_or_None('1:0:0') == timedelta(0, 3600) - assert times.TimeDelta_or_None('12:55:30') == timedelta(0, 46530) - assert times.TimeDelta_or_None('12:55:30.123456') == timedelta(0, 46530, 123456) - assert times.TimeDelta_or_None('12:55:30.123456789') == timedelta(0, 46653, 456789) - assert times.TimeDelta_or_None('12:55:30.123456789123456') == timedelta(1429, 37719, 123456) - - assert times.TimeDelta_or_None('') is None - assert times.TimeDelta_or_None('0') is None - assert times.TimeDelta_or_None('fail') is None + assert times.TimeDelta_or_None("-1:0:0") == timedelta(0, -3600) + assert times.TimeDelta_or_None("1:0:0") == timedelta(0, 3600) + assert times.TimeDelta_or_None("12:55:30") == timedelta(0, 46530) + assert times.TimeDelta_or_None("12:55:30.123456") == timedelta(0, 46530, 123456) + assert times.TimeDelta_or_None("12:55:30.123456789") == timedelta( + 0, 46653, 456789 + ) + assert times.TimeDelta_or_None("12:55:30.123456789123456") == timedelta( + 1429, 37719, 123456 + ) + + assert times.TimeDelta_or_None("") is None + assert times.TimeDelta_or_None("0") is None + assert times.TimeDelta_or_None("fail") is None class TestTicks(unittest.TestCase): - @mock.patch('MySQLdb.times.localtime', side_effect=gmtime) + @mock.patch("MySQLdb.times.localtime", side_effect=gmtime) def test_date_from_ticks(self, mock): assert times.DateFromTicks(0) == date(1970, 1, 1) assert times.DateFromTicks(1430000000) == date(2015, 4, 25) - @mock.patch('MySQLdb.times.localtime', side_effect=gmtime) + @mock.patch("MySQLdb.times.localtime", side_effect=gmtime) def test_time_from_ticks(self, mock): assert times.TimeFromTicks(0) == time(0, 0, 0) assert times.TimeFromTicks(1431100000) == time(15, 46, 40) assert times.TimeFromTicks(1431100000.123) == time(15, 46, 40) - @mock.patch('MySQLdb.times.localtime', side_effect=gmtime) + @mock.patch("MySQLdb.times.localtime", side_effect=gmtime) def test_timestamp_from_ticks(self, mock): assert times.TimestampFromTicks(0) == datetime(1970, 1, 1, 0, 0, 0) assert times.TimestampFromTicks(1430000000) == datetime(2015, 4, 25, 22, 13, 20) - assert times.TimestampFromTicks(1430000000.123) == datetime(2015, 4, 25, 22, 13, 20) + assert times.TimestampFromTicks(1430000000.123) == datetime( + 2015, 4, 25, 22, 13, 20 + ) class TestToLiteral(unittest.TestCase): def test_datetime_to_literal(self): - assert times.DateTime2literal(datetime(2015, 12, 13), '') == b"'2015-12-13 00:00:00'" - assert times.DateTime2literal(datetime(2015, 12, 13, 11, 12, 13), '') == b"'2015-12-13 11:12:13'" - assert times.DateTime2literal(datetime(2015, 12, 13, 11, 12, 13, 123456), '') == b"'2015-12-13 11:12:13.123456'" + self.assertEquals( + times.DateTime2literal(datetime(2015, 12, 13), ""), b"'2015-12-13 00:00:00'" + ) + self.assertEquals( + times.DateTime2literal(datetime(2015, 12, 13, 11, 12, 13), ""), + b"'2015-12-13 11:12:13'", + ) + self.assertEquals( + times.DateTime2literal(datetime(2015, 12, 13, 11, 12, 13, 123456), ""), + b"'2015-12-13 11:12:13.123456'", + ) def test_datetimedelta_to_literal(self): d = datetime(2015, 12, 13, 1, 2, 3) - datetime(2015, 12, 13, 1, 2, 2) - assert times.DateTimeDelta2literal(d, '') == b"'0 0:0:1'" + assert times.DateTimeDelta2literal(d, "") == b"'0 0:0:1'" class TestFormat(unittest.TestCase): def test_format_timedelta(self): d = datetime(2015, 1, 1) - datetime(2015, 1, 1) - assert times.format_TIMEDELTA(d) == '0 0:0:0' + assert times.format_TIMEDELTA(d) == "0 0:0:0" d = datetime(2015, 1, 1, 10, 11, 12) - datetime(2015, 1, 1, 8, 9, 10) - assert times.format_TIMEDELTA(d) == '0 2:2:2' + assert times.format_TIMEDELTA(d) == "0 2:2:2" d = datetime(2015, 1, 1, 10, 11, 12) - datetime(2015, 1, 1, 11, 12, 13) - assert times.format_TIMEDELTA(d) == '-1 22:58:59' + assert times.format_TIMEDELTA(d) == "-1 22:58:59" def test_format_timestamp(self): - assert times.format_TIMESTAMP(datetime(2015, 2, 3)) == '2015-02-03 00:00:00' - assert times.format_TIMESTAMP(datetime(2015, 2, 3, 17, 18, 19)) == '2015-02-03 17:18:19' - assert times.format_TIMESTAMP(datetime(15, 2, 3, 17, 18, 19)) == '0015-02-03 17:18:19' + assert times.format_TIMESTAMP(datetime(2015, 2, 3)) == "2015-02-03 00:00:00" + self.assertEquals( + times.format_TIMESTAMP(datetime(2015, 2, 3, 17, 18, 19)), + "2015-02-03 17:18:19", + ) + self.assertEquals( + times.format_TIMESTAMP(datetime(15, 2, 3, 17, 18, 19)), + "0015-02-03 17:18:19", + ) diff --git a/tests/test_cursor.py b/tests/test_cursor.py index ff96368f..479f3e27 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -1,6 +1,4 @@ -from __future__ import print_function, absolute_import - -import pytest +# import pytest import MySQLdb.cursors from configdb import connection_factory @@ -8,6 +6,7 @@ _conns = [] _tables = [] + def connect(**kwargs): conn = connection_factory(**kwargs) _conns.append(conn) @@ -19,7 +18,7 @@ def teardown_function(function): c = _conns[0] cur = c.cursor() for t in _tables: - cur.execute("DROP TABLE %s" % (t,)) + cur.execute("DROP TABLE {}".format(t)) cur.close() del _tables[:] @@ -35,47 +34,71 @@ def test_executemany(): cursor.execute("create table test (data varchar(10))") _tables.append("test") - m = MySQLdb.cursors.RE_INSERT_VALUES.match("INSERT INTO TEST (ID, NAME) VALUES (%s, %s)") - assert m is not None, 'error parse %s' - assert m.group(3) == '', 'group 3 not blank, bug in RE_INSERT_VALUES?' - - m = MySQLdb.cursors.RE_INSERT_VALUES.match("INSERT INTO TEST (ID, NAME) VALUES (%(id)s, %(name)s)") - assert m is not None, 'error parse %(name)s' - assert m.group(3) == '', 'group 3 not blank, bug in RE_INSERT_VALUES?' - - m = MySQLdb.cursors.RE_INSERT_VALUES.match("INSERT INTO TEST (ID, NAME) VALUES (%(id_name)s, %(name)s)") - assert m is not None, 'error parse %(id_name)s' - assert m.group(3) == '', 'group 3 not blank, bug in RE_INSERT_VALUES?' - - m = MySQLdb.cursors.RE_INSERT_VALUES.match("INSERT INTO TEST (ID, NAME) VALUES (%(id_name)s, %(name)s) ON duplicate update") - assert m is not None, 'error parse %(id_name)s' - assert m.group(3) == ' ON duplicate update', 'group 3 not ON duplicate update, bug in RE_INSERT_VALUES?' + m = MySQLdb.cursors.RE_INSERT_VALUES.match( + "INSERT INTO TEST (ID, NAME) VALUES (%s, %s)" + ) + assert m is not None, "error parse %s" + assert m.group(3) == "", "group 3 not blank, bug in RE_INSERT_VALUES?" + + m = MySQLdb.cursors.RE_INSERT_VALUES.match( + "INSERT INTO TEST (ID, NAME) VALUES (%(id)s, %(name)s)" + ) + assert m is not None, "error parse %(name)s" + assert m.group(3) == "", "group 3 not blank, bug in RE_INSERT_VALUES?" + + m = MySQLdb.cursors.RE_INSERT_VALUES.match( + "INSERT INTO TEST (ID, NAME) VALUES (%(id_name)s, %(name)s)" + ) + assert m is not None, "error parse %(id_name)s" + assert m.group(3) == "", "group 3 not blank, bug in RE_INSERT_VALUES?" + + m = MySQLdb.cursors.RE_INSERT_VALUES.match( + "INSERT INTO TEST (ID, NAME) VALUES (%(id_name)s, %(name)s) ON duplicate update" + ) + assert m is not None, "error parse %(id_name)s" + assert ( + m.group(3) == " ON duplicate update" + ), "group 3 not ON duplicate update, bug in RE_INSERT_VALUES?" # https://github.com/PyMySQL/mysqlclient-python/issues/178 - m = MySQLdb.cursors.RE_INSERT_VALUES.match("INSERT INTO bloup(foo, bar)VALUES(%s, %s)") + m = MySQLdb.cursors.RE_INSERT_VALUES.match( + "INSERT INTO bloup(foo, bar)VALUES(%s, %s)" + ) assert m is not None - # cursor._executed myst bee "insert into test (data) values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)" + # cursor._executed myst bee + # """ + # insert into test (data) + # values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9) + # """ # list args data = range(10) cursor.executemany("insert into test (data) values (%s)", data) - assert cursor._executed.endswith(b",(7),(8),(9)"), 'execute many with %s not in one query' + assert cursor._executed.endswith( + b",(7),(8),(9)" + ), "execute many with %s not in one query" # dict args - data_dict = [{'data': i} for i in range(10)] + data_dict = [{"data": i} for i in range(10)] cursor.executemany("insert into test (data) values (%(data)s)", data_dict) - assert cursor._executed.endswith(b",(7),(8),(9)"), 'execute many with %(data)s not in one query' + assert cursor._executed.endswith( + b",(7),(8),(9)" + ), "execute many with %(data)s not in one query" # %% in column set - cursor.execute("""\ + cursor.execute( + """\ CREATE TABLE percent_test ( `A%` INTEGER, - `B%` INTEGER)""") + `B%` INTEGER)""" + ) try: q = "INSERT INTO percent_test (`A%%`, `B%%`) VALUES (%s, %s)" assert MySQLdb.cursors.RE_INSERT_VALUES.match(q) is not None cursor.executemany(q, [(3, 4), (5, 6)]) - assert cursor._executed.endswith(b"(3, 4),(5, 6)"), "executemany with %% not in one query" + assert cursor._executed.endswith( + b"(3, 4),(5, 6)" + ), "executemany with %% not in one query" finally: cursor.execute("DROP TABLE IF EXISTS percent_test") @@ -84,7 +107,7 @@ def test_pyparam(): conn = connect() cursor = conn.cursor() - cursor.execute(u"SELECT %(a)s, %(b)s", {u'a': 1, u'b': 2}) + cursor.execute("SELECT %(a)s, %(b)s", {"a": 1, "b": 2}) assert cursor._executed == b"SELECT 1, 2" - cursor.execute(b"SELECT %(a)s, %(b)s", {b'a': 3, b'b': 4}) + cursor.execute(b"SELECT %(a)s, %(b)s", {b"a": 3, b"b": 4}) assert cursor._executed == b"SELECT 3, 4" From c67dbd41f13f5302120ad40fee94ea6c7ffc2bfc Mon Sep 17 00:00:00 2001 From: Jason Fried Date: Wed, 5 Feb 2020 03:38:25 -0800 Subject: [PATCH 176/177] Add some gc safety around _mysql__fetch_row (#348) Users of gc.get_referrers() could cause a SystemError to be raised if this function is running in a different python thread. --- MySQLdb/_mysql.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 13280ace..1556fda3 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -1373,9 +1373,15 @@ _mysql_ResultObject_fetch_row( convert_row = row_converters[how]; if (maxrows) { if (!(r = PyTuple_New(maxrows))) goto error; - rowsadded = _mysql__fetch_row(self, &r, skiprows, maxrows, - convert_row); + + // see: https://docs.python.org/3/library/gc.html#gc.get_referrers + // This function can get a reference to the tuple r, and if that + // code is preempted while holding a ref to r, the _PyTuple_Resize + // will raise a SystemError because the ref count is 2. + PyObject_GC_UnTrack(r); + rowsadded = _mysql__fetch_row(self, &r, skiprows, maxrows, convert_row); if (rowsadded == -1) goto error; + PyObject_GC_Track(r); } else { if (self->use) { maxrows = 1000; From 4c1950148ba10e23017bd5190f9da6bdb347e6b3 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 17 Feb 2020 16:01:43 +0900 Subject: [PATCH 177/177] travis: Use bionic (#424) --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index ea1c62ea..2f956c2d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ -dist: xenial +dist: bionic language: python -# See aws s3 ls s3://travis-python-archives/binaries/ubuntu/16.04/x86_64/ +# See aws s3 ls s3://travis-python-archives/binaries/ubuntu/18.04/x86_64/ python: - "nightly" - "pypy3"