From c4f28fb414c342ba362fdee8a68bd169f01aa7d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bartosi=C5=84ski?= Date: Sat, 19 Sep 2020 12:47:46 -0400 Subject: Stringify correctly for non-str exception argument MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- lib/sqlalchemy/exc.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'lib') 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 -- cgit v1.2.1