diff -r a6548e230ed6 Lib/test/test_math.py --- a/Lib/test/test_math.py Thu Oct 27 19:33:22 2016 +0300 +++ b/Lib/test/test_math.py Thu Oct 27 18:46:27 2016 +0100 @@ -475,6 +475,13 @@ self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0) self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0) self.ftest('degrees(0)', math.degrees(0), 0) + self.assertEqual(math.degrees(INF), INF) + self.assertEqual(math.degrees(NINF), NINF) + self.assertTrue(math.isnan(math.degrees(NAN))) + with self.assertRaises(OverflowError): + math.degrees(FLOAT_MAX) + with self.assertRaises(OverflowError): + math.degrees(-FLOAT_MAX) def testExp(self): self.assertRaises(TypeError, math.exp) @@ -999,6 +1006,9 @@ self.ftest('radians(90)', math.radians(90), math.pi/2) self.ftest('radians(-45)', math.radians(-45), -math.pi/4) self.ftest('radians(0)', math.radians(0), 0) + self.assertEqual(math.radians(INF), INF) + self.assertEqual(math.radians(NINF), NINF) + self.assertTrue(math.isnan(math.radians(NAN))) def testSin(self): self.assertRaises(TypeError, math.sin) diff -r a6548e230ed6 Modules/mathmodule.c --- a/Modules/mathmodule.c Thu Oct 27 19:33:22 2016 +0300 +++ b/Modules/mathmodule.c Thu Oct 27 18:46:27 2016 +0100 @@ -1927,10 +1927,24 @@ static PyObject * math_degrees(PyObject *self, PyObject *arg) { - double x = PyFloat_AsDouble(arg); + double r, x; + x = PyFloat_AsDouble(arg); if (x == -1.0 && PyErr_Occurred()) return NULL; - return PyFloat_FromDouble(x * radToDeg); + + /* NaNs and infinities should be returned unchanged. */ + if (!Py_IS_FINITE(x)) { + return PyFloat_FromDouble(x); + } + + /* On overflow, raise OverflowError */ + r = x * radToDeg; + if (!Py_IS_FINITE(r)) { + PyErr_SetString(PyExc_OverflowError, + "math range error"); + return NULL; + } + return PyFloat_FromDouble(r); } PyDoc_STRVAR(math_degrees_doc,