From: Andrzej Bartosiński Date: Sat, 19 Sep 2020 08:01:05 +0000 (+0200) Subject: Fix str(SQLAlchemyError()) (Fixes: #5599) X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=cdccccc42a6ac8de771593a43ee8675bfd8dbeb6;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fix str(SQLAlchemyError()) (Fixes: #5599) --- diff --git a/lib/sqlalchemy/exc.py b/lib/sqlalchemy/exc.py index c2308a5ccc..231b5720eb 100644 --- a/lib/sqlalchemy/exc.py +++ b/lib/sqlalchemy/exc.py @@ -56,10 +56,19 @@ class SQLAlchemyError(Exception): # if len(self.args) == 1: text = self.args[0] + if as_unicode and isinstance(text, compat.binary_types): - return compat.decode_backslashreplace(text, "utf-8") + text = compat.decode_backslashreplace(text, "utf-8") + + # This is for when the argument is not a string of any sort. + # Otherwise, converting this exception to string would fail for + # non-string arguments. + if compat.py3k or not as_unicode: + text = str(text) else: - return self.args[0] + text = compat.text_type(text) + + return text else: # this is not a normal case within SQLAlchemy but is here for # compatibility with Exception.args - the str() comes out as diff --git a/test/engine/test_execute.py b/test/engine/test_execute.py index 7e3893afdd..ed3dd80238 100644 --- a/test/engine/test_execute.py +++ b/test/engine/test_execute.py @@ -58,6 +58,15 @@ class SomeException(Exception): pass +class Foo(object): + + def __str__(self): + return "foo" + + def __unicode__(self): + return util.u("fóó") + + class ExecuteTest(fixtures.TablesTest): __backend__ = True @@ -412,6 +421,13 @@ class ExecuteTest(fixtures.TablesTest): else: eq_(str(err), util.u("some message méil")) + def test_stmt_exception_object_arg(self): + err = tsa.exc.SQLAlchemyError(Foo()) + eq_(str(err), "foo") + + if util.py2k: + eq_(unicode(err), util.u("fóó")) # noqa + def test_stmt_exception_str_multi_args(self): err = tsa.exc.SQLAlchemyError("some message", 206) eq_(str(err), "('some message', 206)")