]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-89381: Fix invalid signatures of math/cmath.log (#101404)
authorSergey B Kirpichev <skirpichev@gmail.com>
Sun, 29 Jan 2023 19:50:10 +0000 (22:50 +0300)
committerGitHub <noreply@github.com>
Sun, 29 Jan 2023 19:50:10 +0000 (11:50 -0800)
Doc/library/cmath.rst
Doc/library/math.rst
Lib/test/test_math.py
Misc/NEWS.d/next/Library/2023-01-16-10-42-58.gh-issue-89381.lM2WL0.rst [new file with mode: 0644]
Modules/clinic/cmathmodule.c.h
Modules/clinic/mathmodule.c.h
Modules/cmathmodule.c
Modules/mathmodule.c

index 28cd96b0e12da9c71cd78fad1730e9f7cd4f66be..c575b90e6461ed8cda9864e41367987745d98a98 100644 (file)
@@ -89,7 +89,7 @@ Power and logarithmic functions
    logarithms.
 
 
-.. function:: log(x[, base])
+.. function:: log(x, base=None)
 
    Returns the logarithm of *x* to the given *base*. If the *base* is not
    specified, returns the natural logarithm of *x*. There is one branch cut, from 0
index 797f32408eac3d9b1398637570cfef41ac8212b3..9da22b6ad0562f553aec81709d8d35ad0ca486ee 100644 (file)
@@ -393,13 +393,12 @@ Power and logarithmic functions
    .. versionadded:: 3.2
 
 
-.. function:: log(x[, base])
+.. function:: log(x, base=None)
 
-   With one argument, return the natural logarithm of *x* (to base *e*).
-
-   With two arguments, return the logarithm of *x* to the given *base*,
-   calculated as ``log(x)/log(base)``.
+   Return the logarithm of *x* to the given *base*.
 
+   If the *base* is not specified, returns the natural
+   logarithm (base *e*) of *x*.
 
 .. function:: log1p(x)
 
index fcdec93484e00034e818f6175d33354a38f7ee3d..2c84e55ab45c57df245cb6d76cdd43b10d2048b0 100644 (file)
@@ -1146,6 +1146,7 @@ class MathTests(unittest.TestCase):
         self.ftest('log(1/e)', math.log(1/math.e), -1)
         self.ftest('log(1)', math.log(1), 0)
         self.ftest('log(e)', math.log(math.e), 1)
+        self.ftest('log(e, None)', math.log(math.e, None), 1)
         self.ftest('log(32,2)', math.log(32,2), 5)
         self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
         self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
diff --git a/Misc/NEWS.d/next/Library/2023-01-16-10-42-58.gh-issue-89381.lM2WL0.rst b/Misc/NEWS.d/next/Library/2023-01-16-10-42-58.gh-issue-89381.lM2WL0.rst
new file mode 100644 (file)
index 0000000..7bffe7d
--- /dev/null
@@ -0,0 +1 @@
+:func:`~math.log` and :func:`~cmath.log` support default base=None values.
index b1da9452c61db88120761be009a1c0e18b5c8149..bc91c20f373bcdfe5c635e5abfcce6571dc4b454 100644 (file)
@@ -639,12 +639,13 @@ exit:
 }
 
 PyDoc_STRVAR(cmath_log__doc__,
-"log($module, z, base=<unrepresentable>, /)\n"
+"log($module, z, base=None, /)\n"
 "--\n"
 "\n"
 "log(z[, base]) -> the logarithm of z to the given base.\n"
 "\n"
-"If the base not specified, returns the natural logarithm (base e) of z.");
+"If the base is not specified or is None, returns the\n"
+"natural logarithm (base e) of z.");
 
 #define CMATH_LOG_METHODDEF    \
     {"log", _PyCFunction_CAST(cmath_log), METH_FASTCALL, cmath_log__doc__},
@@ -657,7 +658,7 @@ cmath_log(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
 {
     PyObject *return_value = NULL;
     Py_complex x;
-    PyObject *y_obj = NULL;
+    PyObject *y_obj = Py_None;
 
     if (!_PyArg_CheckPositional("log", nargs, 1, 2)) {
         goto exit;
@@ -982,4 +983,4 @@ skip_optional_kwonly:
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=0146c656e67f5d5f input=a9049054013a1b77]*/
+/*[clinic end generated code: output=2630f8740909a8f7 input=a9049054013a1b77]*/
index 1f9725883b98202479fcb9d132122a265802d73c..0d61fd1be38ddb8440d2e3bcbd62fcbc4b138fcc 100644 (file)
@@ -187,43 +187,37 @@ exit:
 }
 
 PyDoc_STRVAR(math_log__doc__,
-"log(x, [base=math.e])\n"
+"log($module, x, base=None, /)\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.");
+"If the base is not specified or is None, returns the natural\n"
+"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;
+    PyObject *base = Py_None;
 
-    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 +948,4 @@ math_ulp(PyObject *module, PyObject *arg)
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=899211ec70e4506c input=a9049054013a1b77]*/
+/*[clinic end generated code: output=afec63ebb0da709a input=a9049054013a1b77]*/
index 2038ac26e65857854e3a06bcffa8b9f906117b7e..62caba031eda2754488aadd98fcd9c106503af57 100644 (file)
@@ -952,23 +952,24 @@ 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 = None
     /
 
 log(z[, base]) -> the logarithm of z to the given base.
 
-If the base not specified, returns the natural logarithm (base e) of z.
+If the base is not specified or is None, 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=e7db51859ebf70bf]*/
 {
     Py_complex y;
 
     errno = 0;
     x = c_log(x);
-    if (y_obj != NULL) {
+    if (y_obj != Py_None) {
         y = PyComplex_AsCComplex(y_obj);
         if (PyErr_Occurred()) {
             return NULL;
index 481e29915464f113279849c711fa07b7f45edbb6..e6cdb3bae1ecff19a468836af9c3f0a2635b77e3 100644 (file)
@@ -2366,26 +2366,24 @@ loghelper(PyObject* arg, double (*func)(double))
 math.log
 
     x:    object
-    [
-    base: object(c_default="NULL") = math.e
-    ]
+    base: object = None
     /
 
 Return the logarithm of x to the given base.
 
-If the base not specified, returns the natural logarithm (base e) of x.
+If the base is not specified or is None, 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=ef032cc9837943e1]*/
 {
     PyObject *num, *den;
     PyObject *ans;
 
     num = loghelper(x, m_log);
-    if (num == NULL || base == NULL)
+    if (num == NULL || base == Py_None)
         return num;
 
     den = loghelper(base, m_log);