Skip to content

gh-89381: Fix invalid signatures of math/cmath.log #101404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/library/cmath.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions Doc/library/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,7 @@ def testLog(self):
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:func:`~math.log` and :func:`~cmath.log` support default base=None values.
9 changes: 5 additions & 4 deletions Modules/clinic/cmathmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 19 additions & 25 deletions Modules/clinic/mathmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions Modules/cmathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 6 additions & 8 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down