diff --git a/Lib/test/test_cmath.py b/Lib/test/test_cmath.py index a986fd6b892bd27..be6530210d2e991 100644 --- a/Lib/test/test_cmath.py +++ b/Lib/test/test_cmath.py @@ -406,6 +406,20 @@ def polar_with_errno_set(z): _testcapi.set_errno(0) self.check_polar(polar_with_errno_set) + def test_log_zero_with_base(self): + # gh-161972: cmath.log(0, base) must raise ValueError, like + # cmath.log(0) and math.log(0, base) do. + with self.assertRaises(ValueError): + cmath.log(0) + for base in (2, -1, 0, 1, 10): + with self.assertRaises(ValueError, msg=f"base={base}"): + cmath.log(0, base) + # A base of 0 or 1 still raises for a nonzero argument. + with self.assertRaises(ValueError): + cmath.log(2, 0) + with self.assertRaises(ValueError): + cmath.log(2, 1) + @unittest.skipIf(sys.platform.startswith("sunos"), "skipping, see gh-138573") def test_phase(self): diff --git a/Misc/NEWS.d/next/Library/2026-11-14-12-00-00.gh-issue-161972.YkfZJZ.rst b/Misc/NEWS.d/next/Library/2026-11-14-12-00-00.gh-issue-161972.YkfZJZ.rst new file mode 100644 index 000000000000000..af4a2dad959cf94 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-14-12-00-00.gh-issue-161972.YkfZJZ.rst @@ -0,0 +1 @@ +Fix :func:`cmath.log` with a nonzero base: ``cmath.log(0, base)`` now raises :exc:`ValueError` instead of returning ``(-inf+nanj)``. diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c index e756b550e7753e1..8b234066dec8371 100644 --- a/Modules/cmathmodule.c +++ b/Modules/cmathmodule.c @@ -634,7 +634,6 @@ c_log(Py_complex z) } } r.imag = atan2(z.imag, z.real); - errno = 0; return r; }