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

Conversation

juliantaylor
Copy link
Contributor

np.zeros(2, dtype=[('k', object, 2)]) did only initialize the first
element to zero while the rest stayed None.

In [1]: numpy.zeros(2, dtype=[('k', object, 2)])
Out[1]:
array([([0, None],), ([0, None],)],
      dtype=[('k', 'O', (2,))])

This is a surprising and likely not intended behavior which is fixed
here.
The changed function PyArray_FillObjectArray is only used with None or
zero inputs from numpy, though as its part of the API it could affect
third parties but this is not very likely.

Additionally the memory after the first element was not initialized when
the object was resized.
Closes gh-4857

np.zeros(2, dtype=[('k', object, 2)]) did only initialize the first
element to zero while the rest stayed None.

    In [1]: numpy.zeros(2, dtype=[('k', object, 2)])
    Out[1]:
    array([([0, None],), ([0, None],)],
          dtype=[('k', 'O', (2,))])

This is a surprising and likely not intended behavior which is fixed
here.
The changed function PyArray_FillObjectArray is only used with None or
zero inputs from numpy, though as its part of the API it could affect
third parties but this is not very likely.

Additionally the memory after the first element was not initialized when
the object was resized.
Closes numpygh-4857
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?

@juliantaylor
Copy link
Contributor Author

as this was posted to the mailing list with no objections and I doubt there are many users of the C-API for this type of object I'll put it into master and 1.9

juliantaylor added a commit that referenced this pull request Jul 23, 2014
BUG: initialize object array of array on resize and zeros
@juliantaylor juliantaylor merged commit c96bc5d into numpy:master Jul 23, 2014
juliantaylor added a commit that referenced this pull request Jul 23, 2014
BUG: initialize object array of array on resize and zeros
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Record arrays with fields that contain multiple objects will segfault on array.resize
1 participant
0