Skip to content

Commit 1163594

Browse files
committed
BUG: longdouble op None could cause infinite recursion
This is the second half of the fix for numpygh-1296 (trac 698). None + np.longdouble(3) would cause either a RunTimeError or an interpreter crash. The swapped case (np.longdouble(3) + None) was fixed in 2008 (376d483). When writing binary operators in C, they must treat both arguments equivalently because they will be called for both __op__ and __rop__. This bug fix is necessary because the longdouble and clongdouble operators did not maintain this symmetry.
1 parent 3509704 commit 1163594

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

‎numpy/core/src/umath/scalarmath.c.src‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,9 @@ _@name@_convert2_to_ctypes(PyObject *a, @type@ *arg1,
737737
{
738738
int ret;
739739
ret = _@name@_convert_to_ctype(a, arg1);
740+
if (ret == -2) {
741+
return -3;
742+
}
740743
if (ret < 0) {
741744
return ret;
742745
}

‎numpy/core/tests/test_scalarmath.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,14 @@ def test_builtin_abs(self):
524524
def test_numpy_abs(self):
525525
self._test_abs_func(np.abs)
526526

527+
def test_longdouble_inf_loop():
528+
mul = lambda x, y: x * y
529+
assert_raises(TypeError, mul, np.longdouble(3), None)
530+
assert_raises(TypeError, mul, None, np.longdouble(3))
531+
532+
assert_raises(TypeError, mul, np.clongdouble(3), None)
533+
assert_raises(TypeError, mul, None, np.clongdouble(3))
534+
527535

528536
if __name__ == "__main__":
529537
run_module_suite()

0 commit comments

Comments
 (0)