8000 gh-101123: Adapt vararg functions in the math module to Argument Clin… · python/cpython@3275cb1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3275cb1

Browse files
authored
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().
1 parent 94639f6 commit 3275cb1

File tree

2 files changed

+143
-37
lines changed

2 files changed

+143
-37
lines changed

Modules/clinic/mathmodule.c.h

Lines changed: 99 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/mathmodule.c

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,17 @@ m_log10(double x)
719719
}
720720

721721

722+
/*[clinic input]
723+
math.gcd
724+
725+
*integers as args: object
726+
727+
Greatest Common Divisor.
728+
[clinic start generated code]*/
729+
722730
static PyObject *
723-
math_gcd(PyObject *module, PyObject * const *args, Py_ssize_t nargs)
731+
math_gcd_impl(PyObject *module, Py_ssize_t nargs, PyObject *const *args)
732+
/*[clinic end generated code: output=b57687fcf431c1b8 input=94e675b7ceeaf0c9]*/
724733
{
725734
// Fast-path for the common case: gcd(int, int)
726735
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)
763772
return res;
764773
}
765774

766-
PyDoc_STRVAR(math_gcd_doc,
767-
"gcd($module, *integers)\n"
768-
"--\n"
769-
"\n"
770-
"Greatest Common Divisor.");
771-
772775

773776
static PyObject *
774777
long_lcm(PyObject *a, PyObject *b)
@@ -798,8 +801,17 @@ long_lcm(PyObject *a, PyObject *b)
798801
}
799802

800803

804+
/*[clinic input]
805+
math.lcm
806+
807+
*integers as args: object
808+
809+
Least Common Multiple.
810+
[clinic start generated code]*/
811+
801812
static PyObject *
802-
math_lcm(PyObject *module, PyObject * const *args, Py_ssize_t nargs)
813+
math_lcm_impl(PyObject *module, Py_ssize_t nargs, PyObject *const *args)
814+
/*[clinic end generated code: output=f3eff0c25e4d7030 input=e64c33e85f4c47c6]*/
803815
{
804816
PyObject *res, *x;
805817
Py_ssize_t i;
@@ -839,13 +851,6 @@ math_lcm(PyObject *module, PyObject * const *args, Py_ssize_t nargs)
839851
}
840852

841853

842-
PyDoc_STRVAR(math_lcm_doc,
843-
"lcm($module, *integers)\n"
844-
"--\n"
845-
"\n"
846-
"Least Common Multiple.");
847-
848-
849854
/* Call is_error when errno != 0, and where x is the result libm
850855
* returned. is_error will usually set up an exception and return
851856
* 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)
26212626
return NULL;
26222627
}
26232628

2624-
/* AC: cannot convert yet, waiting for *args support */
2629+
/*[clinic input]
2630+
math.hypot
2631+
2632+
*coordinates as args: object
2633+
2634+
Multidimensional Euclidean distance from the origin to a point.
2635+
2636+
Roughly equivalent to:
2637+
sqrt(sum(x**2 for x in coordinates))
2638+
2639+
For a two dimensional point (x, y), gives the hypotenuse
2640+
using the Pythagorean theorem: sqrt(x*x + y*y).
2641+
2642+
For example, the hypotenuse of a 3/4/5 right triangle is:
2643+
2644+
>>> hypot(3.0, 4.0)
2645+
5.0
2646+
[clinic start generated code]*/
2647+
26252648
static PyObject *
2626-
math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
2649+
math_hypot_impl(PyObject *module, Py_ssize_t nargs, PyObject *const *args)
2650+
/*[clinic end generated code: output=dcb6d4b7a1102ee1 input=5c0061a2d11235ed]*/
26272651
{
26282652
Py_ssize_t i;
26292653
PyObject *item;
@@ -2664,22 +2688,6 @@ math_hypot(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
26642688

26652689
#undef NUM_STACK_ELEMS
26662690

2667-
PyDoc_STRVAR(math_hypot_doc,
2668-
"hypot(*coordinates) -> value\n\n\
2669-
Multidimensional Euclidean distance from the origin to a point.\n\
2670-
\n\
2671-
Roughly equivalent to:\n\
2672-
sqrt(sum(x**2 for x in coordinates))\n\
2673-
\n\
2674-
For a two dimensional point (x, y), gives the hypotenuse\n\
2675-
using the Pythagorean theorem: sqrt(x*x + y*y).\n\
2676-
\n\
2677-
For example, the hypotenuse of a 3/4/5 right triangle is:\n\
2678-
\n\
2679-
>>> hypot(3.0, 4.0)\n\
2680-
5.0\n\
2681-
");
2682-
26832691
/** sumprod() ***************************************************************/
26842692

26852693
/* Forward declaration */
@@ -4112,14 +4120,14 @@ static PyMethodDef math_methods[] = {
41124120
MATH_FREXP_METHODDEF
41134121
MATH_FSUM_METHODDEF
41144122
{"gamma", math_gamma, METH_O, math_gamma_doc},
4115-
{"gcd", _PyCFunction_CAST(math_gcd), METH_FASTCALL, math_gcd_doc},
4116-
{"hypot", _PyCFunction_CAST(math_hypot), METH_FASTCALL, math_hypot_doc},
4123+
MATH_GCD_METHODDEF
4124+
MATH_HYPOT_METHODDEF
41174125
MATH_ISCLOSE_METHODDEF
41184126
MATH_ISFINITE_METHODDEF
41194127
MATH_ISINF_METHODDEF
41204128
MATH_ISNAN_METHODDEF
41214129
MATH_ISQRT_METHODDEF
4122-
{"lcm", _PyCFunction_CAST(math_lcm), METH_FASTCALL, math_lcm_doc},
4130+
MATH_LCM_METHODDEF
41234131
MATH_LDEXP_METHODDEF
41244132
{"lgamma", math_lgamma, METH_O, math_lgamma_doc},
41254133
{"log", _PyCFunction_CAST(math_log), METH_FASTCALL, math_log_doc},

0 commit comments

Comments
 (0)
0