summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik M. Bray <erik.bray@lri.fr>2016-05-06 13:20:32 +0200
committerErik M. Bray <erik.bray@lri.fr>2016-05-31 16:32:32 +0200
commitdb7c0b74d3d85521769042ff91309baeb57aae0d (patch)
tree5bfc363f96ad2f0e51d6e945ad78c801992b7400
parent7a50050386cf05b94c16ba5e8676a68a23cbd6b8 (diff)
downloadnumpy-db7c0b74d3d85521769042ff91309baeb57aae0d.tar.gz
BUG: Change ndarray.__new__ to allow zero-width data types, and in particular without converting zero-width strings to a one-width string dtype.
ndarray.__new__, being relatively low-level, should not be mangling the requested dtype when creating an array. See #6430
-rw-r--r--numpy/core/src/multiarray/arrayobject.c24
-rw-r--r--numpy/core/tests/test_multiarray.py5
2 files changed, 15 insertions, 14 deletions
diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c
index 277c6517c..c21bcaecf 100644
--- a/numpy/core/src/multiarray/arrayobject.c
+++ b/numpy/core/src/multiarray/arrayobject.c
@@ -1652,11 +1652,6 @@ array_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
}
itemsize = descr->elsize;
- if (itemsize == 0) {
- PyErr_SetString(PyExc_ValueError,
- "data-type with unspecified variable length");
- goto fail;
- }
if (strides.ptr != NULL) {
npy_intp nb, off;
@@ -1690,10 +1685,11 @@ array_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
if (buffer.ptr == NULL) {
ret = (PyArrayObject *)
- PyArray_NewFromDescr(subtype, descr,
- (int)dims.len,
- dims.ptr,
- strides.ptr, NULL, is_f_order, NULL);
+ PyArray_NewFromDescr_int(subtype, descr,
+ (int)dims.len,
+ dims.ptr,
+ strides.ptr, NULL, is_f_order, NULL,
+ 0, 1);
if (ret == NULL) {
descr = NULL;
goto fail;
@@ -1726,11 +1722,11 @@ array_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
buffer.flags |= NPY_ARRAY_F_CONTIGUOUS;
}
ret = (PyArrayObject *)\
- PyArray_NewFromDescr(subtype, descr,
- dims.len, dims.ptr,
- strides.ptr,
- offset + (char *)buffer.ptr,
- buffer.flags, NULL);
+ PyArray_NewFromDescr_int(subtype, descr,
+ dims.len, dims.ptr,
+ strides.ptr,
+ offset + (char *)buffer.ptr,
+ buffer.flags, NULL, 0, 1);
if (ret == NULL) {
descr = NULL;
goto fail;
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index 66f6e23cb..9d7c53e67 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -947,6 +947,11 @@ class TestStructured(TestCase):
assert_equal(x['S'], [b'', b'', b'', b''])
assert_equal(x['I'], [0, 0, 0, 0])
+ # Allow zero-width dtypes in ndarray constructor
+ y = np.ndarray(4, dtype=x['S'].dtype)
+ assert_equal(y.itemsize, 0)
+ assert_equal(x['S'], y)
+
# More tests for indexing an array with zero-width fields
assert_equal(np.zeros(4, dtype=[('a', 'S0,S0'),
('b', 'u1')])['a'].itemsize, 0)