gh-101123: Adapt vararg functions in the math module to Argument Clinic (#126235)

This implicitly fixes the math.hypot signature, which was previously
incomprehensible to inspect.signature().
This commit is contained in:
Sergey B Kirpichev 2024-10-31 17:37:47 +03:00 committed by GitHub
parent 94639f6b71
commit 3275cb1953
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 143 additions and 37 deletions

View File

@ -8,6 +8,64 @@ preserve
#endif #endif
#include "pycore_modsupport.h" // _PyArg_CheckPositional() #include "pycore_modsupport.h" // _PyArg_CheckPositional()
PyDoc_STRVAR(math_gcd__doc__,
"gcd($module, /, *integers)\n"
"--\n"
"\n"
"Greatest Common Divisor.");
#define MATH_GCD_METHODDEF \
{"gcd", _PyCFunction_CAST(math_gcd), METH_FASTCALL, math_gcd__doc__},
static PyObject *
math_gcd_impl(PyObject *module, Py_ssize_t nargs, PyObject *const *args);
static PyObject *
math_gcd(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
Py_ssize_t nvararg = nargs - 0;
PyObject *const *__clinic_args = NULL;
if (!_PyArg_CheckPositional("gcd", nargs, 0, PY_SSIZE_T_MAX)) {
goto exit;
}
__clinic_args = args + 0;
return_value = math_gcd_impl(module, nvararg, __clinic_args);
exit:
return return_value;
}
PyDoc_STRVAR(math_lcm__doc__,
"lcm($module, /, *integers)\n"
"--\n"
"\n"
"Least Common Multiple.");
#define MATH_LCM_METHODDEF \
{"lcm", _PyCFunction_CAST(math_lcm), METH_FASTCALL, math_lcm__doc__},
static PyObject *
math_lcm_impl(PyObject *module, Py_ssize_t nargs, PyObject *const *args);
static PyObject *
math_lcm(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
Py_ssize_t nvararg = nargs - 0;
PyObject *const *__clinic_args = NULL;
if (!_PyArg_CheckPositional("lcm", nargs, 0, PY_SSIZE_T_MAX)) {
goto exit;
}
__clinic_args = args + 0;
return_value = math_lcm_impl(module, nvararg, __clinic_args);
exit:
return return_value;
}
PyDoc_STRVAR(math_ceil__doc__, PyDoc_STRVAR(math_ceil__doc__,
"ceil($module, x, /)\n" "ceil($module, x, /)\n"
"--\n" "--\n"
@ -351,6 +409,46 @@ exit:
return return_value; return return_value;
} }
PyDoc_STRVAR(math_hypot__doc__,
"hypot($module, /, *coordinates)\n"
"--\n"
"\n"
"Multidimensional Euclidean distance from the origin to a point.\n"
"\n"
"Roughly equivalent to:\n"
" sqrt(sum(x**2 for x in coordinates))\n"
"\n"
"For a two dimensional point (x, y), gives the hypotenuse\n"
"using the Pythagorean theorem: sqrt(x*x + y*y).\n"
"\n"
"For example, the hypotenuse of a 3/4/5 right triangle is:\n"
"\n"
" >>> hypot(3.0, 4.0)\n"
" 5.0");
#define MATH_HYPOT_METHODDEF \
{"hypot", _PyCFunction_CAST(math_hypot), METH_FASTCALL, math_hypot__doc__},
static PyObject *
math_hypot_impl(PyObject *module, Py_ssize_t nargs, PyObject *const *args);
static PyObject *
math_hypot(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
Py_ssize_t nvararg = nargs - 0;
PyObject *const *__clinic_args = NULL;
if (!_PyArg_CheckPositional("hypot", nargs, 0, PY_SSIZE_T_MAX)) {
goto exit;
}
__clinic_args = args + 0;
return_value = math_hypot_impl(module, nvararg, __clinic_args);
exit:
return return_value;
}
PyDoc_STRVAR(math_sumprod__doc__, PyDoc_STRVAR(math_sumprod__doc__,
"sumprod($module, p, q, /)\n" "sumprod($module, p, q, /)\n"
"--\n" "--\n"
@ -1011,4 +1109,4 @@ math_ulp(PyObject *module, PyObject *arg)
exit: exit:
return return_value; return return_value;
} }
/*[clinic end generated code: output=755da3b1dbd9e45f input=a9049054013a1b77]*/ /*[clinic end generated code: output=ee0a2f6bd1220061 input=a9049054013a1b77]*/

View File

@ -719,8 +719,17 @@ m_log10(double x)
} }
/*[clinic input]
math.gcd
*integers as args: object
Greatest Common Divisor.
[clinic start generated code]*/
static PyObject * static PyObject *
math_gcd(PyObject *module, PyObject * const *args, Py_ssize_t nargs) math_gcd_impl(PyObject *module, Py_ssize_t nargs, PyObject *const *args)
/*[clinic end generated code: output=b57687fcf431c1b8 input=94e675b7ceeaf0c9]*/
{ {
// Fast-path for the common case: gcd(int, int) // Fast-path for the common case: gcd(int, int)
if (nargs == 2 && PyLong_CheckExact(args[0]) && PyLong_CheckExact(args[1])) if (nargs == 2 && PyLong_CheckExact(args[0]) && PyLong_CheckExact(args[1]))
@ -763,12 +772,6 @@ math_gcd(PyObject *module, PyObject * const *args, Py_ssize_t nargs)
return res; return res;
} }
PyDoc_STRVAR(math_gcd_doc,
"gcd($module, *integers)\n"
"--\n"
"\n"
"Greatest Common Divisor.");
static PyObject * static PyObject *
long_lcm(PyObject *a, PyObject *b) long_lcm(PyObject *a, PyObject *b)
@ -798,8 +801,17 @@ long_lcm(PyObject *a, PyObject *b)
} }
/*[clinic input]
math.lcm
*integers as args: object
Least Common Multiple.
[clinic start generated code]*/
static PyObject * static PyObject *
math_lcm(PyObject *module, PyObject * const *args, Py_ssize_t nargs) math_lcm_impl(PyObject *module, Py_ssize_t nargs, PyObject *const *args)
/*[clinic end generated code: output=f3eff0c25e4d7030 input=e64c33e85f4c47c6]*/
{ {
PyObject *res, *x; PyObject *res, *x;
Py_ssize_t i; Py_ssize_t i;
@ -839,13 +851,6 @@ math_lcm(PyObject *module, PyObject * const *args, Py_ssize_t nargs)
} }
PyDoc_STRVAR(math_lcm_doc,
"lcm($module, *integers)\n"
"--\n"
"\n"
"Least Common Multiple.");
/* Call is_error when errno != 0, and where x is the result libm /* Call is_error when errno != 0, and where x is the result libm
* returned. is_error will usually set up an exception and return * returned. is_error will usually set up an exception and return
* true (1), but may return false (0) without setting up an exception. * true (1), but may return false (0) without setting up an exception.
@ -2621,9 +2626,28 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
return NULL; return NULL;
} }
/* AC: cannot convert yet, waiting for *args support */ /*[clinic input]
math.hypot
*coordinates as args: object
Multidimensional Euclidean distance from the origin to a point.
Roughly equivalent to:
sqrt(sum(x**2 for x in coordinates))
For a two dimensional point (x, y), gives the hypotenuse
using the Pythagorean theorem: sqrt(x*x + y*y).
For example, the hypotenuse of a 3/4/5 right triangle is:
>>> hypot(3.0, 4.0)
5.0
[clinic start generated code]*/
static PyObject * static PyObject *
math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs) math_hypot_impl(PyObject *module, Py_ssize_t nargs, PyObject *const *args)
/*[clinic end generated code: output=dcb6d4b7a1102ee1 input=5c0061a2d11235ed]*/
{ {
Py_ssize_t i; Py_ssize_t i;
PyObject *item; PyObject *item;
@ -2664,22 +2688,6 @@ math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
#undef NUM_STACK_ELEMS #undef NUM_STACK_ELEMS
PyDoc_STRVAR(math_hypot_doc,
"hypot(*coordinates) -> value\n\n\
Multidimensional Euclidean distance from the origin to a point.\n\
\n\
Roughly equivalent to:\n\
sqrt(sum(x**2 for x in coordinates))\n\
\n\
For a two dimensional point (x, y), gives the hypotenuse\n\
using the Pythagorean theorem: sqrt(x*x + y*y).\n\
\n\
For example, the hypotenuse of a 3/4/5 right triangle is:\n\
\n\
>>> hypot(3.0, 4.0)\n\
5.0\n\
");
/** sumprod() ***************************************************************/ /** sumprod() ***************************************************************/
/* Forward declaration */ /* Forward declaration */
@ -4112,14 +4120,14 @@ static PyMethodDef math_methods[] = {
MATH_FREXP_METHODDEF MATH_FREXP_METHODDEF
MATH_FSUM_METHODDEF MATH_FSUM_METHODDEF
{"gamma", math_gamma, METH_O, math_gamma_doc}, {"gamma", math_gamma, METH_O, math_gamma_doc},
{"gcd", _PyCFunction_CAST(math_gcd), METH_FASTCALL, math_gcd_doc}, MATH_GCD_METHODDEF
{"hypot", _PyCFunction_CAST(math_hypot), METH_FASTCALL, math_hypot_doc}, MATH_HYPOT_METHODDEF
MATH_ISCLOSE_METHODDEF MATH_ISCLOSE_METHODDEF
MATH_ISFINITE_METHODDEF MATH_ISFINITE_METHODDEF
MATH_ISINF_METHODDEF MATH_ISINF_METHODDEF
MATH_ISNAN_METHODDEF MATH_ISNAN_METHODDEF
MATH_ISQRT_METHODDEF MATH_ISQRT_METHODDEF
{"lcm", _PyCFunction_CAST(math_lcm), METH_FASTCALL, math_lcm_doc}, MATH_LCM_METHODDEF
MATH_LDEXP_METHODDEF MATH_LDEXP_METHODDEF
{"lgamma", math_lgamma, METH_O, math_lgamma_doc}, {"lgamma", math_lgamma, METH_O, math_lgamma_doc},
{"log", _PyCFunction_CAST(math_log), METH_FASTCALL, math_log_doc}, {"log", _PyCFunction_CAST(math_log), METH_FASTCALL, math_log_doc},