summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/core/src/multiarray/arrayobject.c6
-rw-r--r--numpy/core/tests/test_regression.py23
2 files changed, 27 insertions, 2 deletions
diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c
index 1f6c3e3d5..a09e0e3b9 100644
--- a/numpy/core/src/multiarray/arrayobject.c
+++ b/numpy/core/src/multiarray/arrayobject.c
@@ -1435,7 +1435,11 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
return result;
}
array_other = (PyArrayObject *)PyArray_FromObject(other,
- NPY_NOTYPE, 0, 0);
+ NPY_NOTYPE, 0, 0);
+ if (array_other == NULL) {
+ PyErr_Clear();
+ return result;
+ }
if (PyArray_ISSTRING(self) && PyArray_ISSTRING(array_other)) {
Py_DECREF(result);
result = _strings_richcompare(self, (PyArrayObject *)
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
index e4feff372..f3e34c109 100644
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -154,7 +154,8 @@ class TestRegression(TestCase):
{'names':['a'],'formats':['foo']}, align=1)
@dec.knownfailureif((sys.version_info[0] >= 3) or
- (sys.platform == "win32" and platform.architecture()[0] == "64bit"),
+ (sys.platform == "win32" and
+ platform.architecture()[0] == "64bit"),
"numpy.intp('0xff', 16) not supported on Py3, "
"as it does not inherit from Python int")
def test_intp(self,level=rlevel):
@@ -1972,6 +1973,26 @@ class TestRegression(TestCase):
del masked, c
base.dtype
+ def test_richcompare_crash(self):
+ # gh-4613
+ import operator as op
+
+ # dummy class where __array__ throws exception
+ class Foo(object):
+ __array_priority__ = 1002
+ def __array__(self,*args,**kwargs):
+ raise Exception()
+
+ rhs = Foo()
+ lhs = np.array(1)
+ for f in [op.lt, op.le, op.gt, op.ge]:
+ if sys.version_info[0] >= 3:
+ assert_raises(TypeError, f, lhs, rhs)
+ else:
+ f(lhs, rhs)
+ assert_(not op.eq(lhs, rhs))
+ assert_(op.ne(lhs, rhs))
+
if __name__ == "__main__":
run_module_suite()