8000 BUG/ENH: Switch to simplified __array_ufunc__/binop interaction · charris/numpy@73a4fc3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 73a4fc3

Browse files
njsmithcharris
authored andcommitted
BUG/ENH: Switch to simplified __array_ufunc__/binop interaction
As per the discussion at numpygh-5844, and in particular numpy#5844 (comment) this commit switches binop dispatch to mostly defer to ufuncs, except in some specific cases elaborated in a long comment in number.c. The basic strategy is to define a single piece of C code that knows how to handle forward binop overrides, and we put it into private/binop_override.h so that it can be accessed by both the array code in multiarray.so and the scalar code in umath.so.
1 parent c06f2a3 commit 73a4fc3

File tree

8 files changed

+521
-442
lines changed

8 files changed

+521
-442
lines changed

numpy/core/src/multiarray/arrayobject.c

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ maintainer email: oliphant.travis@ieee.org
5454
#include "mem_overlap.h"
5555
#include "numpyos.h"
5656

57+
#include "binop_override.h"
58+
5759
/*NUMPY_API
5860
Compute the size of an array (in number of items)
5961
*/
@@ -1335,23 +1337,12 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
13351337

13361338
switch (cmp_op) {
13371339
case Py_LT:
1338-
if (needs_right_binop_forward(obj_self, other, "__gt__", 0) &&
1339-
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
1340-
/* See discussion in number.c */
1341-
Py_INCREF(Py_NotImplemented);
1342-
return Py_NotImplemented;
1343-
}
1344-
result = PyArray_GenericBinaryFunction(self, other,
1345-
n_ops.less);
1340+
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
1341+
result = PyArray_GenericBinaryFunction(self, other, n_ops.less);
13461342
break;
13471343
case Py_LE:
1348-
if (needs_right_binop_forward(obj_self, other, "__ge__", 0) &&
1349-
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
1350-
Py_INCREF(Py_NotImplemented);
1351-
return Py_NotImplemented;
1352-
}
1353-
result = PyArray_GenericBinaryFunction(self, other,
1354-
n_ops.less_equal);
1344+
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
1345+
result = PyArray_GenericBinaryFunction(self, other, n_ops.less_equal);
13551346
break;
13561347
case Py_EQ:
13571348
/*
@@ -1401,11 +1392,7 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
14011392
return result;
14021393
}
14031394

1404-
if (needs_right_binop_forward(obj_self, other, "__eq__", 0) &&
1405-
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
1406-
Py_INCREF(Py_NotImplemented);
1407-
return Py_NotImplemented;
1408-
}
1395+
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
14091396
result = PyArray_GenericBinaryFunction(self,
14101397
(PyObject *)other,
14111398
n_ops.equal);
@@ -1478,11 +1465,7 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
14781465
return result;
14791466
}
14801467

1481-
if (needs_right_binop_forward(obj_self, other, "__ne__", 0) &&
1482-
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
1483-
Py_INCREF(Py_NotImplemented);
1484-
return Py_NotImplemented;
1485-
}
1468+
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
14861469
result = PyArray_GenericBinaryFunction(self, (PyObject *)other,
14871470
n_ops.not_equal);
14881471
if (result == NULL) {
@@ -1502,20 +1485,12 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
15021485
}
15031486
break;
15041487
case Py_GT:
1505-
if (needs_right_binop_forward(obj_self, other, "__lt__", 0) &&
1506-
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
1507-
Py_INCREF(Py_NotImplemented);
1508-
return Py_NotImplemented;
1509-
}
1488+
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
15101489
result = PyArray_GenericBinaryFunction(self, other,
15111490
n_ops.greater);
15121491
break;
15131492
case Py_GE:
1514-
if (needs_right_binop_forward(obj_self, other, "__le__", 0) &&
1515-
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
1516-
Py_INCREF(Py_NotImplemented);
1517-
return Py_NotImplemented;
1518-
}
1493+
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
15191494
result = PyArray_GenericBinaryFunction(self, other,
15201495
n_ops.greater_equal);
15211496
break;

0 commit comments

Comments
 (0)
0