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

gh-155526: correct errno handling in complex_abs() by skirpichev · Pull Request #155527 · python/cpython · GitHub

Repository navigation

gh-155526: correct errno handling in complex_abs() - #155527

Merged
vstinner merged 23 commits into
python:mainfrom
skirpichev:complex-abs-errno/155526
Sep 11, 2026
Merged

vstinner merged 23 commits into
python:mainfrom
skirpichev:complex-abs-errno/155526

Conversation

skirpichev commented Aug 11, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

Comment thread Lib/test/test_complex.py Outdated
skirpichev requested a review from aisk August 19, 2026 05:34

Copy link
Copy Markdown
Member Author

CC @vstinner

Comment thread Objects/complexobject.c

skirpichev commented Aug 31, 2026 •
edited
Loading

Copy link
Copy Markdown
Member Author

I decided to use alternative approach, ignoring errno, as proposed in #156145. This is in a separate commit and can be easily reverted.

Edit: this will benefit from new private API in #156694. So, I would appreciate if that pr will be reviewed/merged first.

skirpichev removed the needs backport to 3.15 bugs and security fixes label Aug 31, 2026
Comment thread Objects/complexobject.c Outdated
Comment thread Objects/complexobject.c

Copy link
Copy Markdown
Member

This breaks cmath.polar() for an underflowing atan2():

>>> import cmath
>>> cmath.polar(complex(1e300, 1e-320))
Traceback (most recent call last):
  ...
OverflowError: math range error

atan2(1e-320, 1e300) underflows and sets errno to ERANGE. Currently _Py_c_abs() resets it to 0, so cmath_polar_impl() does not see it. cmath.phase() fails this way already -- gh-153144.

Copy link
Copy Markdown
Member Author

@serhiy-storchaka, are you suggesting (1) reversion of errno = 0 stuff or (2) you are ok with moving errno = 0 down in cmath_polar_impl()?

Comment thread Lib/test/test_capi/test_complex.py Outdated

Copy link
Copy Markdown
Member Author

I understand that this change fix a bug in math.isnan() if errno is non-zero.

It fixes abs(complex(nan)), when errno was set.

Would it make sense to backport the fix to 3.13-3.15 branches, without the C API change?

Looks to be a very minor issue. Though, I can revert all C-API changes for a separate PR.

Comment thread Doc/c-api/complex.rst Outdated

Copy link
Copy Markdown
Member

Looks to be a very minor issue. Though, I can revert all C-API changes for a separate PR.

Oh no, there is no need to revert C API changes in this PR.

skirpichev requested a review from vstinner September 10, 2026 23:20

vstinner 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. Thanks for the updated PR. Errno is now tested by all C _Py_c_abs() tests. There is also a test on Python abs() setting/checking errno. The changes are now well documented in Changelog entries and in the documentation. Good!

vstinner merged commit e5d4fa2 into python:main Sep 11, 2026
102 of 104 checks passed

Copy link
Copy Markdown
Member

Merged, thanks for the fix, and sorry for being picky on documenting the change :-D

@skirpichev: So what do you think of backporting the fix to Python 3.15 and older without changing _Py_c_abs() API?

Copy link
Copy Markdown
Member

For the backport, I suppose that we can do simply what the issue title says:

"complex_abs() should call _Py_c_abs() with errno=0 set": just set errno to 0 before calling _Py_c_abs().

skirpichev deleted the complex-abs-errno/155526 branch September 11, 2026 22:49
skirpichev added a commit to skirpichev/cpython that referenced this pull request Sep 11, 2026
…55527)

abs(complex) no longer raises OverflowError if errno was set to ERANGE
by some library call but abs() doesn't overflow.

(cherry picked from commit e5d4fa2)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: hpkfft.com <paul@hpkfft.com>

bedevere-app Bot commented Sep 11, 2026

Copy link
Copy Markdown

GH-157341 is a backport of this pull request to the 3.15 branch.

Copy link
Copy Markdown
Member Author

So what do you think of backporting the fix to Python 3.15 and older

Should I create backports for <3.15 by hand?

clin1234 pushed a commit to clin1234/cpython that referenced this pull request Sep 12, 2026
abs(complex) no longer raises OverflowError if errno was set to ERANGE
by some library call but abs() doesn't overflow.

_Py_c_abs() no longer sets errno to zero on success, but rather leaves it
unchanged.

Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: hpkfft.com <paul@hpkfft.com>

Copy link
Copy Markdown
Member

Thanks for the backport to 3.15. Once it will be merged (after Python 3.15.0 final release), automated backported to 3.13 and 3.14 will be created. No need to backport again by hand.

johnslavik pushed a commit to johnslavik/cpython that referenced this pull request Sep 14, 2026
abs(complex) no longer raises OverflowError if errno was set to ERANGE
by some library call but abs() doesn't overflow.

_Py_c_abs() no longer sets errno to zero on success, but rather leaves it
unchanged.

Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: hpkfft.com <paul@hpkfft.com>
hugovk pushed a commit that referenced this pull request Sep 15, 2026
)

Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: hpkfft.com <paul@hpkfft.com>
hugovk pushed a commit that referenced this pull request Sep 15, 2026
…57341) (#157555)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: hpkfft.com <paul@hpkfft.com>
vstinner added a commit that referenced this pull request Sep 23, 2026
) (#157558)

[3.15] gh-155526: Don't check errno in abs(complex) (GH-155527) (#157341)



(cherry picked from commit 340cfba)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: hpkfft.com <paul@hpkfft.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.

5 participants


Back | FazBrowse Home | New Git URL