--- /dev/null
+.. 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.
#
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
pass
+class Foo(object):
+ def __str__(self):
+ return "foo"
+
+ def __unicode__(self):
+ return util.u("fóó")
+
+
class ExecuteTest(fixtures.TablesTest):
__backend__ = True
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"))
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"))
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)")