summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomaz Solc <tomaz.solc@tablix.org>2021-01-08 16:26:30 +0100
committerTomaz Solc <tomaz.solc@tablix.org>2021-01-08 16:26:30 +0100
commit80625ee2a67c9654230bc0617c8911a3d26ea16c (patch)
tree971626830b7fac434e3058f191aa86aaec75e17a
parent2f5e019513543a51d8f1ce5a3a70f2e5e22d9861 (diff)
downloadunidecode-80625ee2a67c9654230bc0617c8911a3d26ea16c.tar.gz
Avoid exception chaining on Python 3.
This avoids exceptions raised by errors='strict' from displaying as "During handling of the above exception ..." in the backtrace which can be confusing.
-rw-r--r--tests/test_unidecode.py9
-rw-r--r--unidecode/__init__.py11
2 files changed, 16 insertions, 4 deletions
diff --git a/tests/test_unidecode.py b/tests/test_unidecode.py
index 5fadf46..4a93301 100644
--- a/tests/test_unidecode.py
+++ b/tests/test_unidecode.py
@@ -525,6 +525,11 @@ class BaseTestUnidecode():
self.assertEqual(5, e.exception.index)
+ # This checks that the exception is not chained (i.e. you don't get the
+ # "During handling of the above exception, another exception occurred")
+ if sys.version_info[0] >= 3:
+ self.assertIsNone(e.exception.__context__)
+
@unittest.skipIf(sys.maxunicode < 0x10000, "narrow build")
def test_errors_preserve(self):
s = u"test \U000f0000 test"
@@ -537,6 +542,10 @@ class BaseTestUnidecode():
with self.assertRaises(UnidecodeError) as e:
self.unidecode(u"test \U000f0000 test", errors='invalid')
+ # This checks that the exception is not chained (i.e. you don't get the
+ # "During handling of the above exception, another exception occurred")
+ if sys.version_info[0] >= 3:
+ self.assertIsNone(e.exception.__context__)
class TestUnidecode(BaseTestUnidecode, unittest.TestCase):
unidecode = staticmethod(unidecode)
diff --git a/unidecode/__init__.py b/unidecode/__init__.py
index 54c543d..776d6e4 100644
--- a/unidecode/__init__.py
+++ b/unidecode/__init__.py
@@ -65,11 +65,14 @@ def unidecode_expect_ascii(string, errors='ignore', replace_str='?'):
try:
bytestring = string.encode('ASCII')
except UnicodeEncodeError:
- return _unidecode(string, errors, replace_str)
- if version_info[0] >= 3:
- return string
+ pass
else:
- return bytestring
+ if version_info[0] >= 3:
+ return string
+ else:
+ return bytestring
+
+ return _unidecode(string, errors, replace_str)
def unidecode_expect_nonascii(string, errors='ignore', replace_str='?'):
"""Transliterate an Unicode object into an ASCII string