diff options
| author | Charles Harris <charlesr.harris@gmail.com> | 2018-02-06 14:25:00 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-02-06 14:25:00 -0700 |
| commit | d328fdcbc0149fe3f960a5d9b66579af9f0f3948 (patch) | |
| tree | 5bc5fc20c2a2c370a235e6edb5c15ef7bec55a34 /numpy | |
| parent | c83653bfa894fa0eba20903c5a79df7ef444e859 (diff) | |
| parent | a2592e092a076bb0c87f5588dbffee4a6844dc08 (diff) | |
| download | numpy-d328fdcbc0149fe3f960a5d9b66579af9f0f3948.tar.gz | |
Merge pull request #10529 from eric-wieser/fix-pep3118-error
BUG: Provide a better error message for out-of-order fields
Diffstat (limited to 'numpy')
| -rw-r--r-- | numpy/core/src/multiarray/buffer.c | 16 | ||||
| -rw-r--r-- | numpy/core/tests/test_multiarray.py | 13 |
2 files changed, 24 insertions, 5 deletions
diff --git a/numpy/core/src/multiarray/buffer.c b/numpy/core/src/multiarray/buffer.c index e76d406de..f892cf6cd 100644 --- a/numpy/core/src/multiarray/buffer.c +++ b/numpy/core/src/multiarray/buffer.c @@ -12,6 +12,7 @@ #include "npy_pycompat.h" #include "buffer.h" +#include "common.h" #include "numpyos.h" #include "arrayobject.h" @@ -243,14 +244,19 @@ _buffer_format_string(PyArray_Descr *descr, _tmp_string_t *str, child = (PyArray_Descr*)PyTuple_GetItem(item, 0); offset_obj = PyTuple_GetItem(item, 1); - new_offset = base_offset + PyInt_AsLong(offset_obj); + new_offset = PyInt_AsLong(offset_obj); + if (error_converting(new_offset)) { + return -1; + } + new_offset += base_offset; /* Insert padding manually */ if (*offset > new_offset) { - PyErr_SetString(PyExc_RuntimeError, - "This should never happen: Invalid offset in " - "buffer format string generation. Please " - "report a bug to the Numpy developers."); + PyErr_SetString( + PyExc_ValueError, + "dtypes with overlapping or out-of-order fields are not " + "representable as buffers. Consider reordering the fields." + ); return -1; } while (*offset < new_offset) { diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 0fbaacb03..d65bb17e6 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -6461,6 +6461,19 @@ class TestNewBufferProtocol(object): shape, strides = get_buffer_info(arr, ['C_CONTIGUOUS']) assert_(strides[-1] == 8) + def test_out_of_order_fields(self): + dt = np.dtype(dict( + formats=['<i4', '<i4'], + names=['one', 'two'], + offsets=[4, 0], + itemsize=8 + )) + + # overlapping fields cannot be represented by PEP3118 + arr = np.empty(1, dt) + with assert_raises(ValueError): + memoryview(arr) + class TestArrayAttributeDeletion(object): |
