Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_cmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

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.

# 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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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)``.

1 change: 0 additions & 1 deletion Modules/cmathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,6 @@ c_log(Py_complex z)
}
}
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.

return r;
}

Expand Down
Loading