[master 2/16] Make translation to lowerASCII Python[23]-compatible

M4rtinK installerbot-noreply at redhat.com
Fri Apr 10 13:38:08 UTC 2015


From: Vratislav Podzimek <vpodzime at redhat.com>

If we are given a byte string in Python 3 we need to convert it to str (unicode)
first because otherwise we would need another translation table (for the 'bytes'
type) and we would need to convert the result into str (unicode) because that's
what the calling code expects.
---
 blivet/size.py | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/blivet/size.py b/blivet/size.py
index 1300ce7..d2c39e3 100644
--- a/blivet/size.py
+++ b/blivet/size.py
@@ -22,6 +22,7 @@
 import re
 import string
 import locale
+import sys
 from collections import namedtuple
 
 from decimal import Decimal
@@ -78,14 +79,18 @@
 def _lowerASCII(s):
     """Convert a string to lowercase using only ASCII character definitions.
 
-       :param str s: string to convert
+       :param s: string instance to convert
+       :type s: str or bytes
        :returns: lower-cased string
        :rtype: str
     """
-    if six.PY2:
-        return string.translate(s, _ASCIIlower_table) # pylint: disable=no-member
-    else:
-        return str.translate(s, _ASCIIlower_table) # pylint: disable=no-member
+
+    # XXX: Python 3 has str.maketrans() and bytes.maketrans() so we should
+    # ideally use one or the other depending on the type of 's'. But it turns
+    # out we expect this function to always return string even if given bytes.
+    if not six.PY2 and isinstance(s, bytes):
+        s = s.decode(sys.getdefaultencoding())
+    return s.translate(_ASCIIlower_table) # pylint: disable=no-member
 
 def _makeSpec(prefix, suffix, xlate, lowercase=True):
     """ Synthesizes a whole word from prefix and suffix.


-- 
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/d736e54f5278d5600d50055903e38b55d5126a40


More information about the anaconda-patches mailing list