summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2019-04-11 01:47:58 -0700
committerEric Wieser <wieser.eric@gmail.com>2019-04-11 01:49:06 -0700
commitefdd3f50bd5a1e58b815ad8f82d896f4e72ae2b5 (patch)
treed9a21545bc3a0d1e34695d784c14b1a0b1333c5a
parentd7a73f8c700edcf150d59a570e0173b60f84c7a7 (diff)
downloadnumpy-efdd3f50bd5a1e58b815ad8f82d896f4e72ae2b5.tar.gz
BUG: Fix crash when calling savetxt on a padded array
As a general rule, _every_ use of `.descr` is broken. Fixes #13297
-rw-r--r--numpy/lib/npyio.py2
-rw-r--r--numpy/lib/tests/test_io.py12
2 files changed, 12 insertions, 2 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index d702859fa..beeba1334 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -1379,7 +1379,7 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='',
# Complex dtype -- each field indicates a separate column
else:
- ncol = len(X.dtype.descr)
+ ncol = len(X.dtype.names)
else:
ncol = X.shape[1]
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 7ef25538b..fc0f988cd 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -347,13 +347,23 @@ class TestSaveTxt(object):
assert_raises(ValueError, np.savetxt, c, np.array(1))
assert_raises(ValueError, np.savetxt, c, np.array([[[1], [2]]]))
- def test_record(self):
+ def test_structured(self):
a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
c = BytesIO()
np.savetxt(c, a, fmt='%d')
c.seek(0)
assert_equal(c.readlines(), [b'1 2\n', b'3 4\n'])
+ def test_structured_padded(self):
+ # gh-13297
+ a = np.array([(1, 2, 3),(4, 5, 6)], dtype=[
+ ('foo', 'i4'), ('bar', 'i4'), ('baz', 'i4')
+ ])
+ c = BytesIO()
+ c.seek(0)
+ np.savetxt(c, a[['foo', 'baz']])
+ assert_equal(c.readlines(), [b'1 3\n', b'4 6\n'])
+
@pytest.mark.skipif(Path is None, reason="No pathlib.Path")
def test_multifield_view(self):
a = np.ones(1, dtype=[('x', 'i4'), ('y', 'i4'), ('z', 'f4')])