Skip to content

Commit 36fc78c

Browse files
committed
Fixes numpy#2215 Loading numpy with aligned dtype
1 parent a9e9343 commit 36fc78c

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎numpy/lib/format.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
import numpy
153153
import sys
154154
import io
155+
import re
155156
import warnings
156157
from numpy.lib.utils import safe_eval
157158
from numpy.compat import asbytes, asstr, isfileobj, long, basestring
@@ -496,6 +497,10 @@ def _read_array_header(fp, version):
496497
# "descr" : dtype.descr
497498
header = _filter_header(header)
498499
try:
500+
old_header = header
501+
# replace paddings like ('', '|V4')
502+
header = re.sub(r"\s*,*\s*\(\s*''\s*,\s*'\|*V\d'\s*\)", '', header)
503+
aligned = header != old_header
499504
d = safe_eval(header)
500505
except SyntaxError as e:
501506
msg = "Cannot parse header: %r\nException: %r"
@@ -517,7 +522,7 @@ def _read_array_header(fp, version):
517522
msg = "fortran_order is not a valid bool: %r"
518523
raise ValueError(msg % (d['fortran_order'],))
519524
try:
520-
dtype = numpy.dtype(d['descr'])
525+
dtype = numpy.dtype(d['descr'], align=aligned)
521526
except TypeError as e:
522527
msg = "descr is not a valid dtype descriptor: %r"
523528
raise ValueError(msg % (d['descr'],))

‎numpy/lib/tests/test_format.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,15 @@ def test_compressed_roundtrip():
523523
assert_array_equal(arr, arr1)
524524

525525

526+
def test_load_aligned_dtype():
527+
dt1 = np.dtype('i1, i4, i1', align=True)
528+
arr = np.zeros(1, dt1)
529+
npz_file = os.path.join(tempdir, 'aligned.npz')
530+
np.savez(npz_file, arr=arr)
531+
arr1 = np.load(npz_file)['arr']
532+
assert_array_equal(arr, arr1)
533+
534+
526535
def test_python2_python3_interoperability():
527536
if sys.version_info[0] >= 3:
528537
fname = 'win64python2.npy'

0 commit comments

Comments
 (0)