diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-01-10 12:03:40 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-01-10 18:02:00 -0500 |
| commit | 2db54ee92ebd0970f52b271e152a6df9b563693f (patch) | |
| tree | 121e32a82892542086ee6418686daef9b9a6a07c /lib/sqlalchemy/util/compat.py | |
| parent | f1706ae317ab5e3b263420e6218696821fbcd878 (diff) | |
| download | sqlalchemy-2db54ee92ebd0970f52b271e152a6df9b563693f.tar.gz | |
Leave bytestring exception messages as bytestrings
Fixed a regression introduced in version 1.2 where a refactor
of the :class:`.SQLAlchemyError` base exception class introduced an
inappropriate coercion of a plain string message into Unicode under
python 2k, which is not handled by the Python interpreter for characters
outside of the platform's encoding (typically ascii). The
:class:`.SQLAlchemyError` class now passes a bytestring through under
Py2K for ``__str__()`` as is the behavior of exception objects in general
under Py2K, does a safe coercion to unicode utf-8 with
backslash fallback for ``__unicode__()``. For Py3K the message is
typically unicode already, but if not is again safe-coerced with utf-8
with backslash fallback for the ``__str__()`` method.
Fixes: #4429
Change-Id: I2289da3f2c45c7d0041fa43d838958f7614defc3
Diffstat (limited to 'lib/sqlalchemy/util/compat.py')
| -rw-r--r-- | lib/sqlalchemy/util/compat.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/sqlalchemy/util/compat.py b/lib/sqlalchemy/util/compat.py index 7963eebb6..6c24f75e1 100644 --- a/lib/sqlalchemy/util/compat.py +++ b/lib/sqlalchemy/util/compat.py @@ -92,6 +92,9 @@ if py3k: def b64encode(x): return base64.b64encode(x).decode("ascii") + def decode_backslashreplace(text, encoding): + return text.decode(encoding, errors="backslashreplace") + def cmp(a, b): return (a > b) - (a < b) @@ -195,6 +198,15 @@ else: def ue(s): return unicode(s, "unicode_escape") # noqa + def decode_backslashreplace(text, encoding): + try: + return text.decode(encoding) + except UnicodeDecodeError: + # regular "backslashreplace" for an incompatible encoding raises: + # "TypeError: don't know how to handle UnicodeDecodeError in + # error callback" + return repr(text)[1:-1].decode() + # not as nice as that of Py3K, but at least preserves # the code line where the issue occurred exec( |
