Skip to content

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

Open
skirpichev wants to merge 17 commits into
python:mainfrom
skirpichev:complex-abs-errno/155526
Open

gh-155526: correct errno handling in complex_abs()#155527
skirpichev wants to merge 17 commits into
python:mainfrom
skirpichev:complex-abs-errno/155526

Conversation

@skirpichev

@skirpichev skirpichev commented Aug 11, 2026

Copy link
Copy Markdown
Member

@skirpichev skirpichev added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Aug 11, 2026
Comment thread Lib/test/test_complex.py Outdated
@skirpichev
skirpichev requested a review from aisk August 19, 2026 05:34
@skirpichev

Copy link
Copy Markdown
Member Author

CC @vstinner

Comment thread Objects/complexobject.c
@skirpichev

skirpichev commented Aug 31, 2026

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 skirpichev removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Aug 31, 2026
Comment thread Objects/complexobject.c Outdated
Comment thread Objects/complexobject.c
@serhiy-storchaka

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.

@skirpichev

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 Misc/NEWS.d/next/Core_and_Builtins/2026-08-11-06-04-26.gh-issue-155526.W7ZHXu.rst Outdated
Comment thread Lib/test/test_capi/test_complex.py Outdated
@skirpichev
skirpichev requested a review from vstinner September 8, 2026 01:44
Comment thread Lib/test/test_capi/test_complex.py Outdated
@read-the-docs-community

read-the-docs-community Bot commented Sep 9, 2026

Copy link
Copy Markdown

@skirpichev

Copy link
Copy Markdown
Member Author

Just FYI, GH code search has no hits for _Py_c_abs(), except for CPython forks. Victor did PyPI search before for all soft-deprecated complex C-API: one hit for _Py_c_pow() in Numba.

@vstinner 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.

The change mostly LGTM. I just have a last request about the change documentation.

I understand that this change fix a bug in math.isnan() if errno is non-zero. Would it make sense to backport the fix to 3.13-3.15 branches, without the C API change?

Comment thread Doc/whatsnew/3.16.rst

* :c:func:`_Py_c_abs` no longer sets :c:data:`errno` to zero on success,
rather leave it unchanged.
(Contributed by Sergey B Kirpichev in :gh:`155526`.)

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.

Would you mind to copy that in second C API Changelog (NEWS) entry?

Comment thread Doc/c-api/complex.rst Outdated
Comment thread Doc/whatsnew/3.16.rst Outdated
vstinner and others added 2 commits September 9, 2026 18:24
Co-authored-by: hpkfft.com <paul@hpkfft.com>
Co-authored-by: hpkfft.com <paul@hpkfft.com>
@vstinner

vstinner commented Sep 9, 2026

Copy link
Copy Markdown
Member

I applied the 2 @hpkfft's suggestions (fixing typos).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants