8000 BUG: initialize object array of array on resize and zeros by juliantaylor · Pull Request #4889 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: initialize object array of array on resize and zeros #4889

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

Merged
merged 1 commit into from
Jul 23, 2014
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions numpy/core/src/multiarray/refcount.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,12 @@ _fillobject(char *optr, PyObject *obj, PyArray_Descr *dtype)
}
}
else {
Py_XINCREF(obj);
NPY_COPY_PYOBJECT_PTR(optr, &obj);
npy_intp i;
for (i = 0; i < dtype->elsize / sizeof(obj); i++) {
Py_XINCREF(obj);
NPY_COPY_PYOBJECT_PTR(optr, &obj);
optr += sizeof(obj);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, we could possible make this saver by performing a PyObject_Copy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

urg there is no PyObject_Copy capi?

}
return;
}
}
8 changes: 6 additions & 2 deletions numpy/core/src/multiarray/shape.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,12 @@ _putzero(char *optr, PyObject *zero, PyArray_Descr *dtype)
}
}
else {
Py_INCREF(zero);
NPY_COPY_PYOBJECT_PTR(optr, &zero);
npy_intp i;
for (i = 0; i < dtype->elsize / sizeof(zero); i++) {
Py_INCREF(zero);
NPY_COPY_PYOBJECT_PTR(optr, &zero);
optr += sizeof(zero);
}
}
return;
}
Expand Down
12 changes: 12 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ def test_zeros_obj(self):
assert_array_equal(d, [0] * 13)
assert_equal(np.count_nonzero(d), 0)

def test_zeros_obj_obj(self):
d = zeros(10, dtype=[('k', object, 2)])
assert_array_equal(d['k'], 0)

def test_sequence_non_homogenous(self):
assert_equal(np.array([4, 2**80]).dtype, np.object)
assert_equal(np.array([4, 2**80, 4]).dtype, np.object)
Expand Down Expand Up @@ -2784,6 +2788,14 @@ def test_zeros_appended(self):
assert_array_equal(x[0], np.eye(3))
assert_array_equal(x[1], np.zeros((3, 3)))

def test_obj_obj(self):
# check memory is initialized on resize, gh-4857
a = ones(10, dtype=[('k', object, 2)])
a.resize(15,)
assert_equal(a.shape, (15,))
assert_array_equal(a['k'][-5:], 0)
assert_array_equal(a['k'][:-5], 1)


class TestRecord(TestCase):
def test_field_rename(self):
Expand Down
0