Issue #24489: ensure a previously set C errno doesn't disturb cmath.polar().
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 7ce3b78..5896e48 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -1373,6 +1373,17 @@
return NULL;
}
+static PyObject *
+set_errno(PyObject *self, PyObject *args)
+{
+ int new_errno;
+
+ if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno))
+ return NULL;
+
+ errno = new_errno;
+ Py_RETURN_NONE;
+}
static int test_run_counter = 0;
@@ -2032,6 +2043,7 @@
static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS},
+ {"set_errno", set_errno, METH_VARARGS},
{"test_config", (PyCFunction)test_config, METH_NOARGS},
{"test_datetime_capi", test_datetime_capi, METH_NOARGS},
{"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c
index 26e7aba..a674d41 100644
--- a/Modules/cmathmodule.c
+++ b/Modules/cmathmodule.c
@@ -941,9 +941,10 @@
double r, phi;
if (!PyArg_ParseTuple(args, "D:polar", &z))
return NULL;
+ errno = 0;
PyFPE_START_PROTECT("polar function", return 0)
phi = c_atan2(z); /* should not cause any exception */
- r = c_abs(z); /* sets errno to ERANGE on overflow; otherwise 0 */
+ r = c_abs(z); /* sets errno to ERANGE on overflow */
PyFPE_END_PROTECT(r)
if (errno != 0)
return math_error();