Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix unpickling an empty ndarray with a non-zero dimension (#21067) #21140

Merged
merged 1 commit into from Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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