From f37edf9675e101a994c3acea0cc734fe20f8f8e8 Mon Sep 17 00:00:00 2001 From: Tanveer Date: Sat, 14 Nov 2026 12:26:47 +0530 Subject: [PATCH 1/2] gh-161972: Fix cmath.log(0, base) returning (-inf+nanj) instead of raising ValueError --- Lib/test/test_cmath.py | 19 +++++++++++++++++++ ...-11-14-12-00-00.gh-issue-161972.YkfZJZ.rst | 1 + Modules/cmathmodule.c | 8 ++++++++ 3 files changed, 28 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-14-12-00-00.gh-issue-161972.YkfZJZ.rst diff --git a/Lib/test/test_cmath.py b/Lib/test/test_cmath.py index a986fd6b892bd27..df53d60d194dccb 100644 --- a/Lib/test/test_cmath.py +++ b/Lib/test/test_cmath.py @@ -406,6 +406,25 @@ 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. The domain error from + # c_log(0) used to be lost when the second c_log(base) call + # reset errno to 0. + 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) + # Normal two-argument logs still work. + self.assertEqual(cmath.log(2, 2), 1+0j) + self.assertEqual(cmath.log(8, 2), 3+0j) + @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..a5ebf5c8c7fd9e5 --- /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 base: ``cmath.log(0, base)`` now raises :exc:`ValueError` instead of returning ``(-inf+nanj)``. The domain error from the first logarithm was lost when the second call reset ``errno``. diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c index e756b550e7753e1..8cb23d18ff2c6ae 100644 --- a/Modules/cmathmodule.c +++ b/Modules/cmathmodule.c @@ -957,15 +957,23 @@ cmath_log_impl(PyObject *module, Py_complex x, PyObject *y_obj) /*[clinic end generated code: output=4effdb7d258e0d94 input=eb25de0757baf4a0]*/ { Py_complex y; + int errno_save; errno = 0; x = c_log(x); if (y_obj != NULL) { + /* Save the error from c_log(x): the second c_log() call resets + errno to 0 on success, which would hide a domain error from + the first call (gh-161972). */ + errno_save = errno; y = PyComplex_AsCComplex(y_obj); if (PyErr_Occurred()) { return NULL; } y = c_log(y); + if (errno_save != 0) { + errno = errno_save; + } x = _Py_c_quot(x, y); } if (errno != 0) From f65bff828f69ecb87094d0cc9c4ffe8060b141a3 Mon Sep 17 00:00:00 2001 From: Tanveer Date: Sun, 15 Nov 2026 00:01:33 +0530 Subject: [PATCH 2/2] gh-161972: Fix cmath.log(0, base) in c_log() instead of cmath_log_impl --- Lib/test/test_cmath.py | 7 +------ .../2026-11-14-12-00-00.gh-issue-161972.YkfZJZ.rst | 2 +- Modules/cmathmodule.c | 9 --------- 3 files changed, 2 insertions(+), 16 deletions(-) diff --git a/Lib/test/test_cmath.py b/Lib/test/test_cmath.py index df53d60d194dccb..be6530210d2e991 100644 --- a/Lib/test/test_cmath.py +++ b/Lib/test/test_cmath.py @@ -408,9 +408,7 @@ def polar_with_errno_set(z): 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. The domain error from - # c_log(0) used to be lost when the second c_log(base) call - # reset errno to 0. + # cmath.log(0) and math.log(0, base) do. with self.assertRaises(ValueError): cmath.log(0) for base in (2, -1, 0, 1, 10): @@ -421,9 +419,6 @@ def test_log_zero_with_base(self): cmath.log(2, 0) with self.assertRaises(ValueError): cmath.log(2, 1) - # Normal two-argument logs still work. - self.assertEqual(cmath.log(2, 2), 1+0j) - self.assertEqual(cmath.log(8, 2), 3+0j) @unittest.skipIf(sys.platform.startswith("sunos"), "skipping, see gh-138573") 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 index a5ebf5c8c7fd9e5..af4a2dad959cf94 100644 --- 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 @@ -1 +1 @@ -Fix :func:`cmath.log` with a base: ``cmath.log(0, base)`` now raises :exc:`ValueError` instead of returning ``(-inf+nanj)``. The domain error from the first logarithm was lost when the second call reset ``errno``. +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 8cb23d18ff2c6ae..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; } @@ -957,23 +956,15 @@ cmath_log_impl(PyObject *module, Py_complex x, PyObject *y_obj) /*[clinic end generated code: output=4effdb7d258e0d94 input=eb25de0757baf4a0]*/ { Py_complex y; - int errno_save; errno = 0; x = c_log(x); if (y_obj != NULL) { - /* Save the error from c_log(x): the second c_log() call resets - errno to 0 on success, which would hide a domain error from - the first call (gh-161972). */ - errno_save = errno; y = PyComplex_AsCComplex(y_obj); if (PyErr_Occurred()) { return NULL; } y = c_log(y); - if (errno_save != 0) { - errno = errno_save; - } x = _Py_c_quot(x, y); } if (errno != 0)