Skip to content

Commit

Permalink
BUG: core: allow scalar type richcompare to call ndarray-subclass met…
Browse files Browse the repository at this point in the history
…hods

This makes the scalar richcmp behave exactly as
"array(self) <op> other". The difference comes in when "other" is an ndarray
subclass, in which case this change allows the subclass to override the
comparison operations via usual Python binop dispatch rules.
  • Loading branch information
pv committed May 14, 2014
1 parent afe32d7 commit da40eba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion numpy/core/src/multiarray/scalartypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,11 @@ gentype_richcompare(PyObject *self, PyObject *other, int cmp_op)
if (arr == NULL) {
return NULL;
}
ret = Py_TYPE(arr)->tp_richcompare(arr, other, cmp_op);
/*
* Call via PyObject_RichCompare to ensure that other.__eq__
* has a chance to run when necessary
*/
ret = PyObject_RichCompare(arr, other, cmp_op);
Py_DECREF(arr);
return ret;
}
Expand Down
10 changes: 10 additions & 0 deletions numpy/core/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1993,6 +1993,16 @@ def __array__(self,*args,**kwargs):
assert_(not op.eq(lhs, rhs))
assert_(op.ne(lhs, rhs))

def test_richcompare_scalar_and_subclass(self):
# gh-4709
class Foo(np.ndarray):
def __eq__(self, other):
return "OK"
x = np.array([1,2,3]).view(Foo)
assert_equal(10 == x, "OK")
assert_equal(np.int32(10) == x, "OK")
assert_equal(np.array([10]) == x, "OK")


if __name__ == "__main__":
run_module_suite()

0 comments on commit da40eba

Please sign in to comment.