Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: Add void field at end of dtype.descr to match itemsize #6361

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 2 commits into from
Sep 27, 2015
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
4 changes: 4 additions & 0 deletions numpy/core/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 14 additions & 1 deletion numpy/core/tests/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down