FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-156886: Fix spurious ZeroDivisionError for complex powers by serhiy-storchaka · Pull Request #156887 · python/cpython · GitHub

Repository navigation

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

serhiy-storchaka merged 2 commits into
python:mainfrom
serhiy-storchaka:complex-pow-edom

Conversation

serhiy-storchaka commented Sep 3, 2026 •
edited
Loading

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>

Copy link
Copy Markdown
Member Author

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

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

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 removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Sep 3, 2026

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM, one minor comment.

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

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. 😉

Copy link
Copy Markdown
Member

This PR has the side effect that the public C API function _Py_c_pow() now sets errno = 0; on success
The Linux man page for errno says, "The value of errno is never set to zero by any system call or library function."

I don't think this poses practical problems: we shouldn't match libc behavior, it also still correct wrt docs (they say errno is set on overflows, everything else is unspecified). Correct workflow is (as before):

errno = 0;
result = _Py_c_pow(base, exp);
if (errno == ERANGE) {
    ...

Though, I would prefer same errno handling as for libc function, i.e. restore saved errno on success. I can reopen issue or include a fix to #155527.

skirpichev self-assigned this Sep 6, 2026
clin1234 pushed a commit to clin1234/cpython that referenced this pull request Sep 12, 2026
…ythonGH-156887)

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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL