diff options
author | Julian Taylor <juliantaylor108@gmail.com> | 2014-04-11 20:17:45 +0200 |
---|---|---|
committer | Julian Taylor <juliantaylor108@gmail.com> | 2014-04-11 20:17:45 +0200 |
commit | fbf2072039f948ec1753d45d46d26b92cfacb1ef (patch) | |
tree | a36e36c3ecbe7be95026e56a004cd1da3373ac05 | |
parent | bd3ca1eda2cb5159ab5acdea749dead9ca90f575 (diff) | |
parent | df321538f2b278efb4f7e6f048f86d76dfb0a65d (diff) | |
download | numpy-fbf2072039f948ec1753d45d46d26b92cfacb1ef.tar.gz |
Merge pull request #4614 from charris/fix-gh-4613
BUG: Fix lack of NULL check in array_richcompare.
-rw-r--r-- | numpy/core/src/multiarray/arrayobject.c | 6 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 23 |
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() |