Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,22 @@ def test_mtestfile(self):
self.fail('Failures in test_mtestfile:\n ' +
'\n '.join(failures))

def test_issue39871(self):
# A SystemError should not be raised if the first arg to atan2(),
# copysign(), or remainder() cannot be converted to a float.
class F:
def __float__(self):
self.converted = True
1/0
for func in math.atan2, math.copysign, math.remainder:
y = F()
with self.assertRaises(TypeError):
func("not a number", y)

# There should not have been any attempt to convert the second
# argument to a float.
self.assertFalse(getattr(y, "converted", False))

# Custom assertions.

def assertIsNaN(self, value):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a possible :exc:`SystemError` in ``math.{atan2,copysign,remainder}()``
when the first argument cannot be converted to a :class:`float`. Patch by
Zachary Spytz.
6 changes: 5 additions & 1 deletion Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1004,9 +1004,13 @@ math_2(PyObject *args, double (*func) (double, double), const char *funcname)
if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy))
return NULL;
x = PyFloat_AsDouble(ox);
if (x == -1.0 && PyErr_Occurred()) {
return NULL;
}
y = PyFloat_AsDouble(oy);
if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
if (y == -1.0 && PyErr_Occurred()) {
return NULL;
}
errno = 0;
PyFPE_START_PROTECT("in math_2", return 0);
r = (*func)(x, y);
Expand Down