From 8863ee9a24e866f79653f57154d431246a0e9079 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Fri, 25 Sep 2015 17:41:51 +0200 Subject: [PATCH 1/2] BUG: Add void field at end of dtype.descr to match itemsize dtype.descr returns void fields to explain the padding part of the dtype. The last void field for the itemsize itself was however not included. Closes gh-6359 --- numpy/core/_internal.py | 4 ++++ numpy/core/tests/test_dtype.py | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index 879f4a224541..81f5be4ada6f 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -121,6 +121,10 @@ def _array_descr(descriptor): offset += field[0].itemsize result.append(tup) + if descriptor.itemsize > offset: + num = descriptor.itemsize - offset + result.append(('', '|V%d' % num)) + return result # Build a new array from the information in a pickle. diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index 496664622e40..29f2ee7bdd6a 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -535,7 +535,7 @@ def test_empty_string_to_object(self): # Pull request #4722 np.array(["", ""]).astype(object) -class TestDtypeAttributeDeletion(object): +class TestDtypeAttributeDeletion(TestCase): def test_dtype_non_writable_attributes_deletion(self): dt = np.dtype(np.double) @@ -552,6 +552,19 @@ def test_dtype_writable_attributes_deletion(self): for s in attr: assert_raises(AttributeError, delattr, dt, s) + +class TestDtypeAttributes(TestCase): + def test_descr_has_trailing_void(self): + # see gh-6359 + dtype = np.dtype({ + 'names': ['A', 'B'], + 'formats': ['f4', 'f4'], + 'offsets': [0, 8], + 'itemsize': 16}) + new_dtype = np.dtype(dtype.descr) + assert_equal(new_dtype.itemsize, 16) + + class TestDtypeAttributes(TestCase): def test_name_builtin(self): From 89ef1193448297d76951f52efb68436be218fb7d Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Sun, 27 Sep 2015 15:54:42 +0200 Subject: [PATCH 2/2] TST: Add test that array interface descr and typestr itemsize match --- numpy/core/tests/test_multiarray.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index a2667172c71f..f9ae7c16e610 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -5390,6 +5390,16 @@ class ArrayLike(object): assert_equal(np.array(ArrayLike()), 1) +def test_array_interface_itemsize(): + # See gh-6361 + my_dtype = np.dtype({'names': ['A', 'B'], 'formats': ['f4', 'f4'], + 'offsets': [0, 8], 'itemsize': 16}) + a = np.ones(10, dtype=my_dtype) + descr_t = np.dtype(a.__array_interface__['descr']) + typestr_t = np.dtype(a.__array_interface__['typestr']) + assert_equal(descr_t.itemsize, typestr_t.itemsize) + + def test_flat_element_deletion(): it = np.ones(3).flat try: