@@ -1017,7 +1017,7 @@ math_1a(PyObject *arg, double (*func) (double), const char *err_msg)
10171017 The last rule is used to catch overflow on platforms which follow
10181018 C89 but for which HUGE_VAL is not an infinity.
10191019
1020- For most two-argument functions (copysign, fmod, hypot, atan2 )
1020+ For most two-argument functions (fmod, hypot)
10211021 these rules are enough to ensure that Python's functions behave as
10221022 specified in 'Annex F' of the C99 standard, with the 'invalid' and
10231023 'divide-by-zero' floating-point exceptions mapping to Python's
@@ -1030,8 +1030,9 @@ math_2(PyObject *const *args, Py_ssize_t nargs,
10301030 double (* func ) (double , double ), const char * funcname )
10311031{
10321032 double x , y , r ;
1033- if (!_PyArg_CheckPositional (funcname , nargs , 2 , 2 ))
1033+ if (!_PyArg_CheckPositional (funcname , nargs , 2 , 2 )) {
10341034 return NULL ;
1035+ }
10351036 x = PyFloat_AsDouble (args [0 ]);
10361037 if (x == -1.0 && PyErr_Occurred ()) {
10371038 return NULL ;
@@ -1060,6 +1061,29 @@ math_2(PyObject *const *args, Py_ssize_t nargs,
10601061 return PyFloat_FromDouble (r );
10611062}
10621063
1064+ /* variant of math_2, to be used when the function being wrapped is known NOT
1065+ to need error checking (i.e., no overflow, invalid, or divide-by-zero). */
1066+
1067+ static PyObject *
1068+ math_2ne (PyObject * const * args , Py_ssize_t nargs ,
1069+ double (* func ) (double , double ), const char * funcname )
1070+ {
1071+ double x , y , r ;
1072+ if (!_PyArg_CheckPositional (funcname , nargs , 2 , 2 )) {
1073+ return NULL ;
1074+ }
1075+ x = PyFloat_AsDouble (args [0 ]);
1076+ if (x == -1.0 && PyErr_Occurred ()) {
1077+ return NULL ;
1078+ }
1079+ y = PyFloat_AsDouble (args [1 ]);
1080+ if (y == -1.0 && PyErr_Occurred ()) {
1081+ return NULL ;
1082+ }
1083+ r = (* func )(x , y ); // Ignore errno on purpose.
1084+ return PyFloat_FromDouble (r );
1085+ }
1086+
10631087#define FUNC1 (funcname , func , can_overflow , docstring ) \
10641088 static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
10651089 return math_1(args, func, can_overflow, NULL); \
@@ -1090,6 +1114,12 @@ math_2(PyObject *const *args, Py_ssize_t nargs,
10901114 }\
10911115 PyDoc_STRVAR(math_##funcname##_doc, docstring);
10921116
1117+ #define FUNC2NE (funcname , func , docstring ) \
1118+ static PyObject * math_##funcname(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { \
1119+ return math_2ne(args, nargs, func, #funcname); \
1120+ }\
1121+ PyDoc_STRVAR(math_##funcname##_doc, docstring);
1122+
10931123FUNC1D (acos , acos , 0 ,
10941124 "acos($module, x, /)\n--\n\n"
10951125 "Return the arc cosine (measured in radians) of x.\n\n"
@@ -1121,11 +1151,14 @@ FUNC1(atan, atan, 0,
11211151 "atan($module, x, /)\n--\n\n"
11221152 "Return the arc tangent (measured in radians) of x.\n\n"
11231153 "The result is between -pi/2 and pi/2." )
1124- FUNC2 (atan2 , atan2 ,
1154+ /* gh-153144: Ignore atan2 and atan2pi errno on purpose since it can optionally
1155+ * be EDOM, which we should ignore, or ERANGE on underflow, which is
1156+ * silent on Python. Overflow is not possible. */
1157+ FUNC2NE (atan2 , atan2 ,
11251158 "atan2($module, y, x, /)\n--\n\n"
11261159 "Return the arc tangent (measured in radians) of y/x.\n\n"
11271160 "Unlike atan(y/x), the signs of both x and y are considered." )
1128- FUNC2 (atan2pi , m_atan2pi ,
1161+ FUNC2NE (atan2pi , m_atan2pi ,
11291162 "atan2pi($module, y, x, /)\n--\n\n"
11301163 "Return the arc tangent (measured in half-turns) of y/x.\n\n"
11311164 "Unlike atanpi(y/x), the signs of both x and y are considered." )
@@ -1178,7 +1211,9 @@ math_ceil(PyObject *module, PyObject *number)
11781211 return PyLong_FromDouble (ceil (x ));
11791212}
11801213
1181- FUNC2 (copysign , copysign ,
1214+ /* gh-153144: Ignore copysign errno on purpose since IEEE Std 754-2019 requires
1215+ * it to operate on bit strings and to signal no exception. */
1216+ FUNC2NE (copysign , copysign ,
11821217 "copysign($module, x, y, /)\n--\n\n"
11831218 "Return a float with the magnitude (absolute value) of x but the sign of y.\n\n"
11841219 "On platforms that support signed zeros, copysign(1.0, -0.0)\n"
0 commit comments