diff options
-rw-r--r-- | numpy/core/src/multiarray/convert.c | 33 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 8 | ||||
-rw-r--r-- | numpy/lib/arraysetops.py | 7 | ||||
-rw-r--r-- | numpy/lib/stride_tricks.py | 6 | ||||
-rw-r--r-- | numpy/lib/tests/test_stride_tricks.py | 10 |
5 files changed, 50 insertions, 14 deletions
diff --git a/numpy/core/src/multiarray/convert.c b/numpy/core/src/multiarray/convert.c index b610343cc..e1c2cb8d9 100644 --- a/numpy/core/src/multiarray/convert.c +++ b/numpy/core/src/multiarray/convert.c @@ -350,16 +350,33 @@ PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj) } /* Python integer */ else if (PyLong_Check(obj) || PyInt_Check(obj)) { - npy_longlong v = PyLong_AsLongLong(obj); - if (v == -1 && PyErr_Occurred()) { - return -1; + /* Try long long before unsigned long long */ + npy_longlong ll_v = PyLong_AsLongLong(obj); + if (ll_v == -1 && PyErr_Occurred()) { + /* Long long failed, try unsigned long long */ + npy_ulonglong ull_v; + PyErr_Clear(); + ull_v = PyLong_AsUnsignedLongLong(obj); + if (ull_v == (unsigned long long)-1 && PyErr_Occurred()) { + return -1; + } + value = (char *)value_buffer; + *(npy_ulonglong *)value = ull_v; + + dtype = PyArray_DescrFromType(NPY_ULONGLONG); + if (dtype == NULL) { + return -1; + } } - value = (char *)value_buffer; - *(npy_longlong *)value = v; + else { + /* Long long succeeded */ + value = (char *)value_buffer; + *(npy_longlong *)value = ll_v; - dtype = PyArray_DescrFromType(NPY_LONGLONG); - if (dtype == NULL) { - return -1; + dtype = PyArray_DescrFromType(NPY_LONGLONG); + if (dtype == NULL) { + return -1; + } } } /* Python float */ diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 1694cea61..0c13cff6a 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -194,6 +194,14 @@ class TestAttributes(TestCase): y[...] = 1 assert_equal(x, y) + def test_fill_max_uint64(self): + x = empty((3, 2, 1), dtype=uint64) + y = empty((3, 2, 1), dtype=uint64) + value = 2**64 - 1 + y[...] = value + x.fill(value) + assert_array_equal(x, y) + def test_fill_struct_array(self): # Filling from a scalar x = array([(0, 0.0), (1, 1.0)], dtype='i4,f8') diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index cb24eb24e..7776d7e76 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -97,10 +97,11 @@ def unique(ar, return_index=False, return_inverse=False, return_counts=False): """ Find the unique elements of an array. - Returns the sorted unique elements of an array. There are two optional + Returns the sorted unique elements of an array. There are three optional outputs in addition to the unique elements: the indices of the input array - that give the unique values, and the indices of the unique array that - reconstruct the input array. + that give the unique values, the indices of the unique array that + reconstruct the input array, and the number of times each unique value + comes up in the input array. Parameters ---------- diff --git a/numpy/lib/stride_tricks.py b/numpy/lib/stride_tricks.py index 0f46ed335..a5f247abf 100644 --- a/numpy/lib/stride_tricks.py +++ b/numpy/lib/stride_tricks.py @@ -60,9 +60,9 @@ def _broadcast_to(array, shape, subok, readonly): if any(size < 0 for size in shape): raise ValueError('all elements of broadcast shape must be non-' 'negative') - broadcast = np.nditer((array,), flags=['multi_index', 'zerosize_ok'], - op_flags=['readonly'], itershape=shape, order='C' - ).itviews[0] + broadcast = np.nditer( + (array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'], + op_flags=['readonly'], itershape=shape, order='C').itviews[0] result = _maybe_view_as_subclass(array, broadcast) if not readonly and array.flags.writeable: result.flags.writeable = True diff --git a/numpy/lib/tests/test_stride_tricks.py b/numpy/lib/tests/test_stride_tricks.py index 0b73109bc..ef483921c 100644 --- a/numpy/lib/tests/test_stride_tricks.py +++ b/numpy/lib/tests/test_stride_tricks.py @@ -364,5 +364,15 @@ def test_writeable(): assert_equal(result.flags.writeable, False) +def test_reference_types(): + input_array = np.array('a', dtype=object) + expected = np.array(['a'] * 3, dtype=object) + actual = broadcast_to(input_array, (3,)) + assert_array_equal(expected, actual) + + actual, _ = broadcast_arrays(input_array, np.ones(3)) + assert_array_equal(expected, actual) + + if __name__ == "__main__": run_module_suite() |