summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorPauli Virtanen <pav@iki.fi>2008-12-30 20:37:30 +0000
committerPauli Virtanen <pav@iki.fi>2008-12-30 20:37:30 +0000
commit8b4e9ab86738e87f94be10a5f98072c250ec5ffa (patch)
tree2b97b9f937ea00c4a1f21374218ab21867e5c85a /numpy
parent87d1c9c3c310874e1502c8691bc953fce539c166 (diff)
downloadnumpy-8b4e9ab86738e87f94be10a5f98072c250ec5ffa.tar.gz
Implement NumPyOS_ascii_strtod to work around a bug in PyOS_ascii_strtod. Use it to make fromstring properly locale-independent.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/arraytypes.inc.src6
-rw-r--r--numpy/core/src/npy_format.c43
2 files changed, 44 insertions, 5 deletions
diff --git a/numpy/core/src/arraytypes.inc.src b/numpy/core/src/arraytypes.inc.src
index 5b659f8cf..9886a83b3 100644
--- a/numpy/core/src/arraytypes.inc.src
+++ b/numpy/core/src/arraytypes.inc.src
@@ -979,19 +979,15 @@ static int
#fname=FLOAT,DOUBLE,LONGDOUBLE#
#type=float,double,longdouble#
*/
-#if (PY_VERSION_HEX >= 0x02040000) || defined(PyOS_ascii_strtod)
static int
@fname@_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@_fromstr NULL
-#endif
/**end repeat**/
diff --git a/numpy/core/src/npy_format.c b/numpy/core/src/npy_format.c
index 173e324c7..6cb233df4 100644
--- a/numpy/core/src/npy_format.c
+++ b/numpy/core/src/npy_format.c
@@ -295,6 +295,46 @@ NumPyOS_ascii_isspace(char c)
}
+/* 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 @@ buffer_filled:
/* 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 */