summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2017-02-26 16:23:03 -0700
committerCharles Harris <charlesr.harris@gmail.com>2017-02-28 18:10:19 -0700
commit542390d680d5779af1028591d1885af0ff6d25ed (patch)
tree6dd23bc6070538a69d7d564723828a1a6ae1502e
parent9a8e8298d0bac90959ca88a26ab9657603905264 (diff)
downloadnumpy-542390d680d5779af1028591d1885af0ff6d25ed.tar.gz
BUG: Fix deepcopy regression for empty arrays.
Deepcopy of empty arrays was failing because the nditer was constructed without the NPY_ITER_ZEROSIZE_OK flag. Closes #8536.
-rw-r--r--numpy/core/src/multiarray/methods.c57
-rw-r--r--numpy/core/tests/test_regression.py7
2 files changed, 36 insertions, 28 deletions
diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c
index f2e1d87ad..80d4aebeb 100644
--- a/numpy/core/src/multiarray/methods.c
+++ b/numpy/core/src/multiarray/methods.c
@@ -1441,41 +1441,42 @@ array_deepcopy(PyArrayObject *self, PyObject *args)
Py_DECREF(copied_array);
return NULL;
}
- iter = (NpyIter *)NpyIter_New(copied_array,
- (NPY_ITER_READWRITE |
- NPY_ITER_EXTERNAL_LOOP |
- NPY_ITER_REFS_OK),
- NPY_KEEPORDER,
- NPY_NO_CASTING,
- NULL);
+ iter = NpyIter_New(copied_array,
+ NPY_ITER_READWRITE |
+ NPY_ITER_EXTERNAL_LOOP |
+ NPY_ITER_REFS_OK |
+ NPY_ITER_ZEROSIZE_OK,
+ NPY_KEEPORDER, NPY_NO_CASTING,
+ NULL);
if (iter == NULL) {
Py_DECREF(deepcopy);
Py_DECREF(copied_array);
return NULL;
}
- iternext = NpyIter_GetIterNext(iter, NULL);
- if (iternext == NULL) {
- NpyIter_Deallocate(iter);
- Py_DECREF(deepcopy);
- Py_DECREF(copied_array);
- return NULL;
- }
-
- dataptr = NpyIter_GetDataPtrArray(iter);
- strideptr = NpyIter_GetInnerStrideArray(iter);
- innersizeptr = NpyIter_GetInnerLoopSizePtr(iter);
-
- do {
- data = *dataptr;
- stride = *strideptr;
- count = *innersizeptr;
- while (count--) {
- _deepcopy_call(data, data, PyArray_DESCR(copied_array),
- deepcopy, visit);
- data += stride;
+ if (NpyIter_GetIterSize(iter) != 0) {
+ iternext = NpyIter_GetIterNext(iter, NULL);
+ if (iternext == NULL) {
+ NpyIter_Deallocate(iter);
+ Py_DECREF(deepcopy);
+ Py_DECREF(copied_array);
+ return NULL;
}
- } while (iternext(iter));
+ dataptr = NpyIter_GetDataPtrArray(iter);
+ strideptr = NpyIter_GetInnerStrideArray(iter);
+ innersizeptr = NpyIter_GetInnerLoopSizePtr(iter);
+
+ do {
+ data = *dataptr;
+ stride = *strideptr;
+ count = *innersizeptr;
+ while (count--) {
+ _deepcopy_call(data, data, PyArray_DESCR(copied_array),
+ deepcopy, visit);
+ data += stride;
+ }
+ } while (iternext(iter));
+ }
NpyIter_Deallocate(iter);
Py_DECREF(deepcopy);
}
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
index 721a025de..4875f852a 100644
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -2094,6 +2094,13 @@ class TestRegression(TestCase):
# Check the references hold for the copied objects.
self.assertTrue(arr_cp[0, 1] is arr_cp[1, 1])
+ def test_deepcopy_empty_object_array(self):
+ # Ticket #8536.
+ # Deepcopy should succeed
+ a = np.array([], dtype=object)
+ b = copy.deepcopy(a)
+ assert_(a.shape == b.shape)
+
def test_bool_subscript_crash(self):
# gh-4494
c = np.rec.array([(1, 2, 3), (4, 5, 6)])