From 6966bdcf28ec709e794d4b0c65cb72b0275d8e92 Mon Sep 17 00:00:00 2001 From: Eric Fode Date: Tue, 10 Jul 2012 14:09:44 -0400 Subject: BUG: ticket #2028: When min_scalar was not checking to see if numbers could fit into unsigned long longs. This was fixed by adding an extra case to function to check if the passed number could fit into a unsigned long long if it could not fit into anything else. Additionaly, regression tests were added. --- numpy/core/src/multiarray/common.c | 10 ++++++++-- numpy/core/tests/test_multiarray.py | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/common.c b/numpy/core/src/multiarray/common.c index f62b37b4d..82c2b8f71 100644 --- a/numpy/core/src/multiarray/common.c +++ b/numpy/core/src/multiarray/common.c @@ -36,9 +36,15 @@ _array_find_python_scalar_type(PyObject *op) /* if integer can fit into a longlong then return that*/ if ((PyLong_AsLongLong(op) == -1) && PyErr_Occurred()) { PyErr_Clear(); + if((PyLong_AsUnsignedLongLong(op) == -1) && PyErr_Occurred()){ + PyErr_Clear(); + } else { + return PyArray_DescrFromType(NPY_ULONGLONG); + } return PyArray_DescrFromType(NPY_OBJECT); - } - return PyArray_DescrFromType(NPY_LONGLONG); + } else { + return PyArray_DescrFromType(NPY_LONGLONG); + } } return NULL; } diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 99861748c..1d55a0ce6 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -2377,6 +2377,29 @@ class TestWarnings(object): finally: warn_ctx.__exit__() +class TestMinScalarType(object): + def test_usigned_shortshort(self): + dt = np.min_scalar_type(2**8-1) + wanted = np.dtype('uint8') + assert_equal(wanted, dt) + def test_usigned_short(self): + dt = np.min_scalar_type(2**16-1) + wanted = np.dtype('uint16') + assert_equal(wanted, dt) + def test_usigned_int(self): + dt = np.min_scalar_type(2**32-1) + wanted = np.dtype('uint32') + assert_equal(wanted, dt) + def test_usigned_longlong(self): + dt = np.min_scalar_type(2**63-1) + wanted = np.dtype('uint64') + assert_equal(wanted, dt) + def test_object(self): + dt = np.min_scalar_type(2**64) + wanted = np.dtype('O') + assert_equal(wanted, dt) + + if sys.version_info >= (2, 6): if sys.version_info[:2] == (2, 6): -- cgit v1.2.1