]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix str(SQLAlchemyError(<object>)) (Fixes: #5599) 5600/head
authorAndrzej Bartosiński <neob91@gmail.com>
Sat, 19 Sep 2020 08:01:05 +0000 (10:01 +0200)
committerAndrzej Bartosiński <neob91@gmail.com>
Sat, 19 Sep 2020 15:30:21 +0000 (17:30 +0200)
lib/sqlalchemy/exc.py
test/engine/test_execute.py

index c2308a5cccbf68d5f827300e67ed1e0c5cfa8f72..231b5720eba3b1860ac1d16c7888d72a4aba0186 100644 (file)
@@ -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
index 7e3893afddf2bce318ac7110c810ea49b80defe0..ed3dd80238d82a564521fa4307f843183659ab6f 100644 (file)
@@ -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)")