Skip to content

BUG: make void-scalar getfield/setfield use ndarray methods #5947

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
Jun 11, 2015

Conversation

ahaldane
Copy link
Member

@ahaldane ahaldane commented Jun 6, 2015

This is a continuation of #5642.

This PR removes most of the logic in voidtype_getfield and voidtype_setfield. Instead they simply call the ndarray getfield and setfield. This solves bugs related to void-scalar assignment, #3126 and #3561. It also makes these functions safer since ndarray's get/setfield does safety checks to avoid segfaults involving object arrays.

Additional minor notes:

I changed the calling convention of voidtype_getfield. Previously it accepted (dtype, offset, title) and dropped the (optional) title. Now it expects only (dtype, offset), just like ndarray getfield. This simplifies the voidtype_getfield code.

I also removed code that does byteswapping in both getfield and setfield. After looking at it for a while, I am pretty convinced this code is unnecessary since in both cases we usegentype_generic_method to convert the void scalar to a 0-d ndarray, do the get/setfield, and then convert the returned value to a scalar using PyArray_ToScalar (in PyArray_Return). PyArray_ToScalar already does the byteswap for us. Therefore, any scalars that reach voidtype_getfield are already in NBO and there is no need to swap again.

Also, at the end of voidtype_setfield I effectively do arr[()] = val, and actually create an empty tuple object. Is that really the best way to use setfield, given that arr is possible 0-d?

* checks on the field datatypes and because it broadcasts properly.
* However, as a special case, void-scalar assignment broadcasts
* differently from ndarrays when assigning to an object field: Assignment
* to an ndarray object field brodcasts, but assignment to a void-scalar
Copy link
Member

Choose a reason for hiding this comment

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

brodcasts -> broadcasts.

@charris
Copy link
Member

charris commented Jun 10, 2015

Looks generally good.

This commit modifies voidtype_get/setfield to call ndarray's
get/setfield, which does proper safety checks (for object arrays) and
broadcasts properly.  This solves bugs related to void-scalar
assignment.

Also changed the calling convention of voidtype_getfield. Previously it
accepted a (dtype, offset, title) tuple and dropped title. Now it
expects only (dtype, offset), just like ndarray's getfield.

Fixes numpy#3126.
Fixes numpy#3561.
@ahaldane ahaldane force-pushed the voidscalar_getsetfield branch from 3a8e98e to df959ed Compare June 11, 2015 03:02
@ahaldane
Copy link
Member Author

All right, updated.

@charris
Copy link
Member

charris commented Jun 11, 2015

OK, let's give this a shot. Thanks @ahaldane .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants