8000 BUG: Fix crash in repr of void subclasses · eric-wieser/numpy@0ec2a4f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ec2a4f

Browse files
committed
BUG: Fix crash in repr of void subclasses
Fixes numpy#12206, which was a regression introduced by numpy#10602 Frankly I'd consider the results expected by `test_void_subclass_unsized` and `test_void_subclass_sized` undesirable, but they match the behavior in 1.12.x
1 parent 25a37a9 commit 0ec2a4f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

numpy/core/_dtype.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ def _scalar_str(dtype, short):
138138
else:
139139
return "'%sU%d'" % (byteorder, dtype.itemsize / 4)
140140

141-
elif dtype.type == np.void:
141+
# unlike the other types, subclasses of void are preserved - but
142+
# historically the repr does not actually reveal the subclass
143+
elif issubclass(dtype.type, np.void):
142144
if _isunsized(dtype):
143145
return "'V'"
144146
else:

numpy/core/tests/test_dtype.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,25 @@ def test_empty_string_to_object(self):
620620
# Pull request #4722
621621
np.array(["", ""]).astype(object)
622622

623+
def test_void_subclass_unsized(self):
624+
dt = np.dtype(np.record)
625+
assert_equal(repr(dt), "dtype('V')")
626+
assert_equal(str(dt), '|V0')
627+
assert_equal(dt.name, 'record')
628+
629+
def test_void_subclass_sized(self):
630+
dt = np.dtype((np.record, 2))
631+
assert_equal(repr(dt), "dtype('V2')")
632+
assert_equal(str(dt), '|V2')
633+
assert_equal(dt.name, 'record16')
634+
635+
def test_void_subclass_fields(self):
636+
dt = np.dtype((np.record, [('a', '<u2')]))
637+
assert_equal(repr(dt), "dtype((numpy.record, [('a', '<u2')]))")
638+
assert_equal(str(dt), "(numpy.record, [('a', '<u2')])")
639+
assert_equal(dt.name, 'record16')
640+
641+
623642
class TestDtypeAttributeDeletion(object):
624643

625644
def test_dtype_non_writable_attributes_deletion(self):

0 commit comments

Comments
 (0)
0