diff --git a/Lib/test/test_cmath.py b/Lib/test/test_cmath.py index c13e3aff7b4e9a..62769561e821d2 100644 --- a/Lib/test/test_cmath.py +++ b/Lib/test/test_cmath.py @@ -406,6 +406,22 @@ 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): self.assertAlmostEqual(phase(0), 0.) self.assertAlmostEqual(phase(1.), 0.) 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 00000000000000..af4a2dad959cf9 --- /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 6e2d8e60c99e97..d0f6bf0a68972d 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; }