8000 Merge pull request #13561 from charris/backport-13433 · numpy/numpy@28e484d · GitHub
[go: up one dir, main page]

Skip to content

Commit 28e484d

Browse files
authored
Merge pull request #13561 from charris/backport-13433
BUG: Handle subarrays in descr_to_dtype
2 parents 04801db + 3651a8c commit 28e484d

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

numpy/lib/format.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,19 @@ def dtype_to_descr(dtype):
262262
def descr_to_dtype(descr):
263263
'''
264264
descr may be stored as dtype.descr, which is a list of
265-
(name, format, [shape]) tuples. Offsets are not explicitly saved, rather
266-
empty fields with name,format == '', '|Vn' are added as padding.
265+
(name, format, [shape]) tuples where format may be a str or a tuple.
266+
Offsets are not explicitly saved, rather empty fields with
267+
name, format == '', '|Vn' are added as padding.
267268
268269
This function reverses the process, eliminating the empty padding fields.
269270
'''
270-
if isinstance(descr, (str, dict)):
271+
if isinstance(descr, str):
271272
# No padding removal needed
272273
return numpy.dtype(descr)
273-
274+
elif isinstance(descr, tuple):
275+
# subtype, will always have a shape descr[1]
276+
dt = descr_to_dtype(descr[0])
277+
return numpy.dtype((dt, descr[1]))
274278
fields = []
275279
offset = 0
276280
for field in descr:

numpy/lib/tests/test_format.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ def teardown_module():
411411
np.array(NbufferT, dtype=np.dtype(Ndescr).newbyteorder('<')),
412412
np.array(PbufferT, dtype=np.dtype(Pdescr).newbyteorder('>')),
413413
np.array(NbufferT, dtype=np.dtype(Ndescr).newbyteorder('>')),
414+
np.zeros(1, dtype=[('c', ('<f8', (5,)), (2,))])
414415
]
415416

416417

@@ -628,6 +629,61 @@ def test_pickle_disallow():
628629
assert_raises(ValueError, np.save, path, np.array([None], dtype=object),
629630
allow_pickle=False)
630631

632+
@pytest.mark.parametrize('dt', [
633+
np.dtype(np.dtype([('a', np.int8),
634+
('b', np.int16),
635+
('c', np.int32),
636+
], align=True),
637+
(3,)),
638+
np.dtype([('x', np.dtype({'names':['a','b'],
639+
'formats':['i1','i1'],
640+
'offsets':[0,4],
641+
'itemsize':8,
642+
},
643+
(3,)),
644+
(4,),
645+
)]),
646+
np.dtype([('x',
647+
('<f8', (5,)),
648+
(2,),
649+
)]),
650+
np.dtype([('x', np.dtype((
651+
np.dtype((
652+
np.dtype({'names':['a','b'],
653+
'formats':['i1','i1'],
654+
'offsets':[0,4],
655+
'itemsize':8}),
656+
(3,)
657+
)),
658+
(4,)
659+
)))
660+
]),
661+
np.dtype([
662+
('a', np.dtype((
663+
np.dtype((
664+
np.dtype((
665+
np.dtype([
666+
('a', int),
667+
('b', np.dtype({'names':['a','b'],
668+
'formats':['i1','i1'],
669+
'offsets':[0,4],
670+
'itemsize':8})),
671+
]),
672+
(3,),
673+
)),
674+
(4,),
675+
)),
676+
(5,),
677+
)))
678+
]),
679+
])
680+
681+
def test_descr_to_dtype(dt):
682+
dt1 = format.descr_to_dtype(dt.descr)
683+
assert_equal_(dt1, dt)
684+
arr1 = np.zeros(3, dt)
685+
arr2 = roundtrip(arr1)
686+
assert_array_equal(arr1, arr2)
631687

632688
def test_version_2_0():
633689
f = BytesIO()

0 commit comments

Comments
 (0)
0