summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/exc.py
diff options
context:
space:
mode:
authorAndrzej Bartosiński <neob91@gmail.com>2020-09-19 12:47:46 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2020-09-20 14:09:23 -0400
commitc4f28fb414c342ba362fdee8a68bd169f01aa7d2 (patch)
tree09711543699fd5ae3c8bfed3ed5748e42b0ab2b8 /lib/sqlalchemy/exc.py
parent29575552b04f4d4e4f7373a8ddcaa2572046029e (diff)
downloadsqlalchemy-c4f28fb414c342ba362fdee8a68bd169f01aa7d2.tar.gz
Stringify correctly for non-str exception argument
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
Diffstat (limited to 'lib/sqlalchemy/exc.py')
-rw-r--r--lib/sqlalchemy/exc.py12
1 files changed, 10 insertions, 2 deletions
diff --git a/lib/sqlalchemy/exc.py b/lib/sqlalchemy/exc.py
index c2308a5cc..b80bf9b01 100644
--- a/lib/sqlalchemy/exc.py
+++ b/lib/sqlalchemy/exc.py
@@ -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