]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Stringify correctly for non-str exception argument
authorAndrzej Bartosiński <neob91@gmail.com>
Sat, 19 Sep 2020 16:47:46 +0000 (12:47 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 20 Sep 2020 18:10:59 +0000 (14:10 -0400)
Fixed issue where a non-string object sent to
:class:`_exc.SQLAlchemyError` or a subclass, as occurs with some third
party dialects, would fail to stringify correctly. Pull request
courtesy Andrzej Bartosiński.

Fixes: #5599
Closes: #5600
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5600
Pull-request-sha: cdccccc42a6ac8de771593a43ee8675bfd8dbeb6

Change-Id: Icd710d9015abc80f61a84893d75fbb33ee0fe46e
(cherry picked from commit c4f28fb414c342ba362fdee8a68bd169f01aa7d2)

doc/build/changelog/unreleased_13/5599.rst [new file with mode: 0644]
lib/sqlalchemy/exc.py
test/engine/test_execute.py

diff --git a/doc/build/changelog/unreleased_13/5599.rst b/doc/build/changelog/unreleased_13/5599.rst
new file mode 100644 (file)
index 0000000..c5a5dcc
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, engine
+    :tickets: 5599
+
+    Fixed issue where a non-string object sent to
+    :class:`_exc.SQLAlchemyError` or a subclass, as occurs with some third
+    party dialects, would fail to stringify correctly. Pull request
+    courtesy Andrzej Bartosiński.
index 8d8c2080240e0907afbb980beef56f7bced3e67f..b86803097d30ab64835ae2fb504bdc49e70ef9f9 100644 (file)
@@ -56,10 +56,18 @@ 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.
+            elif 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 304ffe6d4139784d8edecad22825ddd7231ff29b..7836ef567affaacfefa36dccde3ec9f433c468aa 100644 (file)
@@ -54,6 +54,14 @@ class SomeException(Exception):
     pass
 
 
+class Foo(object):
+    def __str__(self):
+        return "foo"
+
+    def __unicode__(self):
+        return util.u("fóó")
+
+
 class ExecuteTest(fixtures.TestBase):
     __backend__ = True
 
@@ -437,7 +445,7 @@ class ExecuteTest(fixtures.TestBase):
             eq_(str(err), message)
 
             # unicode accessor decodes to utf-8
-            eq_(unicode(err), util.u("some message méil"))  # noqa
+            eq_(unicode(err), util.u("some message méil"))  # noqa F821
         else:
             eq_(str(err), util.u("some message méil"))
 
@@ -452,7 +460,7 @@ class ExecuteTest(fixtures.TestBase):
             eq_(str(err), message)
 
             # unicode accessor decodes to utf-8
-            eq_(unicode(err), util.u("some message m\\xe9il"))  # noqa
+            eq_(unicode(err), util.u("some message m\\xe9il"))  # noqa F821
         else:
             eq_(str(err), util.u("some message m\\xe9il"))
 
@@ -463,10 +471,17 @@ class ExecuteTest(fixtures.TestBase):
 
         err = tsa.exc.SQLAlchemyError(message)
         if util.py2k:
-            eq_(unicode(err), util.u("some message méil"))  # noqa
+            eq_(unicode(err), util.u("some message méil"))  # noqa F821
         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 F821
+
     def test_stmt_exception_str_multi_args(self):
         err = tsa.exc.SQLAlchemyError("some message", 206)
         eq_(str(err), "('some message', 206)")