[Numpy-svn] r6279 - branches/fix_float_format/numpy/core/src

numpy-svn at scipy.org numpy-svn at scipy.org
Tue Dec 30 15:37:43 EST 2008


Author: ptvirtan
Date: 2008-12-30 14:37:30 -0600 (Tue, 30 Dec 2008)
New Revision: 6279

Modified:
   branches/fix_float_format/numpy/core/src/arraytypes.inc.src
   branches/fix_float_format/numpy/core/src/npy_format.c
Log:
Implement NumPyOS_ascii_strtod to work around a bug in PyOS_ascii_strtod. Use it to make fromstring properly locale-independent.

Modified: branches/fix_float_format/numpy/core/src/arraytypes.inc.src
===================================================================
--- branches/fix_float_format/numpy/core/src/arraytypes.inc.src	2008-12-30 20:37:05 UTC (rev 6278)
+++ branches/fix_float_format/numpy/core/src/arraytypes.inc.src	2008-12-30 20:37:30 UTC (rev 6279)
@@ -979,19 +979,15 @@
 #fname=FLOAT,DOUBLE,LONGDOUBLE#
 #type=float,double,longdouble#
 */
-#if (PY_VERSION_HEX >= 0x02040000) || defined(PyOS_ascii_strtod)
 static int
 @fname at _fromstr(char *str, @type@ *ip, char **endptr, PyArray_Descr *NPY_UNUSED(ignore))
 {
     double result;
 
-    result = PyOS_ascii_strtod(str, endptr);
+    result = NumPyOS_ascii_strtod(str, endptr);
     *ip = (@type@) result;
     return 0;
 }
-#else
-#define @fname at _fromstr NULL
-#endif
 /**end repeat**/
 
 

Modified: branches/fix_float_format/numpy/core/src/npy_format.c
===================================================================
--- branches/fix_float_format/numpy/core/src/npy_format.c	2008-12-30 20:37:05 UTC (rev 6278)
+++ branches/fix_float_format/numpy/core/src/npy_format.c	2008-12-30 20:37:30 UTC (rev 6279)
@@ -295,6 +295,46 @@
 }
 
 
+/* NumPyOS_ascii_strtod:
+ *
+ * Work around bugs in PyOS_ascii_strtod
+ */
+static double
+NumPyOS_ascii_strtod(const char *s, char** endptr)
+{
+    char buffer[FLOAT_FORMATBUFLEN+1];
+    char *p;
+    size_t n;
+    double result;
+
+    /* ## 1
+     *
+     * At least Python versions <= 2.5.2 and <= 2.6.1
+     *
+     * Fails to do best-efforts parsing of strings of the form "1,234"
+     * under foreign locale.
+     */
+    p = s;
+    while ((*p >= '0' && *p <= '9') || *p == '+' || *p == '-'
+           || NumPyOS_ascii_isspace(*p)) {
+        ++p;
+    }
+    if (*p == ',') {
+        n = (size_t)(p - s);
+        if (n > FLOAT_FORMATBUFLEN)
+            n = FLOAT_FORMATBUFLEN;
+        memcpy(buffer, s, n);
+        buffer[n] = '\0';
+        result = PyOS_ascii_strtod(buffer, &p);
+        *endptr = s + (p - buffer);
+        return result;
+    }
+    /* End of ##1 */
+
+    return PyOS_ascii_strtod(s, endptr);
+}
+
+
 /*
  * NumPyOS_ascii_ftolf:
  * 	* fp: FILE pointer
@@ -429,6 +469,9 @@
 
     /* 5. try to convert buffer. */
 
+    /* No need for NumPyOS here, the bugs in PyOS_ascii_strtod discussed
+       above can't manifest here, since the above parsing only copies
+       "good" strings. */
     *value = PyOS_ascii_strtod(buffer, &p);
 
     return (buffer == p) ? 0 : 1; /* if something was read */




More information about the Numpy-svn mailing list