-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: fix for #4753 (out keyword with __numpy_ufunc__) #5566
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
out
kwarg for __nump_ufunc__ override
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
#include <string.h> | ||
#include "numpy/ufuncobject.h" | ||
|
||
/* Normalize different ufunc methods */ | ||
static void | ||
normalize___call___args(PyUFuncObject *ufunc, PyObject *args, | ||
PyObject **normal_args, PyObject **normal_kwds, | ||
|
@@ -152,6 +153,64 @@ normalize_at_args(PyUFuncObject *ufunc, PyObject *args, | |
return; | ||
} | ||
|
||
/* | ||
* Check args for a object with a `__numpy_ufunc__` attribute. | ||
*/ | ||
static int | ||
check_tuple_for_numpy_ufunc(PyObject *args, int *noa, | ||
int *i, PyObject *with_override[NPY_MAXARGS], | ||
int with_override_pos[NPY_MAXARGS]) { | ||
int og_i = *i; /* original i */ | ||
int og_noa = *noa; /* original number of overriding args */ | ||
int nargs = PyTuple_GET_SIZE(args); | ||
PyObject *obj; | ||
|
||
for (; *i < (og_i + nargs); ++*i) { | ||
|
||
obj = PyTuple_GET_ITEM(args, *i); | ||
|
||
/* short circuit optimization for common cases. */ | ||
if (PyArray_CheckExact(obj) || PyArray_IsScalar(obj, Generic) || | ||
_is_basic_python_type(obj)) { | ||
continue; | ||
} | ||
|
||
if (PyObject_HasAttrString(obj, "__numpy_ufunc__")) { | ||
with_override[*noa] = obj; | ||
with_override_pos[*noa] = *i; | ||
++*noa; | ||
} | ||
} | ||
if ((noa - og_noa) > 0) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
static int | ||
check_kwds_for_numpy_ufunc(PyObject *kwds, int *noa, int *i, | ||
PyObject **with_override, | ||
int with_override_pos[NPY_MAXARGS]) { | ||
PyObject *obj; | ||
if ((kwds)&& (PyDict_CheckExact(kwds))) { | ||
obj = PyDict_GetItemString(kwds, "out"); | ||
if (obj != NULL) { | ||
if (PyObject_HasAttrString(obj, "__numpy_ufunc__")) { | ||
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. This may be a microoptimization, but similarly to what you do in 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. Okay thanks @jaimefrio |
||
with_override[*noa] = obj; | ||
with_override_pos[*noa] = *i; | ||
++*noa; | ||
return 1; | ||
} | ||
if PyTuple_CheckExact(obj) { | ||
return check_tuple_for_numpy_ufunc(obj, noa, i, | ||
with_override, | ||
with_override_pos); | ||
} | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
/* | ||
* Check a set of args for the `__numpy_ufunc__` method. If more than one of | ||
* the input arguments implements `__numpy_ufunc__`, they are tried in the | ||
|
@@ -169,11 +228,10 @@ PyUFunc_CheckOverride(PyUFuncObject *ufunc, char *method, | |
PyObject **result, | ||
int nin) | ||
{ | ||
int i; | ||
int override_pos; /* Position of override in args.*/ | ||
int i = 0; | ||
int override_pos; /* Position of winning override in args.*/ | ||
int j; | ||
|
||
int nargs = PyTuple_GET_SIZE(args); | ||
int noa = 0; /* Number of overriding args.*/ | ||
|
||
PyObject *obj; | ||
|
@@ -205,22 +263,8 @@ PyUFunc_CheckOverride(PyUFuncObject *ufunc, char *method, | |
goto fail; | ||
} | ||
|
||
for (i = 0; i < nargs; ++i) { | ||
obj = PyTuple_GET_ITEM(args, i); | ||
/* | ||
* TODO: could use PyArray_GetAttrString_SuppressException if it | ||
* weren't private to multiarray.so | ||
*/ | ||
if (PyArray_CheckExact(obj) || PyArray_IsScalar(obj, Generic) || | ||
_is_basic_python_type(obj)) { | ||
continue; | ||
} | ||
if (PyObject_HasAttrString(obj, "__numpy_ufunc__")) { | ||
with_override[noa] = obj; | ||
with_override_pos[noa] = i; | ||
++noa; | ||
} | ||
} | ||
check_tuple_for_numpy_ufunc(args, &noa, &i, with_override, with_override_pos); | ||
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. The return value doesn't seem to be used for anything, is that intentional? If so, why not use it for 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. Will do. 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. Or you could just combine the functions. They aren't used anywhere else, correct? |
||
check_kwds_for_numpy_ufunc(kwds, &noa, &i, with_override, with_override_pos); | ||
|
||
/* No overrides, bail out.*/ | ||
if (noa == 0) { | ||
|
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.
Ewww. Instead of
*i
, use a local variable and assign its value on exit. Best rename the argument to something other thani
also.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.
Noted.