Repository navigation
gh-153144: Avoid checking errno for atan2 - #153148
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
If this PR is merged, it looks to me like #146402 can (and should) be reverted. |
skirpichev
left a comment
There was a problem hiding this comment.
No, I doubt that this is a right approach. We must handle error in same way for all libm functions.
Perhaps, m_atan2() (like m_log1p() we have currently) could be restored (see #122681) to workaround broken platform functions.
The mathmodule currently has 3 ways of handling 1-argument libm functions:
Maybe it's not so bad to have |
|
Perhaps, we could modify math_2() helper to check that errno!=EDOM, if inputs and output are finite. When domain error occurs - result should be nan. But I think that a little wrapper for libm's atan2 is better, if we are going to add some workaround for the given issue. |
skirpichev
left a comment
There was a problem hiding this comment.
Please revert unrelated changes.
|
@vstinner, can you look at this at your convenience? |
|
You also could use new helper function for copysign. On another hand, I would prefer just remove errno stuff from math_2(), per #156145. It should be possible for all two-argument functions: atan2, atan2pi, copysign, remainder and fmod (which could utilize same wrapper). |
My thinking is only to do what is necessary to fix the bug (the incorrect results when building with Intel or Solaris math libraries). Then, I would recommend applying this fix to Python 3.14 and 3.15. With that in mind, I don't want to make any enhancements that are not necessary to fix the reported bug.
Yes, 156145 can have a more ambitious goal. Can you trigger the buildbots to run this PR? I am not able to check myself that it works on Solaris. (I do think it will work based on what I read in the Solaris bug report.) |
Then we loose chance of using new helper function for copysign().
!buildbot Solaris |
|
!buildbot Solaris |
|
🤖 New build scheduled with the buildbot fleet by @skirpichev for commit 5b6d005 🤖 Results will be shown at: https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F153148%2Fmerge The command will test the builders whose names match following regular expression: The builders matched are:
|
OK, I'll add that commit tomorrow. Since it will be a separate commit, it can easily be reverted (or skipped when squashing all the commits before the final merge). The same is true for the comments Victor requested--they're in their own separate commit. |
|
Do not click the "Update branch" button without a good reason because it notifies everyone watching the PR that there are new changes, when there are not, and it uses up limited CI resources. |
|
We unfortunately get past 3.13 release and it is not security-only. I am ok with putting it in 3.16 and maybe 3.14/3.15 but I would be interested in having some NumPy's opinion just in case (cc @ngoldbaum). I think fixing the phase/atan2 inconsistency takes precedence over other behavioral change concerns. |
|
Thanks for the ping. NumPy already behaves the way CPython will behave after this PR. The only difference is that NumPy can optionally surface the underflow, and it reports it as an underflow, never as an overflow. |
|
Yeah so this matches what users will expect. I am fine with this PR then! thanks for doing it. |
| /* variant of math_2, to be used when the function being wrapped is known NOT | ||
| to need error checking (i.e., no overflow, invalid, or divide-by-zero). */ |
There was a problem hiding this comment.
I suggest expand this with atan2() example and remove other comments below.
There was a problem hiding this comment.
I'm fine with leaving comments as they are: general comment on math_2ne(), and detailed comments on functions using math_2ne() below.
| C89 but for which HUGE_VAL is not an infinity. | ||
| For most two-argument functions (copysign, fmod, hypot, atan2) | ||
| For most two-argument functions (fmod, hypot) |
There was a problem hiding this comment.
Actually, the only remaining math_2()-type function is remainder()...
There was a problem hiding this comment.
I think (fmod, hypot) in the comment above refers to the C math library functions.
Unless I've missed it, Python does not use the C math library function double remainder(double, double), so I don't think we should add it to this comment.
There was a problem hiding this comment.
I think (fmod, hypot) in the comment above refers to the C math library functions.
I don't think so. Before 53876d9, both fmod and hypot were using FUNC2 macro.
Python does not use the C math library function double remainder(double, double)
Yes, but m_remainder (name in style m_log and co, suggesting a workarounded libm's function) is just an implementation of Annex F remainder.
Perhaps, nowadays we could replace this function by standard library call: at least this works well for me locally. See previous attempt of removing legacy stuff: #101679
Edit: FYI, #158826.
There was a problem hiding this comment.
Interesting. But it seems sufficiently unrelated as to allow this PR to be merged without further tweaks to the comments.
There was a problem hiding this comment.
I don't understand well if the comment should be updated or not. At least, I can say that with this change, the last math_2() user is FUNC2(remainder, m_remainder, ...). math.fmod() and math.hypot() no longer use it.
There was a problem hiding this comment.
Previously, I had re-flowed this comment to avoid the first line looking silly for being short, and I was asked to revert that change, which I did.
This PR does not affect fmod, hypot, or remainder. It seems inconsistent to consider a substantive change to the comment as part of this PR if a white-space change was frowned upon.
|
@Yhg1s, please consider whether you'd like this PR made into a patch for 3.13. I see that:
Using Linux/GNU, and taking a complex point The correct phase angle for I cannot resist mentioning that the Ariane 5 launch failure might have been avoided if, instead of an exception, the calculated value had saturated, or wrapped, or been set to zero, etc. The result was not used after lift-off, so the return value didn't matter. But since exceptions that ought not to happen cause crashes, .... |
Unexpected exceptions aren't security vulnerabilities, they're regular bugs. I don't see a reason, or any user reports, to backport this. |
vstinner
left a comment
There was a problem hiding this comment.
LGTM.
Honestly, I don't understand the subtle details of this change, but overall it looks good to me.
vstinner
left a comment
There was a problem hiding this comment.
@skirpichev: Are you fine with the current PR or do you still have concerns?
Yes, most of my comments are still valid, e.g. this thread or this (where edited comment is just misleading now). I think that we shouldn't add references to issue in the code. Or add specific comments on concrete FUNC2NE use cases. We could drop math_2(), as it's used just for one function. We also could reduce patch for cmath.phase: keep L1007-1008, just drop errno handling. But all that are minor nitpicks, feel free to ignore. |
|
I merged your fix, thanks @hpkfft! This PR changes math and cmath functions in a subtle way, I'm not comfortable to consider them as bugfixes and backport them. Python has the same behavior for several years, users had to learn how to work around Python math/cmath issues anyway in the meanwhile. Backporting the change to 3.14 and 3.15 would change the behavior in bugfix 3.14.x and 3.15.x releases which would be bad.
Ok, I chose to ignore these nitpicks and merge the PR as it is to unblock this fix. You can open a follow-up PR. For example, "We could drop math_2()" would be better done as a separated change, since it's not directly directly to this errno/OverflowError change. |
The C23 standard states that for
atan2andatan2pi:Since Python should not raise ValueError in either of these cases (i.e., when both arguments are zero or when the computation underflows), this PR avoids checking
errnowhen calling these trig functions. As a bonus,math.atan2()is about 4% faster.math.atan2(0.0, 0.0)andcmath.phase(0.0)using icx #153144The statement about range error in the standard should, I think, be interpreted as: