diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2017-04-22 15:06:44 +0100 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2017-10-01 16:46:20 -0700 |
commit | 49b16783c7263c2d8f298411594d71cbd48d9432 (patch) | |
tree | 3fb8b24e34413f0cd385190b0c66a2bbff570196 | |
parent | 0e9e205c3b1d90dc6782bd21bb428d17aded0e90 (diff) | |
download | numpy-49b16783c7263c2d8f298411594d71cbd48d9432.tar.gz |
BUG: Allow empty strings to be viewed as themselves
-rw-r--r-- | numpy/core/src/multiarray/getset.c | 27 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 10 |
2 files changed, 22 insertions, 15 deletions
diff --git a/numpy/core/src/multiarray/getset.c b/numpy/core/src/multiarray/getset.c index 77d9b8c66..d58071239 100644 --- a/numpy/core/src/multiarray/getset.c +++ b/numpy/core/src/multiarray/getset.c @@ -469,22 +469,18 @@ array_descr_set(PyArrayObject *self, PyObject *arg) Py_DECREF(safe); } - if (newtype->elsize == 0) { - /* Allow a void view */ - if (newtype->type_num == NPY_VOID) { - PyArray_DESCR_REPLACE(newtype); - if (newtype == NULL) { - return -1; - } - newtype->elsize = PyArray_DESCR(self)->elsize; - } - /* But no other flexible types */ - else { - PyErr_SetString(PyExc_TypeError, - "data-type must not be 0-sized"); - Py_DECREF(newtype); + /* + * Treat V0 as resizable void - unless the destination is already V0, then + * don't allow np.void to be duplicated + */ + if (newtype->type_num == NPY_VOID && + newtype->elsize == 0 && + PyArray_DESCR(self)->elsize != 0) { + PyArray_DESCR_REPLACE(newtype); + if (newtype == NULL) { return -1; } + newtype->elsize = PyArray_DESCR(self)->elsize; } @@ -532,7 +528,8 @@ array_descr_set(PyArrayObject *self, PyObject *arg) if (newtype->elsize < PyArray_DESCR(self)->elsize) { /* if it is compatible, increase the size of the relevant axis */ - if (PyArray_DESCR(self)->elsize % newtype->elsize != 0) { + if (newtype->elsize == 0 || + PyArray_DESCR(self)->elsize % newtype->elsize != 0) { PyErr_SetString(PyExc_ValueError, "When changing to a smaller dtype, its size must be a " "divisor of the size of original dtype"); diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 8ab2c97f5..8bb91619e 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -1252,6 +1252,16 @@ class TestZeroSizeFlexible(object): zs.resize(25) zs.resize((10, 10)) + def test_view(self): + for dt in [bytes, np.void, unicode]: + zs = self._zeros(10, dt) + + # viewing as itself should be allowed + assert_equal(zs.view(dt).dtype, np.dtype(dt)) + + # viewing as any non-empty type gives an empty result + assert_equal(zs.view((dt, 1)).shape, (0,)) + class TestMethods(object): def test_compress(self): |