Skip to content

gh-156886: Fix spurious ZeroDivisionError for complex powers - #156887

Merged
serhiy-storchaka merged 2 commits into
python:mainfrom
serhiy-storchaka:complex-pow-edom
Sep 4, 2026
Merged

gh-156886: Fix spurious ZeroDivisionError for complex powers#156887
serhiy-storchaka merged 2 commits into
python:mainfrom
serhiy-storchaka:complex-pow-edom

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Sep 3, 2026

Copy link
Copy Markdown
Member

cos() and sin() of an infinite phase set errno to EDOM, which complex_pow() reported as a zero base.

🤖 Generated with Claude Code

cos() and sin() of an infinite phase set errno to EDOM, which
complex_pow() reported as a zero base.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@serhiy-storchaka

Copy link
Copy Markdown
Member Author

Not sure that this is the best solution, but it is the simplest.

@skirpichev skirpichev added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Sep 3, 2026
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @skirpichev for commit 08cc039 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F156887%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Sep 3, 2026

@skirpichev skirpichev left a comment

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.

LGTM, one minor comment.

Comment thread Objects/complexobject.c Outdated
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
@serhiy-storchaka
serhiy-storchaka enabled auto-merge (squash) September 4, 2026 07:35
@serhiy-storchaka
serhiy-storchaka merged commit 5f7d709 into python:main Sep 4, 2026
53 of 54 checks passed
@hpkfft

hpkfft commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

This PR has the side effect that the public C API function _Py_c_pow() now sets errno = 0; on success (except if either the base or the exponent is zero, in which case, on success, errno is left unchanged). The old code, on success, when neither argument was zero, did set errno to zero if it was previously ERANGE, but if it was, e.g., ENOENT, then it left it unchanged. Well, except if the real or imaginary part of either the base or the exponent was not finite, in which case, on success, ERANGE was also left unchanged.

The Linux man page for errno says, "The value of errno is never set to zero by any system call or library function."
Of course, Python does not have to abide by this restriction, but then it might be good to update the documentation for _Py_c_pow() to mention that errno may or may not be set to zero on success. See also: #156914

When I merged main into #156968 this change (setting errno to zero) was removed, so merging that PR is another option available to you. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants