[Numpy-svn] r4836 - in trunk/numpy/core: . code_generators

numpy-svn@scip... numpy-svn@scip...
Fri Feb 29 15:21:05 CST 2008


Author: rkern
Date: 2008-02-29 15:21:04 -0600 (Fri, 29 Feb 2008)
New Revision: 4836

Modified:
   trunk/numpy/core/code_generators/generate_umath.py
   trunk/numpy/core/numerictypes.py
Log:
Fix #643. This doesn't resolve *all* of the issues of working within a Turkish locale, but this will work around the bug that the OLPC project found.

Modified: trunk/numpy/core/code_generators/generate_umath.py
===================================================================
--- trunk/numpy/core/code_generators/generate_umath.py	2008-02-29 06:35:47 UTC (rev 4835)
+++ trunk/numpy/core/code_generators/generate_umath.py	2008-02-29 21:21:04 UTC (rev 4836)
@@ -562,6 +562,39 @@
 # 2) fill in functions and data in InitOperators
 # 3) add function.
 
+# String-handling utilities to avoid locale-dependence.
+
+import string
+UPPER_TABLE = string.maketrans(string.ascii_lowercase, string.ascii_uppercase)
+
+def english_upper(s):
+    """ Apply English case rules to convert ASCII strings to all upper case.
+
+    This is an internal utility function to replace calls to str.upper() such
+    that we can avoid changing behavior with changing locales. In particular,
+    Turkish has distinct dotted and dotless variants of the Latin letter "I" in
+    both lowercase and uppercase. Thus, "i".upper() != "I" in a "tr" locale.
+
+    Parameters
+    ----------
+    s : str
+
+    Returns
+    -------
+    uppered : str
+
+    Examples
+    --------
+    >>> from numpy.lib.utils import english_upper
+    >>> english_upper('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_')
+    'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
+    >>> english_upper('')
+    ''
+    """
+    uppered = s.translate(UPPER_TABLE)
+    return uppered
+
+
 def make_arrays(funcdict):
     # functions array contains an entry for every type implemented
     #   NULL should be placed where PyUfunc_ style function will be filled in later
@@ -606,11 +639,11 @@
                 sub += 1
             else:
                 datalist.append('(void *)NULL');
-                tname = chartoname[t.type].upper()
+                tname = english_upper(chartoname[t.type])
                 funclist.append('%s_%s' % (tname, name))
 
             for x in t.in_ + t.out:
-                siglist.append('PyArray_%s' % (chartoname[x].upper(),))
+                siglist.append('PyArray_%s' % (english_upper(chartoname[x]),))
 
             k += 1
 

Modified: trunk/numpy/core/numerictypes.py
===================================================================
--- trunk/numpy/core/numerictypes.py	2008-02-29 06:35:47 UTC (rev 4835)
+++ trunk/numpy/core/numerictypes.py	2008-02-29 21:21:04 UTC (rev 4836)
@@ -87,6 +87,98 @@
 # as numerictypes.bool, etc.
 from __builtin__ import bool, int, long, float, complex, object, unicode, str
 
+
+# String-handling utilities to avoid locale-dependence.
+
+import string
+LOWER_TABLE = string.maketrans(string.ascii_uppercase, string.ascii_lowercase)
+UPPER_TABLE = string.maketrans(string.ascii_lowercase, string.ascii_uppercase)
+
+def english_lower(s):
+    """ Apply English case rules to convert ASCII strings to all lower case.
+
+    This is an internal utility function to replace calls to str.lower() such
+    that we can avoid changing behavior with changing locales. In particular,
+    Turkish has distinct dotted and dotless variants of the Latin letter "I" in
+    both lowercase and uppercase. Thus, "I".lower() != "i" in a "tr" locale.
+
+    Parameters
+    ----------
+    s : str
+
+    Returns
+    -------
+    lowered : str
+
+    Examples
+    --------
+    >>> from numpy.lib.utils import english_lower
+    >>> english_lower('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_')
+    'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789_'
+    >>> english_upper('')
+    ''
+    """
+    lowered = s.translate(LOWER_TABLE)
+    return lowered
+
+def english_upper(s):
+    """ Apply English case rules to convert ASCII strings to all upper case.
+
+    This is an internal utility function to replace calls to str.upper() such
+    that we can avoid changing behavior with changing locales. In particular,
+    Turkish has distinct dotted and dotless variants of the Latin letter "I" in
+    both lowercase and uppercase. Thus, "i".upper() != "I" in a "tr" locale.
+
+    Parameters
+    ----------
+    s : str
+
+    Returns
+    -------
+    uppered : str
+
+    Examples
+    --------
+    >>> from numpy.lib.utils import english_upper
+    >>> english_upper('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_')
+    'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
+    >>> english_upper('')
+    ''
+    """
+    uppered = s.translate(UPPER_TABLE)
+    return uppered
+
+def english_capitalize(s):
+    """ Apply English case rules to convert the first character of an ASCII
+    string to upper case.
+
+    This is an internal utility function to replace calls to str.capitalize()
+    such that we can avoid changing behavior with changing locales.
+
+    Parameters
+    ----------
+    s : str
+
+    Returns
+    -------
+    capitalized : str
+
+    Examples
+    --------
+    >>> from numpy.lib.utils import english_capitalize
+    >>> english_capitalize('int8')
+    'Int8'
+    >>> english_capitalize('Int8')
+    'Int8'
+    >>> english_capitalize('')
+    ''
+    """
+    if s:
+        return english_upper(s[0]) + s[1:]
+    else:
+        return s
+
+
 sctypeDict = {}      # Contains all leaf-node scalar types with aliases
 sctypeNA = {}        # Contails all leaf-node types -> numarray type equivalences
 allTypes = {}      # Collect the types we will add to the module here
@@ -114,7 +206,7 @@
             newname = name[:-1]
         else:
             newname = name
-        info = typeinfo[newname.upper()]
+        info = typeinfo[english_upper(newname)]
         assert(info[-1] == obj)  # sanity check
         bits = info[2]
 
@@ -149,7 +241,7 @@
 
 def _add_types():
     for a in typeinfo.keys():
-        name = a.lower()
+        name = english_lower(a)
         if isinstance(typeinfo[a], tuple):
             typeobj = typeinfo[a][-1]
 
@@ -165,7 +257,7 @@
 
 def _add_aliases():
     for a in typeinfo.keys():
-        name = a.lower()
+        name = english_lower(a)
         if not isinstance(typeinfo[a], tuple):
             continue
         typeobj = typeinfo[a][-1]
@@ -179,12 +271,12 @@
                 allTypes[myname] = typeobj
                 sctypeDict[myname] = typeobj
                 if base == 'complex':
-                    na_name = '%s%d' % (base.capitalize(), bit/2)
+                    na_name = '%s%d' % (english_capitalize(base), bit/2)
                 elif base == 'bool':
-                    na_name = base.capitalize()
+                    na_name = english_capitalize(base)
                     sctypeDict[na_name] = typeobj
                 else:
-                    na_name = "%s%d" % (base.capitalize(), bit)
+                    na_name = "%s%d" % (english_capitalize(base), bit)
                     sctypeDict[na_name] = typeobj
                 sctypeNA[na_name] = typeobj
                 sctypeDict[na_name] = typeobj



More information about the Numpy-svn mailing list