-
-
Notifications
You must be signed in to change notification settings - Fork 11k
ENH: PyArray_FromInterface checks descr if typestr is np.void #5519
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2069,6 +2069,51 @@ PyArray_FromStructInterface(PyObject *input) | |
return NULL; | ||
} | ||
|
||
/* | ||
* Checks if the object in descr is the default 'descr' member for the | ||
* __array_interface__ dictionary with 'typestr' member typestr. | ||
*/ | ||
NPY_NO_EXPORT int | ||
_is_default_descr(PyObject *descr, PyObject *typestr) { | ||
PyObject *tuple, *name, *typestr2; | ||
#if defined(NPY_PY3K) | ||
PyObject *tmp = NULL; | ||
#endif | ||
int ret = 0; | ||
|
||
if (!PyList_Check(descr) || PyList_GET_SIZE(descr) != 1) { | ||
return 0; | ||
} | ||
tuple = PyList_GET_ITEM(descr, 0); | ||
if (!(PyTuple_Check(tuple) && PyTuple_GET_SIZE(tuple) == 2)) { | ||
return 0; | ||
} | ||
name = PyTuple_GET_ITEM(tuple, 0); | ||
if (!(PyUString_Check(name) && PyUString_GET_SIZE(name) == 0)) { | ||
return 0; | ||
} | ||
typestr2 = PyTuple_GET_ITEM(tuple, 1); | ||
#if defined(NPY_PY3K) | ||
/* Allow unicode type strings */ | ||
if (PyUnicode_Check(typestr2)) { | ||
tmp = PyUnicode_AsASCIIString(typestr2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
if (tmp == NULL) { | ||
return 0; | ||
} | ||
typestr2 = tmp; | ||
} | ||
#endif | ||
if (PyBytes_Check(typestr2) && | ||
PyObject_RichCompareBool(typestr, typestr2, Py_EQ)) { | ||
ret = 1; | ||
} | ||
#if defined(NPY_PY3K) | ||
Py_XDECREF(tmp); | ||
#endif | ||
|
||
return ret; | ||
} | ||
|
||
#define PyIntOrLong_Check(obj) (PyInt_Check(obj) || PyLong_Check(obj)) | ||
|
||
/*NUMPY_API*/ | ||
|
@@ -2087,11 +2132,6 @@ PyArray_FromInterface(PyObject *origin) | |
npy_intp dims[NPY_MAXDIMS], strides[NPY_MAXDIMS]; | ||
int dataflags = NPY_ARRAY_BEHAVED; | ||
|
||
/* Get the typestring -- ignore array_descr */ | ||
/* Get the shape */ | ||
/* Get the memory from __array_data__ and __array_offset__ */ | ||
/* Get the strides */ | ||
|
||
iface = PyArray_GetAttrString_SuppressException(origin, | ||
"__array_interface__"); | ||
if (iface == NULL) { | ||
|
@@ -2135,6 +2175,22 @@ PyArray_FromInterface(PyObject *origin) | |
goto fail; | ||
} | ||
|
||
/* | ||
* If the dtype is NPY_VOID, see if there is extra information in | ||
* the 'descr' attribute. | ||
*/ | ||
if (dtype->type_num == NPY_VOID) { | ||
PyObject *descr = PyDict_GetItemString(iface, "descr"); | ||
PyArray_Descr *new_dtype = NULL; | ||
|
||
if (descr != NULL && !_is_default_descr(descr, attr) && | ||
PyArray_DescrConverter2(descr, &new_dtype) == NPY_SUCCEED && | ||
new_dtype != NULL) { | ||
Py_DECREF(dtype); | ||
dtype = new_dtype; | ||
} | ||
} | ||
|
||
/* Get shape tuple from interface specification */ | ||
attr = PyDict_GetItemString(iface, "shape"); | ||
if (attr == NULL) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ;)