Skip to content

Commit f65bff8

Browse files
committed
gh-161972: Fix cmath.log(0, base) in c_log() instead of cmath_log_impl
1 parent f37edf9 commit f65bff8

3 files changed

Lines changed: 2 additions & 16 deletions

File tree

‎Lib/test/test_cmath.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,7 @@ def polar_with_errno_set(z):
408408

409409
def test_log_zero_with_base(self):
410410
# gh-161972: cmath.log(0, base) must raise ValueError, like
411-
# cmath.log(0) and math.log(0, base) do. The domain error from
412-
# c_log(0) used to be lost when the second c_log(base) call
413-
# reset errno to 0.
411+
# cmath.log(0) and math.log(0, base) do.
414412
with self.assertRaises(ValueError):
415413
cmath.log(0)
416414
for base in (2, -1, 0, 1, 10):
@@ -421,9 +419,6 @@ def test_log_zero_with_base(self):
421419
cmath.log(2, 0)
422420
with self.assertRaises(ValueError):
423421
cmath.log(2, 1)
424-
# Normal two-argument logs still work.
425-
self.assertEqual(cmath.log(2, 2), 1+0j)
426-
self.assertEqual(cmath.log(8, 2), 3+0j)
427422

428423
@unittest.skipIf(sys.platform.startswith("sunos"),
429424
"skipping, see gh-138573")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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``.
1+
Fix :func:`cmath.log` with a nonzero base: ``cmath.log(0, base)`` now raises :exc:`ValueError` instead of returning ``(-inf+nanj)``.

‎Modules/cmathmodule.c‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,6 @@ c_log(Py_complex z)
634634
}
635635
}
636636
r.imag = atan2(z.imag, z.real);
637-
errno = 0;
638637
return r;
639638
}
640639

@@ -957,23 +956,15 @@ cmath_log_impl(PyObject *module, Py_complex x, PyObject *y_obj)
957956
/*[clinic end generated code: output=4effdb7d258e0d94 input=eb25de0757baf4a0]*/
958957
{
959958
Py_complex y;
960-
int errno_save;
961959

962960
errno = 0;
963961
x = c_log(x);
964962
if (y_obj != NULL) {
965-
/* Save the error from c_log(x): the second c_log() call resets
966-
errno to 0 on success, which would hide a domain error from
967-
the first call (gh-161972). */
968-
errno_save = errno;
969963
y = PyComplex_AsCComplex(y_obj);
970964
if (PyErr_Occurred()) {
971965
return NULL;
972966
}
973967
y = c_log(y);
974-
if (errno_save != 0) {
975-
errno = errno_save;
976-
}
977968
x = _Py_c_quot(x, y);
978969
}
979970
if (errno != 0)

0 commit comments

Comments
 (0)