https://github.com/python/cpython/commit/c08c89addfe30a3e0e70c3200e7d5c2a56373dc8
commit: c08c89addfe30a3e0e70c3200e7d5c2a56373dc8
branch: main
author: Sergey B Kirpichev <[email protected]>
committer: zware <[email protected]>
date: 2026-07-19T02:24:16+02:00
summary:
Use Py_complex_protected AC converter for cmath.rect() (GH-154034)
files:
M Modules/clinic/cmathmodule.c.h
M Modules/cmathmodule.c
diff --git a/Modules/clinic/cmathmodule.c.h b/Modules/clinic/cmathmodule.c.h
index ecb5257cf6b2408..fe40ece030116bf 100644
--- a/Modules/clinic/cmathmodule.c.h
+++ b/Modules/clinic/cmathmodule.c.h
@@ -745,7 +745,7 @@ PyDoc_STRVAR(cmath_rect__doc__,
#define CMATH_RECT_METHODDEF \
{"rect", _PyCFunction_CAST(cmath_rect), METH_FASTCALL, cmath_rect__doc__},
-static PyObject *
+static Py_complex
cmath_rect_impl(PyObject *module, double r, double phi);
static PyObject *
@@ -754,6 +754,7 @@ cmath_rect(PyObject *module, PyObject *const *args,
Py_ssize_t nargs)
PyObject *return_value = NULL;
double r;
double phi;
+ Py_complex _return_value;
if (!_PyArg_CheckPositional("rect", nargs, 2, 2)) {
goto exit;
@@ -778,7 +779,18 @@ cmath_rect(PyObject *module, PyObject *const *args,
Py_ssize_t nargs)
goto exit;
}
}
- return_value = cmath_rect_impl(module, r, phi);
+ _return_value = cmath_rect_impl(module, r, phi);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ goto exit;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ goto exit;
+ }
+ else {
+ return_value = PyComplex_FromCComplex(_return_value);
+ }
exit:
return return_value;
@@ -987,4 +999,4 @@ cmath_isclose(PyObject *module, PyObject *const *args,
Py_ssize_t nargs, PyObjec
exit:
return return_value;
}
-/*[clinic end generated code: output=7d5ad4cf258526cd input=a9049054013a1b77]*/
+/*[clinic end generated code: output=89513888cb105a65 input=a9049054013a1b77]*/
diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c
index 7c736f4610bb988..f6e1475b00ecfbb 100644
--- a/Modules/cmathmodule.c
+++ b/Modules/cmathmodule.c
@@ -1060,7 +1060,7 @@ static Py_complex rect_special_values[7][7] = {
};
/*[clinic input]
-cmath.rect
+cmath.rect -> Py_complex_protected
r: double
phi: double
@@ -1069,9 +1069,9 @@ cmath.rect
Convert from polar coordinates to rectangular coordinates.
[clinic start generated code]*/
-static PyObject *
+static Py_complex
cmath_rect_impl(PyObject *module, double r, double phi)
-/*[clinic end generated code: output=385a0690925df2d5 input=24c5646d147efd69]*/
+/*[clinic end generated code: output=74ff3d17585f3388 input=50e60c5d28c834e6]*/
{
Py_complex z;
errno = 0;
@@ -1115,11 +1115,7 @@ cmath_rect_impl(PyObject *module, double r, double phi)
z.imag = r * sin(phi);
errno = 0;
}
-
- if (errno != 0)
- return math_error();
- else
- return PyComplex_FromCComplex(z);
+ return z;
}
/*[clinic input]
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]