8000 WIP: ENH: allow __array_ufunc__ to override __matmul__ by mattip · Pull Request #11061 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

WIP: ENH: allow __array_ufunc__ to override __matmul__ #11061

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
check_override is a bound class method, no self needed. Returns tuple
  • Loading branch information
mattip committed May 23, 2018
commit 40b5b79cff9d27737c30255737692db713fad9cf
8 changes: 4 additions & 4 deletions numpy/core/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ def solve(self, other):
return result
'''
def __call__(self, meth):
def wrap(obj, *args, **kwds):
r = self.check_override(*((self,) + args), **kwds)
if r:
def wrap(*args, **kwds):
(status, r) = self.check_override(*args, **kwds)
if status > 0:
return r
return meth(obj, *args, **kwds)
return meth(*args, **kwds)
wrap.__name__ = meth.__name__
wrap.__doc__ = meth.__doc__
return wrap
Expand Down
14 changes: 11 additions & 3 deletions numpy/core/src/multiarray/number.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ static PyObject *
array_matrix_multiply(PyArrayObject *m1, PyObject *m2)
{
static PyObject *matmul=NULL, *wrapper=NULL, *ufunc=NULL, *checker=NULL;
PyObject *result=NULL;
PyObject *result = Py_None, *res_tuple;
int status;
if (ufunc == NULL) {
PyObject *s;
npy_cache_import("numpy.core.multiarray", "matmul", &matmul);
8000 Expand All @@ -393,10 +394,17 @@ array_matrix_multiply(PyArrayObject *m1, PyObject *m2)
}
}
BINOP_GIVE_UP_IF_NEEDED(m1, m2, nb_matrix_multiply, array_matrix_multiply);
result = PyObject_CallFunctionObjArgs(checker, ufunc, m1, m2, NULL);
if (result && result != Py_None) {
res_tuple = PyObject_CallFunctionObjArgs(checker, ufunc, m1, m2, NULL);
if (PyArg_ParseTuple(res_tuple, "iO", &status, &result) < 0) {
Py_DECREF(res_tuple);
return NULL;
}
if (status > 0) {
Py_INCREF(result);
Py_DECREF(res_tuple);
return result;
}
Py_DECREF(res_tuple);
if (PyErr_Occurred())
return NULL;
return PyArray_GenericBinaryFunction(m1, m2, matmul);
Expand Down
4 changes: 2 additions & 2 deletions numpy/core/src/umath/ufunc_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -5875,9 +5875,9 @@ ufunc_check_override(PyObject *self, PyObject *args, PyObject *kwds) {
return NULL;
}
else if (result) {
return result;
return Py_BuildValue("iO", 1, result);
}
Py_RETURN_NONE;
return Py_BuildValue("iO", 0, Py_None);
}

static struct PyMethodDef ufuncwrapper_methods[] = {
Expand Down
0