Skip to content

Commit

Permalink
Merge pull request #21140 from charris/backport-21067
Browse files Browse the repository at this point in the history
BUG: Fix unpickling an empty ndarray with a none-zero dimension (#21067)
  • Loading branch information
charris committed Mar 3, 2022
2 parents 09a3791 + 03d842e commit 954c9b5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion numpy/core/src/multiarray/methods.c
Expand Up @@ -2175,7 +2175,7 @@ array_setstate(PyArrayObject *self, PyObject *args)
Py_DECREF(typecode);
}
else {
memcpy(PyArray_DATA(self), datastr, num);
memcpy(PyArray_DATA(self), datastr, PyArray_NBYTES(self));
}
PyArray_ENABLEFLAGS(self, NPY_ARRAY_OWNDATA);
fa->base = NULL;
Expand Down
9 changes: 9 additions & 0 deletions numpy/core/tests/test_multiarray.py
Expand Up @@ -1640,6 +1640,15 @@ def test_pickle(self):

assert_equal(zs.dtype, zs2.dtype)

def test_pickle_empty(self):
"""Checking if an empty array pickled and un-pickled will not cause a
segmentation fault"""
arr = np.array([]).reshape(999999, 0)
pk_dmp = pickle.dumps(arr)
pk_load = pickle.loads(pk_dmp)

assert pk_load.size == 0

@pytest.mark.skipif(pickle.HIGHEST_PROTOCOL < 5,
reason="requires pickle protocol 5")
def test_pickle_with_buffercallback(self):
Expand Down

0 comments on commit 954c9b5

Please sign in to comment.