From c9bb618fe4ea8447173020541f435bf9aabb90f9 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Tue, 17 Jan 2023 16:54:40 -0800 Subject: [PATCH 1/2] gh-89381: Fix signature for math.log and cmath.log The optional group in math.log seems unnecessary, as far as I can tell. Once we remove this, Argument Clinic knows what to do. For cmath, we just do what math is doing, which is use c_default = NULL (so the actual runtime logic is unchanged), but mark cmath.e as the fake default value. --- Modules/clinic/cmathmodule.c.h | 6 +++--- Modules/clinic/mathmodule.c.h | 39 ++++++++++++++-------------------- Modules/cmathmodule.c | 6 +++--- Modules/mathmodule.c | 7 ++---- 4 files changed, 24 insertions(+), 34 deletions(-) diff --git a/Modules/clinic/cmathmodule.c.h b/Modules/clinic/cmathmodule.c.h index b1da9452c61db8..9488504adaeb8e 100644 --- a/Modules/clinic/cmathmodule.c.h +++ b/Modules/clinic/cmathmodule.c.h @@ -639,10 +639,10 @@ cmath_tanh(PyObject *module, PyObject *arg) } PyDoc_STRVAR(cmath_log__doc__, -"log($module, z, base=, /)\n" +"log($module, z, base=cmath.e, /)\n" "--\n" "\n" -"log(z[, base]) -> the logarithm of z to the given base.\n" +"Return the logarithm of z to the given base.\n" "\n" "If the base not specified, returns the natural logarithm (base e) of z."); @@ -982,4 +982,4 @@ cmath_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec exit: return return_value; } -/*[clinic end generated code: output=0146c656e67f5d5f input=a9049054013a1b77]*/ +/*[clinic end generated code: output=bbc937dc64f3520b input=a9049054013a1b77]*/ diff --git a/Modules/clinic/mathmodule.c.h b/Modules/clinic/mathmodule.c.h index 1f9725883b9820..8d069c96a5466b 100644 --- a/Modules/clinic/mathmodule.c.h +++ b/Modules/clinic/mathmodule.c.h @@ -187,43 +187,36 @@ math_modf(PyObject *module, PyObject *arg) } PyDoc_STRVAR(math_log__doc__, -"log(x, [base=math.e])\n" +"log($module, x, base=math.e, /)\n" +"--\n" +"\n" "Return the logarithm of x to the given base.\n" "\n" "If the base not specified, returns the natural logarithm (base e) of x."); #define MATH_LOG_METHODDEF \ - {"log", (PyCFunction)math_log, METH_VARARGS, math_log__doc__}, + {"log", _PyCFunction_CAST(math_log), METH_FASTCALL, math_log__doc__}, static PyObject * -math_log_impl(PyObject *module, PyObject *x, int group_right_1, - PyObject *base); +math_log_impl(PyObject *module, PyObject *x, PyObject *base); static PyObject * -math_log(PyObject *module, PyObject *args) +math_log(PyObject *module, PyObject *const *args, Py_ssize_t nargs) { PyObject *return_value = NULL; PyObject *x; - int group_right_1 = 0; PyObject *base = NULL; - switch (PyTuple_GET_SIZE(args)) { - case 1: - if (!PyArg_ParseTuple(args, "O:log", &x)) { - goto exit; - } - break; - case 2: - if (!PyArg_ParseTuple(args, "OO:log", &x, &base)) { - goto exit; - } - group_right_1 = 1; - break; - default: - PyErr_SetString(PyExc_TypeError, "math.log requires 1 to 2 arguments"); - goto exit; + if (!_PyArg_CheckPositional("log", nargs, 1, 2)) { + goto exit; + } + x = args[0]; + if (nargs < 2) { + goto skip_optional; } - return_value = math_log_impl(module, x, group_right_1, base); + base = args[1]; +skip_optional: + return_value = math_log_impl(module, x, base); exit: return return_value; @@ -954,4 +947,4 @@ math_ulp(PyObject *module, PyObject *arg) exit: return return_value; } -/*[clinic end generated code: output=899211ec70e4506c input=a9049054013a1b77]*/ +/*[clinic end generated code: output=619aba52da61843d input=a9049054013a1b77]*/ diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c index 2038ac26e65857..976dfb1dfef3d2 100644 --- a/Modules/cmathmodule.c +++ b/Modules/cmathmodule.c @@ -952,17 +952,17 @@ cmath_tanh_impl(PyObject *module, Py_complex z) cmath.log z as x: Py_complex - base as y_obj: object = NULL + base as y_obj: object(c_default="NULL") = cmath.e / -log(z[, base]) -> the logarithm of z to the given base. +Return the logarithm of z to the given base. If the base not specified, returns the natural logarithm (base e) of z. [clinic start generated code]*/ static PyObject * cmath_log_impl(PyObject *module, Py_complex x, PyObject *y_obj) -/*[clinic end generated code: output=4effdb7d258e0d94 input=230ed3a71ecd000a]*/ +/*[clinic end generated code: output=4effdb7d258e0d94 input=6783d68473d21cf3]*/ { Py_complex y; diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 1342162fa74be7..f6962fdcba75a7 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2366,9 +2366,7 @@ loghelper(PyObject* arg, double (*func)(double)) math.log x: object - [ base: object(c_default="NULL") = math.e - ] / Return the logarithm of x to the given base. @@ -2377,9 +2375,8 @@ If the base not specified, returns the natural logarithm (base e) of x. [clinic start generated code]*/ static PyObject * -math_log_impl(PyObject *module, PyObject *x, int group_right_1, - PyObject *base) -/*[clinic end generated code: output=7b5a39e526b73fc9 input=0f62d5726cbfebbd]*/ +math_log_impl(PyObject *module, PyObject *x, PyObject *base) +/*[clinic end generated code: output=1dead263cbb1e854 input=59b97db7030bb40b]*/ { PyObject *num, *den; PyObject *ans; From 206d6c600ffb141aded932455c7e4011d8e85082 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 18 Jan 2023 00:59:12 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-01-18-00-59-10.gh-issue-89381.AYHxIv.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-01-18-00-59-10.gh-issue-89381.AYHxIv.rst diff --git a/Misc/NEWS.d/next/Library/2023-01-18-00-59-10.gh-issue-89381.AYHxIv.rst b/Misc/NEWS.d/next/Library/2023-01-18-00-59-10.gh-issue-89381.AYHxIv.rst new file mode 100644 index 00000000000000..92ee9dcb22feac --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-01-18-00-59-10.gh-issue-89381.AYHxIv.rst @@ -0,0 +1 @@ +Fix signature for :func:`math.log` and :func:`cmath.log`.