8000 Merge pull request #5618 from johntyree/arrfill_ulonglong · numpy/numpy@a29f50e · GitHub
[go: up one dir, main page]

Skip to content

Commit a29f50e

Browse files
committed
Merge pull request #5618 from johntyree/arrfill_ulonglong
BUG: Fixes ndarray.fill to accept maximum uint64 value
2 parents 96abd32 + 11dfe93 commit a29f50e

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

numpy/core/src/multiarray/convert.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,16 +350,33 @@ PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj)
350350
}
351351
/* Python integer */
352352
else if (PyLong_Check(obj) || PyInt_Check(obj)) {
353-
npy_longlong v = PyLong_AsLongLong(obj);
354-
if (v == -1 && PyErr_Occurred()) {
355-
return -1;
353+
/* Try long long before unsigned long long */
354+
npy_longlong ll_v = PyLong_AsLongLong(obj);
355+
if (ll_v == -1 && PyErr_Occurred()) {
356+
/* Long long failed, try unsigned long long */
357+
npy_ulonglong ull_v;
358+
PyErr_Clear();
359+
ull_v = PyLong_AsUnsignedLongLong(obj);
360+
if (ull_v == (unsigned long long)-1 && PyErr_Occurred()) {
361+
return -1;
362+
}
363+
value = (char *)value_buffer;
364+
*(npy_ulonglong *)value = ull_v;
365+
366+
dtype = PyArray_DescrFromType(NPY_ULONGLONG);
367+
if (dtype == NULL) {
368+
return -1;
369+
}
356370
}
357-
value = (char *)value_buffer;
358-
*(npy_longlong *)value = v;
371+
else {
372+
/* Long long succeeded */
373+
value = (char *)value_buffer;
374+
*(npy_longlong *)value = ll_v;
359375

360-
dtyp 8000 e = PyArray_DescrFromType(NPY_LONGLONG);
361-
if (dtype == NULL) {
362-
return -1;
376+
dtype = PyArray_DescrFromType(NPY_LONGLONG);
377+
if (dtype == NULL) {
378+
return -1;
379+
}
363380
}
364381
}
365382
/* Python float */

numpy/core/tests/test_multiarray.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ def test_fill(self):
194194
y[...] = 1
195195
assert_equal(x, y)
196196

197+
def test_fill_max_uint64(self):
198+
x = empty((3, 2, 1), dtype=uint64)
199+
y = empty((3, 2, 1), dtype=uint64)
200+
value = 2**64 - 1
201+
y[...] = value
202+
x.fill(value)
203+
assert_array_equal(x, y)
204+
197205
def test_fill_struct_array(self):
198206
# Filling from a scalar
199207
x = array([(0, 0.0), (1, 1.0)], dtype='i4,f8')

0 commit comments

Comments
 (0)
0