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

ENH: PyArray_FromInterface checks descr if typestr is np.void #5519

Merged
merged 1 commit into from
Mar 12, 2015

Conversation

jaimefrio
Copy link
Member

When the 'typestr' member of the array_interface dictionary defines
a np.void dtype, check the 'descr' member, and if it is a valid dtype
description and it is not the default one, use it to construct the
dtype for the array to return.

This fixes #5081, as as_strided no longer has to worry about changing
the dtype of the return.

@jaimefrio
Copy link
Member Author

There is still a hole in the ability of numpy to fully reconstruct an array, including its dtype, using __array_interface__. Right now any structured dtype is converted by PyArray_FromInterface into an array of type np.void. This PR fixes it for almost all structured dtypes except for one: a structured dtype of a single field, with name the empty string, and dtype np.void. This is hard to produce, as this will not give you that:

>>> dt = np.dtype([('', 'V8')])
dtype([('f0', 'V8')])

but if you really want it, you can get it:

>>> np.dtype({'names': [''], 'formats': ['V8']})
dtype([('', 'V8')])

@jaimefrio
Copy link
Member Author

Added a little logic to as_strided so it can properly handle even that weird corner case.

#if defined(NPY_PY3K)
/* Allow unicode type strings */
if (PyUnicode_Check(typestr2)) {
tmp = PyUnicode_AsASCIIString(typestr2);
Copy link
Member

Choose a reason for hiding this comment

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

Looks like tmp can be NULL, assuming that the string cannot be converted to ascii. I'm not sure what the policy for this codec is.

@charris
Copy link
Member

charris commented Feb 11, 2015

Looks like there may be some conflicts with #5371, so lets get that in first. You are welcome to merge it :0

@charris
Copy link
Member

charris commented Feb 27, 2015

@jaimefrio Needs rebase.

@jaimefrio
Copy link
Member Author

Rebased it is. Still need to figure how to handle that potential failure in the ASCII conversion...

When the 'typestr' member of the __array_interface__ dictionary defines
a np.void dtype, check the 'descr' member, and if it is a valid dtype
description and it is not the default one, use it to construct the
dtype for the array to return.

This fixes numpy#5081, as as_strided no longer has to worry about changing
the dtype of the return.
@jaimefrio
Copy link
Member Author

I think I have now taken care of the possible error: if we cannot convert to ASCII, then it isn't the default descr, so we return 0. tmp is set to NULL at the beginning, and Py_XDECREF'ed at the end just to be sure.

return 0;
}
tuple = PyList_GET_ITEM(descr, 0);
if (!(PyTuple_Check(tuple) && PyTuple_GET_SIZE(tuple) == 2)) {
Copy link
Member

Choose a reason for hiding this comment

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

I like the way this condition is expressed better than the one above ;)

charris added a commit that referenced this pull request Mar 12, 2015
ENH: PyArray_FromInterface checks descr if typestr is np.void
@charris charris merged commit e84454a into numpy:master Mar 12, 2015
@charris
Copy link
Member

charris commented Mar 12, 2015

LGTM, thanks Jaime.

@jaimefrio jaimefrio deleted the array_interface_dtype branch March 12, 2015 22:44
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.

BUG np.broadcast_arrays does not work on void arrays with object fields
3 participants