Repository navigation
Implement ndarray.__array_ufunc__ #5
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1007,6 +1007,45 @@ array_getarray(PyArrayObject *self, PyObject *args) | |
| } | ||
|
|
||
|
|
||
| static PyObject * | ||
| array_ufunc(PyArrayObject *self, PyObject *args, PyObject *kwds) | ||
| { | ||
| PyObject *ufunc, *method_name, *normal_args, *ufunc_method, *result; | ||
|
|
||
| if (PyTuple_Size(args) < 2) { | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "__array_ufunc__ requires at least 2 arguments"); | ||
| return NULL; | ||
| } | ||
| ufunc = PyTuple_GET_ITEM(args, 0); | ||
| if (ufunc == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
||
| method_name = PyTuple_GET_ITEM(args, 1); | ||
| if (method_name == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
||
| normal_args = PyTuple_GetSlice(args, 2, PyTuple_GET_SIZE(args)); | ||
| if (normal_args == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
||
| ufunc_method = PyObject_GetAttr(ufunc, method_name); | ||
| if (ufunc_method == NULL) { | ||
| Py_DECREF(normal_args); | ||
| return NULL; | ||
| } | ||
|
|
||
| result = PyObject_Call(ufunc_method, normal_args, kwds); | ||
|
Owner
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. Hmm, why doesn't this loop? My understanding is that ufuncs cannot be used by ndarray subclasses using
Author
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. You're not missing anything: the subclass has to clean the arguments up before calling For this class, super passes on to
Author
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. p.s. The looping was of course why I needed to be sure that the
Owner
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. Since the base function is never called by the ufunc, why not just give it another name, say
Author
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. @charris - renaming it would beat the whole point of using (but I agree that it is a bit strange that the base
Owner
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. Multiple inheritance would also be a hassle, as it already is... 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. Sorry, I'm missing some context here to understand the question – can you elaborate?
Owner
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 idea here is to add @mhvk wants the function in the base class so he can call it from subclasses in his units implementation.
Author
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. @charris - @njsmith - for context, maybe most useful to look at the updated docs, in particular https://github.com/charris/numpy/pull/5/files#diff-5a79b101110fd040a0cd5a254b4742f6R515 (if that isn't clear, let me know -- it is supposed to explain why we do things this way!).
Author
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. Ah, I see we both answered. Note that this is not specific to |
||
| Py_DECREF(normal_args); | ||
| Py_DECREF(ufunc_method); | ||
| /* no need to DECREF borrowed references ufunc and method_name */ | ||
| return result; | ||
| } | ||
|
|
||
|
|
||
| static PyObject * | ||
| array_copy(PyArrayObject *self, PyObject *args, PyObject *kwds) | ||
| { | ||
|
|
@@ -2429,6 +2468,9 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = { | |
| {"__array_wrap__", | ||
| (PyCFunction)array_wraparray, | ||
| METH_VARARGS, NULL}, | ||
| {"__array_ufunc__", | ||
| (PyCFunction)array_ufunc, | ||
| METH_VARARGS | METH_KEYWORDS, NULL}, | ||
|
|
||
| /* for the sys module */ | ||
| {"__sizeof__", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| #include "npy_pycompat.h" | ||
| #include "numpy/ufuncobject.h" | ||
| #include "get_attr_string.h" | ||
| #include "npy_import.h" | ||
|
|
||
| #include "ufunc_override.h" | ||
|
|
||
|
|
@@ -169,6 +170,47 @@ normalize_at_args(PyUFuncObject *ufunc, PyObject *args, | |
| return; | ||
| } | ||
|
|
||
| /* | ||
| * Check whether an object has __array_ufunc__ defined on its class and it | ||
| * is not the default, i.e., the object is not an ndarray, and its | ||
| * __array_ufunc__ is not the same as that of ndarray. | ||
| * | ||
| * Note that since this module is used with both multiarray and umath, we do | ||
| * not have access to PyArray_Type and therewith neither to PyArray_CheckExact | ||
| * nor to the default __array_ufunc__ method, so instead we import locally. | ||
| * TODO: Can this really not be done more smartly? | ||
| */ | ||
| static int | ||
| has_non_default_array_ufunc(PyObject *obj) | ||
|
Owner
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. How about something like
Author
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. It checks two things: whether the class has |
||
| { | ||
| static PyObject *ndarray = NULL; | ||
| static PyObject *ndarray_array_ufunc = NULL; | ||
| PyTypeObject *cls_array_ufunc; | ||
| int non_default; | ||
|
|
||
| /* on first entry, import and cache ndarray and its __array_ufunc__ */ | ||
| if (ndarray == NULL) { | ||
| npy_cache_import("numpy.core.multiarray", "ndarray", &ndarray); | ||
| ndarray_array_ufunc = PyObject_GetAttrString(ndarray, | ||
| "__array_ufunc__"); | ||
| } | ||
|
|
||
| /* Fast return for ndarray */ | ||
| if (Py_TYPE(obj) == ndarray) { | ||
| return 0; | ||
| } | ||
| /* does the class define __array_ufunc__? */ | ||
| cls_array_ufunc = PyArray_GetAttrString_SuppressException( | ||
| Py_TYPE(obj), "__array_ufunc__"); | ||
| if (cls_array_ufunc == NULL) { | ||
| return 0; | ||
| } | ||
| /* is it different from ndarray.__array_ufunc__? */ | ||
| non_default = (cls_array_ufunc != ndarray_array_ufunc); | ||
| Py_DECREF(cls_array_ufunc); | ||
| return non_default; | ||
| } | ||
|
|
||
| /* | ||
| * Check a set of args for the `__array_ufunc__` method. If more than one of | ||
| * the input arguments implements `__array_ufunc__`, they are tried in the | ||
|
|
@@ -250,11 +292,14 @@ PyUFunc_CheckOverride(PyUFuncObject *ufunc, char *method, | |
| obj = out_kwd_obj; | ||
| } | ||
| } | ||
| tmp = PyArray_GetAttrString_SuppressException(obj, "__array_ufunc__"); | ||
| if (tmp) { | ||
| Py_DECREF(tmp); | ||
| with_override[noa] = obj; | ||
| ++noa; | ||
| /* | ||
| * Now see if the object provides an __array_ufunc__. However, we should | ||
| * ignore the base ndarray.__ufunc__, so we skip any ndarray as well as | ||
| * any ndarray subclass instances that did not override __array_ufunc__. | ||
| */ | ||
| if (has_non_default_array_ufunc(obj)) { | ||
| with_override[noa] = obj; | ||
| ++noa; | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
Hmm, this doesn't render correctly. Maybe the link cannot be split across lines, or maybe github has a problem. We can allow long lines for links if needed.
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.
That's strange: I did try

make htmland it looked OK: