From: Mike Bayer Date: Fri, 22 Nov 2013 23:35:36 +0000 (-0500) Subject: - Fixed bug where SQL statement would be improperly ASCII-encoded X-Git-Tag: rel_0_9_0~105 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f112dc1d533033f19186eb65227aba1660d03102;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Fixed bug where SQL statement would be improperly ASCII-encoded when a pre-DBAPI :class:`.StatementError` were raised within :meth:`.Connection.execute`, causing encoding errors for non-ASCII statements. The stringification now remains within Python unicode thus avoiding encoding errors. [ticket:2871] --- diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index cc51febe1c..e5df476163 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -11,6 +11,17 @@ .. changelog:: :version: 0.8.4 + .. change:: + :tags: bug, engine + :tickets: 2871 + :versions: 0.9.0b2 + + Fixed bug where SQL statement would be improperly ASCII-encoded + when a pre-DBAPI :class:`.StatementError` were raised within + :meth:`.Connection.execute`, causing encoding errors for + non-ASCII statements. The stringification now remains within + Python unicode thus avoiding encoding errors. + .. change:: :tags: bug, oracle :tickets: 2870 diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 6110992bb1..1c6a4cc4c2 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -824,7 +824,7 @@ class Connection(Connectable): context = constructor(dialect, self, conn, *args) except Exception as e: self._handle_dbapi_exception(e, - str(statement), parameters, + util.text_type(statement), parameters, None, None) if context.compiled: diff --git a/test/engine/test_execute.py b/test/engine/test_execute.py index dbefc9f42a..8204eb5294 100644 --- a/test/engine/test_execute.py +++ b/test/engine/test_execute.py @@ -1,4 +1,4 @@ - +# coding: utf-8 from sqlalchemy.testing import eq_, assert_raises, assert_raises_message, \ config, is_ @@ -226,7 +226,7 @@ class ExecuteTest(fixtures.TestBase): def _go(conn): assert_raises_message( tsa.exc.StatementError, - r"nope \(original cause: Exception: nope\) 'SELECT 1 ", + r"nope \(original cause: Exception: nope\) u?'SELECT 1 ", conn.execute, select([1]).\ where( @@ -240,6 +240,24 @@ class ExecuteTest(fixtures.TestBase): finally: conn.close() + def test_stmt_exception_non_ascii(self): + name = util.u('méil') + assert_raises_message( + tsa.exc.StatementError, + util.u( + "A value is required for bind parameter 'uname'" + r'.*SELECT users.user_name AS "m\\xe9il"') if util.py2k + else + util.u( + "A value is required for bind parameter 'uname'" + '.*SELECT users.user_name AS "méil"') + , + testing.db.execute, + select([users.c.user_name.label(name)]).where( + users.c.user_name == bindparam("uname")), + {'uname_incorrect': 'foo'} + ) + def test_stmt_exception_pickleable_no_dbapi(self): self._test_stmt_exception_pickleable(Exception("hello world")) diff --git a/test/sql/test_query.py b/test/sql/test_query.py index 8c5e1db4d9..40c63b1793 100644 --- a/test/sql/test_query.py +++ b/test/sql/test_query.py @@ -68,7 +68,7 @@ class QueryTest(fixtures.TestBase): r"A value is required for bind parameter 'user_name', in " "parameter group 2 \(original cause: (sqlalchemy.exc.)?InvalidRequestError: A " "value is required for bind parameter 'user_name', in " - "parameter group 2\) 'INSERT INTO query_users", + "parameter group 2\) u?'INSERT INTO query_users", users.insert().execute, {'user_id':7, 'user_name':'jack'}, {'user_id':8, 'user_name':'ed'},