From df1117262992fdcc42831bca63436ba91b7c9236 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: May 21 2019 16:18:19 +0000 Subject: fix str-type testing in fix_encoding Fixes: https://pagure.io/koji/issue/1318 --- diff --git a/koji/__init__.py b/koji/__init__.py index 024ce3d..54f702d 100644 --- a/koji/__init__.py +++ b/koji/__init__.py @@ -3071,7 +3071,6 @@ def removeNonprintable(value): return value.translate(NONPRINTABLE_CHARS_TABLE) - def _fix_print(value): """Fix a string so it is suitable to print @@ -3114,11 +3113,10 @@ def fix_encoding(value, fallback='iso8859-15', remove_nonprintable=False): # play encoding tricks for py2 strings if six.PY2: - if isinstance(value, six.text_type): - # value is already unicode, so just convert it - # to a utf8-encoded str + if isinstance(value, unicode): + # just convert it to a utf8-encoded str value = value.encode('utf8') - elif isinstance(value, six.binary_type): + elif isinstance(value, str): # value is a str, but may be encoded in utf8 or some # other non-ascii charset. Try to verify it's utf8, and if not, # decode it using the fallback encoding. @@ -3140,7 +3138,7 @@ def fix_encoding(value, fallback='iso8859-15', remove_nonprintable=False): def fixEncodingRecurse(value, fallback='iso8859-15', remove_nonprintable=False): """Recursively fix string encoding in an object - This is simply fixEncoding2 recursively applied to an object + This is simply fixEncoding recursively applied to an object """ kwargs = {'fallback': fallback, 'remove_nonprintable': remove_nonprintable} walker = util.DataWalker(value, fix_encoding, kwargs) diff --git a/tests/test_lib/test_fixEncoding.py b/tests/test_lib/test_fixEncoding.py index e53622e..ab26399 100644 --- a/tests/test_lib/test_fixEncoding.py +++ b/tests/test_lib/test_fixEncoding.py @@ -17,7 +17,7 @@ class FixEncodingTestCase(unittest.TestCase): """Main test case container""" simple_values = [ - # [ value, fixed ] + # [ unicode value, utf-8 encoded string ] ['', ''], [u'', ''], [u'góðan daginn', 'g\xc3\xb3\xc3\xb0an daginn'], @@ -51,6 +51,8 @@ class FixEncodingTestCase(unittest.TestCase): self.assertEqual(koji.fixEncoding(d, remove_nonprintable=True), b) else: self.assertEqual(koji.fixEncoding(a), a) + d = a[:-3] + u'\x00\x01' + a[-3:] + self.assertEqual(koji.fixEncoding(d, remove_nonprintable=True), a) def test_fix_print(self): """Test the _fix_print function"""