[PATCH 1/3] Add ASCII-only upper and lower string functions.

David Shea dshea at redhat.com
Fri Aug 16 13:08:17 UTC 2013


On 08/15/2013 07:55 PM, Brian C. Lane wrote:
> On Thu, Aug 15, 2013 at 03:09:54PM -0400, David Shea wrote:
>
>> +def upperASCII(s):
>> +    """Convert a string to uppercase using only ASCII character definitions.
>> +
>> +    The returned string will contain only ASCII characters. This function is
>> +    locale-independent.
>> +    """
>> +    return string.translate(_toASCII(s), _ASCIIupper_table)
>> +
>> +def lowerASCII(s):
>> +    """Convert a string to lowercase using only ASCII character definitions.
>> +
>> +    The returned string will contain only ASCII characters. This function is
>> +    locale-independent.
>> +    """
>> +    return string.translate(_toASCII(s), _ASCIIlower_table)
> I didn't think of this the last time I looked, but these are going
> to need some input validation. They blow up with None, which may happen
> since a number of the calls are using .get()
>
> Some callers are expecting a string (eg. .startswith()) so returning ""
> in those failure cases may be the right thing to do.

Good point. I don't think it'll change the existing behavior, since 
these two functions are replacing instances of upper(), lower(), or the 
uppercase_ASCII_string function in simpleconfig that also blew up on 
None, but nothing wrong with making things not blow up. I changed 
_toASCII to return an empty string if the input type isn't StringType or 
UnicodeType.

def _toASCII(s):
     """Convert a unicode string to ASCII"""
     if type(s) == types.UnicodeType:
         # Decompose the string using the NFK decomposition, which in 
addition
         # to the canonical decomposition replaces characters based on
         # compatibility equivalence (e.g., ROMAN NUMERAL ONE has its 
own code
         # point but it's really just a capital I), so that we can keep 
as much
         # of the ASCII part of the string as possible.
         s = unicodedata.normalize('NKFD', s).encode('ascii', 'ignore')
     elif type(s) != types.StringType:
         s = ''
     return s


More information about the anaconda-patches mailing list