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

Skip to content

Commit 258fd4e

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 aba3788 commit 258fd4e

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
/*
@@ -1402,11 +1393,7 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
14021393
return result;
14031394
}
14041395

1405-
if (needs_right_binop_forward(obj_self, other, "__eq__", 0) &&
1406-
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
1407-
Py_INCREF(Py_NotImplemented);
1408-
return Py_NotImplemented;
1409-
}
1396+
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
14101397
result = PyArray_GenericBinaryFunction(self,
14111398
(PyObject *)other,
14121399
n_ops.equal);
@@ -1480,11 +1467,7 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
14801467
return result;
14811468
}
14821469

1483-
if (needs_right_binop_forward(obj_self, other, "__ne__", 0) &&
1484-
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
1485-
Py_INCREF(Py_NotImplemented);
1486-
return Py_NotImplemented;
1487-
}
1470+
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
14881471
result = PyArray_GenericBinaryFunction(self, (PyObject *)other,
14891472
n_ops.not_equal);
14901473
if (result == NULL) {
@@ -1504,20 +1487,12 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
15041487
}
15051488
break;
15061489
case Py_GT:
1507-
if (needs_right_binop_forward(obj_self, other, "__lt__", 0) &&
1508-
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
1509-
Py_INCREF(Py_NotImplemented);
1510-
return Py_NotImplemented;
1511-
}
1490+
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
15121491
result = PyArray_GenericBinaryFunction(self, other,
15131492
n_ops.greater);
15141493
break;
15151494
case Py_GE:
1516-
if (needs_right_binop_forward(obj_self, other, "__le__", 0) &&
1517-
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
1518-
Py_INCREF(Py_NotImplemented);
1519-
return Py_NotImplemented;
1520-
}
1495+
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
15211496
result = PyArray_GenericBinaryFunction(self, other,
15221497
n_ops.greater_equal);
15231498
break;

0 commit comments

Comments
 (0)
0