From 2db54ee92ebd0970f52b271e152a6df9b563693f Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 10 Jan 2019 12:03:40 -0500 Subject: Leave bytestring exception messages as bytestrings Fixed a regression introduced in version 1.2 where a refactor of the :class:`.SQLAlchemyError` base exception class introduced an inappropriate coercion of a plain string message into Unicode under python 2k, which is not handled by the Python interpreter for characters outside of the platform's encoding (typically ascii). The :class:`.SQLAlchemyError` class now passes a bytestring through under Py2K for ``__str__()`` as is the behavior of exception objects in general under Py2K, does a safe coercion to unicode utf-8 with backslash fallback for ``__unicode__()``. For Py3K the message is typically unicode already, but if not is again safe-coerced with utf-8 with backslash fallback for the ``__str__()`` method. Fixes: #4429 Change-Id: I2289da3f2c45c7d0041fa43d838958f7614defc3 --- lib/sqlalchemy/testing/suite/test_dialect.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'lib/sqlalchemy/testing') diff --git a/lib/sqlalchemy/testing/suite/test_dialect.py b/lib/sqlalchemy/testing/suite/test_dialect.py index 245ccc6f0..1b3307042 100644 --- a/lib/sqlalchemy/testing/suite/test_dialect.py +++ b/lib/sqlalchemy/testing/suite/test_dialect.py @@ -1,3 +1,5 @@ +#! coding: utf-8 + from .. import assert_raises from .. import config from .. import eq_ @@ -11,6 +13,7 @@ from ... import Integer from ... import literal_column from ... import select from ... import String +from ...util import compat class ExceptionTest(fixtures.TablesTest): @@ -53,6 +56,28 @@ class ExceptionTest(fixtures.TablesTest): trans.rollback() + def test_exception_with_non_ascii(self): + with config.db.connect() as conn: + try: + # try to create an error message that likely has non-ascii + # characters in the DBAPI's message string. unfortunately + # there's no way to make this happen with some drivers like + # mysqlclient, pymysql. this at least does produce a non- + # ascii error message for cx_oracle, psycopg2 + conn.execute(select([literal_column(u"méil")])) + assert False + except exc.DBAPIError as err: + err_str = str(err) + + assert str(err.orig) in str(err) + + # test that we are actually getting string on Py2k, unicode + # on Py3k. + if compat.py2k: + assert isinstance(err_str, str) + else: + assert isinstance(err_str, str) + class AutocommitTest(fixtures.TablesTest): -- cgit v1.2.1