Skip to content

gh-161972: Fix cmath.log(0, base) returning (-inf+nanj) instead of raising ValueError - #161986

Open
tanveer-arch wants to merge 3 commits into
python:mainfrom
tanveer-arch:fix-cmath-log-zero-base
Open

tanveer-arch wants to merge 3 commits into
python:mainfrom
tanveer-arch:fix-cmath-log-zero-base

Conversation

@tanveer-arch

@tanveer-arch tanveer-arch commented Nov 14, 2026 •

Copy link
Copy Markdown

Fixes #161972.

cmath.log(0) raises ValueError, but cmath.log(0, 2) returned (-inf+nanj). The domain error from c_log(x) was stored in errno, and the second call c_log(y) reset errno to 0 on success before the check, so the error was lost.

The fix saves errno after the first c_log() call and restores it after the second one, following the same save/restore pattern cmath_log10_impl already uses. cmath.log(0, base) now raises ValueError for any base, and a base of 0 or 1 still raises for a nonzero argument. Normal two-argument logs are unchanged (cmath.log(2, 2) == 1+0j).

Added a regression test test_log_zero_with_base in Lib/test/test_cmath.py and a news entry.

I used an AI assistant to help draft this change and reviewed it in detail; I understand the fix and can explain it.

@skirpichev skirpichev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's a wrong place to fix.

c_log() is the problem. In libc no library call set errno to zero.

Comment thread Lib/test/test_cmath.py Resolved
Comment thread Lib/test/test_cmath.py Resolved
@tanveer-arch

Copy link
Copy Markdown
Author

Thanks, you're right the fix belongs in c_log(), not in cmath_log_impl. I've moved it there: c_log() no longer clears errno on success (like libc, it now only sets errno on error), so the EDOM from c_log(0) survives the second c_log(base) call, and the save/restore workaround in cmath_log_impl is gone. I also applied all three of your suggestions shortened test comment, removed the redundant log(2, 2) / log(8, 2) assertions, and reworded the news entry. All 34 test_cmath tests pass and CI is green. Could you take another look?

@@ -0,0 +1 @@
Fix :func:`cmath.log` with a nonzero base: ``cmath.log(0, base)`` now raises :exc:`ValueError` instead of returning ``(-inf+nanj)``.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Fix :func:`cmath.log` with a nonzero base: ``cmath.log(0, base)`` now raises :exc:`ValueError` instead of returning ``(-inf+nanj)``.
Fix :func:`cmath.log` with a nonzero base: ``cmath.log(0, base)`` now raises
:exc:`ValueError` instead of returning ``(-inf+nanj)``.

Comment thread Modules/cmathmodule.c
}
}
r.imag = atan2(z.imag, z.real);
errno = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to leave c_log() unchanged: c_xxx() functions are documented to set errno to 0 on success, see #161972 (comment).

Instead, check errno after the first c_log() call in cmath_log_impl(). Something like:

{
    x = c_log(x);
    if (errno != 0) {
        return math_error();
    }

    if (y_obj != NULL) {
        Py_complex y = PyComplex_AsCComplex(y_obj);
        if (PyErr_Occurred()) {
            return NULL;
        }

        y = c_log(y);
        if (errno != 0) {
            return math_error();
        }

        x = _Py_c_quot(x, y);
    }
    return PyComplex_FromCComplex(x);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like:

_Py_c_quot() also can set errno.

Comment thread Lib/test/test_cmath.py
_testcapi.set_errno(0)
self.check_polar(polar_with_errno_set)

def test_log_zero_with_base(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmath.log(2, 0) doesn't call cmath.log(0, base), so I suggest to rename the test to:

Suggested change
def test_log_zero_with_base(self):
def test_log_with_base(self):

I would prefer to add the new test at the end of the CMathTests class.

@vstinner

Copy link
Copy Markdown
Member

@skirpichev:

c_log() is the problem. In libc no library call set errno to zero.

Ah? But the comment that you cited at #161972 (comment) says that c_xxx() functions set errno on success. Does it mean that the comment is outdated?

@skirpichev

Copy link
Copy Markdown
Member

But the comment that you cited at #161972 (comment) says that c_xxx() functions set errno on success. Does it mean that the comment is outdated?

Strictly speaking, it's about module functions. And this is still be valid for cmath_log_impl().

But I think that better would be, if we remove settings of errno to 0 in module functions. The Py_complex_protected_converter already do this. I think AC should do this before any call to *_impl() functions. And they - should set only nonzero errno.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cmath.log(0, base) returns (-inf+nanj) instead of raising ValueError

3 participants