From 8a4c75ee7f4ab57b91c63735b12bd4a4427505ac Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Tue, 5 Dec 2017 20:54:23 -0500 Subject: [PATCH] rename fixPrint->_fix_print and add a docstring --- koji/__init__.py | 13 +++++++++---- koji/util.py | 4 ++-- tests/test_lib/test_fixEncoding.py | 10 +++++----- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/koji/__init__.py b/koji/__init__.py index 5de0225c..67819e95 100644 --- a/koji/__init__.py +++ b/koji/__init__.py @@ -2966,16 +2966,21 @@ def removeNonprintable(value): # expects raw-encoded string, not unicode return value.translate(None, NONPRINTABLE_CHARS) -def fixPrint(value): - if not value: - return str('') - elif six.PY2 and isinstance(value, six.text_type): + +def _fix_print(value): + """Fix a string so it is suitable to print + + In python2, this means we return a utf8 encoded str + In python3, this means we return unicode + """ + if six.PY2 and isinstance(value, six.text_type): return value.encode('utf8') elif six.PY3 and isinstance(value, six.binary_type): return value.decode('utf8') else: return value + def fixEncoding(value, fallback='iso8859-15', remove_nonprintable=False): """ Convert value to a 'str' object encoded as UTF-8. diff --git a/koji/util.py b/koji/util.py index aa8a44a5..f7f50b52 100644 --- a/koji/util.py +++ b/koji/util.py @@ -62,8 +62,8 @@ def formatChangelog(entries): %s """ % (_changelogDate(entry['date']), - koji.fixPrint(entry['author']), - koji.fixPrint(entry['text'])) + koji._fix_print(entry['author']), + koji._fix_print(entry['text'])) return result DATE_RE = re.compile(r'(\d+)-(\d+)-(\d+)') diff --git a/tests/test_lib/test_fixEncoding.py b/tests/test_lib/test_fixEncoding.py index b5b6f19c..2f7ca8fb 100644 --- a/tests/test_lib/test_fixEncoding.py +++ b/tests/test_lib/test_fixEncoding.py @@ -46,15 +46,15 @@ class FixEncodingTestCase(unittest.TestCase): self.assertEqual(koji.fixEncoding(d, remove_nonprintable=True), b) @mock.patch('sys.stdout', new_callable=six.StringIO) - def test_fixPrint(self, stdout): - """Test the fixPrint function""" + def test_fix_print(self, stdout): + """Test the _fix_print function""" expected = '' for a, b in self.simple_values: if six.PY3: - self.assertEqual(koji.fixPrint(b), a) + self.assertEqual(koji._fix_print(b), a) else: - self.assertEqual(koji.fixPrint(b), b) - print(koji.fixPrint(b)) + self.assertEqual(koji._fix_print(b), b) + print(koji._fix_print(b)) if six.PY3: expected = expected + a + '\n' else: