-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
ENH: structured datatype safety checks #5548
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
ENH: structured datatype safety checks
Previously views of structured arrays containing objects were completely disabled. This commit adds more lenient check for whether an object-array view is allowed, and adds similar checks to getfield/setfield Fixes #2346. Fixes #3256. Fixes #2599. Fixes #3253. Fixes #3286. Fixes #5762.
- Loading branch information
commit e2cd6fa869cec6d92062fb687d8e6952c1202017
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
|
||
#include "npy_config.h" | ||
#include "npy_pycompat.h" | ||
#include "npy_import.h" | ||
#include "ufunc_override.h" | ||
#include "common.h" | ||
#include "ctors.h" | ||
|
@@ -358,15 +359,23 @@ NPY_NO_EXPORT PyObject * | |
PyArray_GetField(PyArrayObject *self, PyArray_Descr *typed, int offset) | ||
{ | ||
PyObject *ret = NULL; | ||
PyObject *safe; | ||
static PyObject *checkfunc = NULL; | ||
|
||
if (offset < 0 || (offset + typed->elsize) > PyArray_DESCR(self)->elsize) { | ||
PyErr_Format(PyExc_ValueError, | ||
"Need 0 <= offset <= %d for requested type " | ||
"but received offset = %d", | ||
PyArray_DESCR(self)->elsize-typed->elsize, offset); | ||
Py_DECREF(typed); | ||
npy_cache_pyfunc("numpy.core._internal", "_getfield_is_safe", &checkfunc); | ||
if (checkfunc == NULL) { | ||
return NULL; | ||
} | ||
|
||
/* check that we are not reinterpreting memory containing Objects */ | ||
/* only returns True or raises */ | ||
safe = PyObject_CallFunction(checkfunc, "OOi", PyArray_DESCR(self), | ||
typed, offset); | ||
if (safe == NULL) { | ||
return NULL; | ||
} | ||
Py_DECREF(safe); | ||
|
||
ret = PyArray_NewFromDescr(Py_TYPE(self), | ||
typed, | ||
PyArray_NDIM(self), PyArray_DIMS(self), | ||
|
@@ -417,23 +426,12 @@ PyArray_SetField(PyArrayObject *self, PyArray_Descr *dtype, | |
PyObject *ret = NULL; | ||
int retval = 0; | ||
|
||
if (offset < 0 || (offset + dtype->elsize) > PyArray_DESCR(self)->elsize) { | ||
PyErr_Format(PyExc_ValueError, | ||
"Need 0 <= offset <= %d for requested type " | ||
"but received offset = %d", | ||
PyArray_DESCR(self)->elsize-dtype->elsize, offset); | ||
Py_DECREF(dtype); | ||
return -1; | ||
} | ||
ret = PyArray_NewFromDescr(Py_TYPE(self), | ||
dtype, PyArray_NDIM(self), PyArray_DIMS(self), | ||
PyArray_STRIDES(self), PyArray_BYTES(self) + offset, | ||
PyArray_FLAGS(self), (PyObject *)self); | ||
/* getfield returns a view we can write to */ | ||
ret = PyArray_GetField(self, dtype, offset); | ||
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. Interesting approach. Perhaps it warrants an explanatory comment that the return is a view, not a copy? |
||
if (ret == NULL) { | ||
return -1; | ||
} | ||
|
||
PyArray_UpdateFlags((PyArrayObject *)ret, NPY_ARRAY_UPDATE_ALL); | ||
retval = PyArray_CopyObject((PyArrayObject *)ret, val); | ||
Py_DECREF(ret); | ||
return retval; | ||
|
@@ -455,13 +453,6 @@ array_setfield(PyArrayObject *self, PyObject *args, PyObject *kwds) | |
return NULL; | ||
} | ||
|
||
if (PyDataType_REFCHK(PyArray_DESCR(self))) { | ||
PyErr_SetString(PyExc_RuntimeError, | ||
"cannot call setfield on an object array"); | ||
Py_DECREF(dtype); | ||
return NULL; | ||
} | ||
|
||
if (PyArray_SetField(self, dtype, offset, value) < 0) { | ||
return NULL; | ||
} | ||
|
Oops, something went wrong.
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.
There are smarter ways of going about this check, but we probably shouldn't let micro-optimizations distract us from the larger goal.