summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Taylor <jtaylor.debian@googlemail.com>2014-05-14 20:31:07 +0200
committerJulian Taylor <jtaylor.debian@googlemail.com>2014-05-14 20:53:56 +0200
commitceffd0443e548b728708e50a2e1ffc5591c0161b (patch)
treeed7b931ed61f7c438ea46965b7cf18325aee4f17
parentafe32d77b3540f11ff564f851f1db5afd26857b3 (diff)
downloadnumpy-ceffd0443e548b728708e50a2e1ffc5591c0161b.tar.gz
BUG: Fix string to int64 conversion on 32-bit platforms
closes gh-4575
-rw-r--r--numpy/core/setup_common.py3
-rw-r--r--numpy/core/src/multiarray/arraytypes.c.src33
-rw-r--r--numpy/core/tests/test_multiarray.py12
3 files changed, 44 insertions, 4 deletions
diff --git a/numpy/core/setup_common.py b/numpy/core/setup_common.py
index 85e92d923..e66b1653a 100644
--- a/numpy/core/setup_common.py
+++ b/numpy/core/setup_common.py
@@ -99,7 +99,8 @@ MANDATORY_FUNCS = ["sin", "cos", "tan", "sinh", "cosh", "tanh", "fabs",
# replacement implementation. Note that some of these are C99 functions.
OPTIONAL_STDFUNCS = ["expm1", "log1p", "acosh", "asinh", "atanh",
"rint", "trunc", "exp2", "log2", "hypot", "atan2", "pow",
- "copysign", "nextafter", "ftello", "fseeko"]
+ "copysign", "nextafter", "ftello", "fseeko",
+ "strtoll", "strtoull"]
OPTIONAL_HEADERS = [
diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src
index 4043b5490..8e22afa31 100644
--- a/numpy/core/src/multiarray/arraytypes.c.src
+++ b/numpy/core/src/multiarray/arraytypes.c.src
@@ -105,6 +105,31 @@ MyPyLong_AsUnsigned@Type@ (PyObject *obj)
/**end repeat**/
+static npy_longlong
+npy_strtoll(const char *str, char **endptr, int base)
+{
+#if defined HAVE_STRTOLL
+ return strtoll(str, endptr, base);
+#elif defined _MSC_VER
+ return _strtoi64(str, endptr, base);
+#else
+ /* ok on 64 bit posix */
+ return PyOS_strtol(str, endptr, base);
+#endif
+}
+
+static npy_ulonglong
+npy_strtoull(const char *str, char **endptr, int base)
+{
+#if defined HAVE_STRTOULL
+ return strtoull(str, endptr, base);
+#elif defined _MSC_VER
+ return _strtoui64(str, endptr, base);
+#else
+ /* ok on 64 bit posix */
+ return PyOS_strtoul(str, endptr, base);
+#endif
+}
/*
*****************************************************************************
@@ -1506,8 +1531,10 @@ BOOL_scan(FILE *fp, npy_bool *ip, void *NPY_UNUSED(ignore),
* #type = npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint,
* npy_long, npy_ulong, npy_longlong, npy_ulonglong,
* npy_datetime, npy_timedelta#
- * #func = (l, ul)*5, l, l#
- * #btype = (npy_long, npy_ulong)*5, npy_long, npy_long#
+ * #func = (PyOS_strtol, PyOS_strtoul)*4, npy_strtoll, npy_strtoull,
+ * npy_strtoll*2#
+ * #btype = (npy_long, npy_ulong)*4, npy_longlong, npy_ulonglong,
+ * npy_longlong*2#
*/
static int
@fname@_fromstr(char *str, @type@ *ip, char **endptr,
@@ -1515,7 +1542,7 @@ static int
{
@btype@ result;
- result = PyOS_strto@func@(str, endptr, 10);
+ result = @func@(str, endptr, 10);
*ip = (@type@) result;
return 0;
}
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index 7dae0f163..bc3670653 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -2444,6 +2444,18 @@ class TestIO(object):
y = np.fromstring('1 0 -2.3 0.0', sep=' ', dtype=np.bool_)
assert_array_equal(v, y)
+ def test_uint64_fromstring(self):
+ d = np.fromstring("9923372036854775807 104783749223640",
+ dtype=np.uint64, sep=' ');
+ e = np.array([9923372036854775807, 104783749223640], dtype=np.uint64)
+ assert_array_equal(d, e)
+
+ def test_int64_fromstring(self):
+ d = np.fromstring("-25041670086757 104783749223640",
+ dtype=np.int64, sep=' ');
+ e = np.array([-25041670086757, 104783749223640], dtype=np.int64)
+ assert_array_equal(d, e)
+
def test_empty_files_binary(self):
f = open(self.filename, 'w')
f.close()