8000 BUG: non-integers can end up in dtype offsets by ahaldane · Pull Request #8080 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: non-integers can end up in dtype offsets #8080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
BUG: non-integers can end up in dtype offsets
Fix is to convert offsets to python ints at dtype creation.
Fixes #8059
  • Loading branch information
ahaldane committed Sep 22, 2016
commit 7efef9dca97304e048038193fdf1c69b56ecc10a
1 change: 1 addition & 0 deletions numpy/core/src/multiarray/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ _unpack_field(PyObject *value, PyArray_Descr **descr, npy_intp *offset)
*offset = PyLong_AsSsize_t(off);
}
else {
PyErr_SetString(PyExc_IndexError, "can't convert offset");
return -1;
}

Expand Down
17 changes: 14 additions & 3 deletions numpy/core/src/multiarray/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1071,8 +1071,19 @@ _convert_from_dict(PyObject *obj, int align)
if (!off) {
goto fail;
}
offset = PyInt_AsLong(off);
PyTuple_SET_ITEM(tup, 1, off);
offset = PyArray_PyIntAsInt(off);
if (offset == -1 && PyErr_Occurred()) {
Py_DECREF(off);
goto fail;
}
Py_DECREF(off);
if (offset < 0) {
PyErr_Format(PyExc_ValueError, "offset %d cannot be negative",
(int)offset);
goto fail;
}

PyTuple_SET_ITEM(tup, 1, PyInt_FromLong(offset));
/* Flag whether the fields are specified out of order */
if (offset < totalsize) {
has_out_of_order_fields = 1;
Expand Down Expand Up @@ -1186,7 +1197,7 @@ _convert_from_dict(PyObject *obj, int align)
if (tmp == NULL) {
PyErr_Clear();
} else {
itemsize = (int)PyInt_AsLong(tmp);
itemsize = (int)PyArray_PyIntAsInt(tmp);
if (itemsize == -1 && PyErr_Occurred()) {
Py_DECREF(new);
return NULL;
Expand Down
15 changes: 15 additions & 0 deletions numpy/core/tests/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,21 @@ def test_bool_commastring(self):
for n in d.names:
assert_equal(d.fields[n][0], np.dtype('?'))

def test_nonint_offsets(self):
# gh-8059
def make_dtype(off):
return np.dtype({'names': ['A'], 'formats': ['i4'],
'offsets': [off]})

assert_raises(TypeError, make_dtype, 'ASD')
assert_raises(OverflowError, make_dtype, 2**70)
assert_raises(TypeError, make_dtype, 2.3)
assert_raises(ValueError, make_dtype, -10)

# no errors here:
dt = make_dtype(np.uint32(0))
np.zeros(1, dtype=dt)[0].item()


class TestSubarray(TestCase):
def test_single_subarray(self):
Expand Down
0