From b9aa0530a855ef3cb23d84e50dea47bf3fc14f99 Mon Sep 17 00:00:00 2001 From: Niko Wenselowski Date: Thu, 9 Jan 2014 18:03:53 +0100 Subject: [PATCH 1/4] Improved support for Python 3. --- MySQLdb/connections.py | 6 +++++- MySQLdb/cursors.py | 4 ++-- setup_posix.py | 6 +++++- tests/test_MySQLdb_capabilities.py | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/MySQLdb/connections.py b/MySQLdb/connections.py index 908706a3..13e55c4a 100644 --- a/MySQLdb/connections.py +++ b/MySQLdb/connections.py @@ -33,7 +33,11 @@ def defaulterrorhandler(connection, cursor, errorclass, errorvalue): connection.messages.append(error) del cursor del connection - raise errorclass, errorvalue + if errorclass is not None: + raise errorclass(errorvalue) + else: + raise Exception(errorvalue) + re_numeric_part = re.compile(r"^(\d+)") diff --git a/MySQLdb/cursors.py b/MySQLdb/cursors.py index 348a586a..b45ddaf9 100644 --- a/MySQLdb/cursors.py +++ b/MySQLdb/cursors.py @@ -188,7 +188,7 @@ def execute(self, query, args=None): try: r = None r = self._query(query) - except TypeError, m: + except TypeError as m: if m.args[0] in ("not enough arguments for format string", "not all arguments converted"): self.messages.append((ProgrammingError, m.args[0])) @@ -247,7 +247,7 @@ def executemany(self, query, args): for key, item in a.iteritems())) else: q.append(qv % tuple([db.literal(item) for item in a])) - except TypeError, msg: + except TypeError as msg: if msg.args[0] in ("not enough arguments for format string", "not all arguments converted"): self.errorhandler(self, ProgrammingError, msg.args[0]) diff --git a/setup_posix.py b/setup_posix.py index cfcf33ca..dc9b90bf 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -1,5 +1,9 @@ import os, sys -from ConfigParser import SafeConfigParser +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 # of mysql_config diff --git a/tests/test_MySQLdb_capabilities.py b/tests/test_MySQLdb_capabilities.py index ead69822..31e5f6fc 100644 --- a/tests/test_MySQLdb_capabilities.py +++ b/tests/test_MySQLdb_capabilities.py @@ -76,7 +76,7 @@ def test_bug_2671682(self): from MySQLdb.constants import ER try: self.cursor.execute("describe some_non_existent_table"); - except self.connection.ProgrammingError, msg: + except self.connection.ProgrammingError as msg: self.assertEquals(msg[0], ER.NO_SUCH_TABLE) def test_bug_3514287(self): From 0773933caead46776ceb5cd86abccfe840e49839 Mon Sep 17 00:00:00 2001 From: Niko Wenselowski Date: Fri, 10 Jan 2014 10:01:17 +0100 Subject: [PATCH 2/4] Changing the way checks for the error code are made. --- tests/test_MySQLdb_capabilities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_MySQLdb_capabilities.py b/tests/test_MySQLdb_capabilities.py index 31e5f6fc..c45353fd 100644 --- a/tests/test_MySQLdb_capabilities.py +++ b/tests/test_MySQLdb_capabilities.py @@ -77,7 +77,7 @@ def test_bug_2671682(self): try: self.cursor.execute("describe some_non_existent_table"); except self.connection.ProgrammingError as msg: - self.assertEquals(msg[0], ER.NO_SUCH_TABLE) + self.assertTrue(str(ER.NO_SUCH_TABLE) in str(msg)) def test_bug_3514287(self): c = self.cursor From 2072903e0c9f3623f2d4c2bba9f69d6f6ac93bb7 Mon Sep 17 00:00:00 2001 From: Niko Wenselowski Date: Thu, 17 Apr 2014 11:20:39 +0200 Subject: [PATCH 3/4] Importing SafeConfigParser from setup_common --- setup_posix.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/setup_posix.py b/setup_posix.py index dc9b90bf..df8e2d14 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -1,9 +1,6 @@ -import os, sys -try: - from ConfigParser import SafeConfigParser -except ImportError: - # Probably running Python 3.x - from configparser import ConfigParser as SafeConfigParser +import os +import sys +from setup_common import SafeConfigParser # This dequote() business is required for some older versions # of mysql_config From 9000bdf004920df35f6c59e39b33d44dfd4796db Mon Sep 17 00:00:00 2001 From: Niko Wenselowski Date: Thu, 17 Apr 2014 11:21:03 +0200 Subject: [PATCH 4/4] Adding encoding to files. --- setup_common.py | 2 ++ setup_posix.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/setup_common.py b/setup_common.py index 03c39bb7..4f72cda3 100644 --- a/setup_common.py +++ b/setup_common.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + try: # Python 2.x from ConfigParser import SafeConfigParser diff --git a/setup_posix.py b/setup_posix.py index df8e2d14..9f3d9e64 100644 --- a/setup_posix.py +++ b/setup_posix.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + import os import sys from setup_common import SafeConfigParser