fix str-type testing in fix_encoding

Fixes: https://pagure.io/koji/issue/1318
This commit is contained in:
Tomas Kopecek 2019-03-05 12:03:17 +01:00 committed by Mike McLean
parent 7532682f5e
commit df11172629
2 changed files with 7 additions and 7 deletions

View file

@ -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)

View file

@ -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"""