Skip to content

Commit 6966bdc

Browse files
Eric Fodeericfode
authored andcommitted
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.
1 parent 6a1ab03 commit 6966bdc

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎numpy/core/src/multiarray/common.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ _array_find_python_scalar_type(PyObject *op)
3636
/* if integer can fit into a longlong then return that*/
3737
if ((PyLong_AsLongLong(op) == -1) && PyErr_Occurred()) {
3838
PyErr_Clear();
39+
if((PyLong_AsUnsignedLongLong(op) == -1) && PyErr_Occurred()){
40+
PyErr_Clear();
41+
} else {
42+
return PyArray_DescrFromType(NPY_ULONGLONG);
43+
}
3944
return PyArray_DescrFromType(NPY_OBJECT);
40-
}
41-
return PyArray_DescrFromType(NPY_LONGLONG);
45+
} else {
46+
return PyArray_DescrFromType(NPY_LONGLONG);
47+
}
4248
}
4349
return NULL;
4450
}

‎numpy/core/tests/test_multiarray.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,6 +2377,29 @@ def test_complex_warning(self):
23772377
finally:
23782378
warn_ctx.__exit__()
23792379

2380+
class TestMinScalarType(object):
2381+
def test_usigned_shortshort(self):
2382+
dt = np.min_scalar_type(2**8-1)
2383+
wanted = np.dtype('uint8')
2384+
assert_equal(wanted, dt)
2385+
def test_usigned_short(self):
2386+
dt = np.min_scalar_type(2**16-1)
2387+
wanted = np.dtype('uint16')
2388+
assert_equal(wanted, dt)
2389+
def test_usigned_int(self):
2390+
dt = np.min_scalar_type(2**32-1)
2391+
wanted = np.dtype('uint32')
2392+
assert_equal(wanted, dt)
2393+
def test_usigned_longlong(self):
2394+
dt = np.min_scalar_type(2**63-1)
2395+
wanted = np.dtype('uint64')
2396+
assert_equal(wanted, dt)
2397+
def test_object(self):
2398+
dt = np.min_scalar_type(2**64)
2399+
wanted = np.dtype('O')
2400+
assert_equal(wanted, dt)
2401+
2402+
23802403
if sys.version_info >= (2, 6):
23812404

23822405
if sys.version_info[:2] == (2, 6):

0 commit comments

Comments
 (0)