-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-97786: Fix compiler warnings in pytime.c #101826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-97786: Fix compiler warnings in pytime.c #101826
Conversation
|
🤖 New build scheduled with the buildbot fleet by @mdickinson for commit 32aee25 🤖 If you want to schedule another build, you need to add the |
|
I've removed the The only possible issue with a non-integer
So the failure mode is benign. But in any case, |
|
I think this is ready to merge. @gpshead Do you have bandwidth for a quick re-review? |
|
Thanks @mdickinson for the PR, and @gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.11. |
|
Sorry, @mdickinson and @gpshead, I could not cleanly backport this to |
Fixes compiler warnings in pytime.c. (cherry picked from commit b1b375e) Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
|
GH-102062 is a backport of this pull request to the 3.11 branch. |
Fixes compiler warnings in pytime.c.
* main: (60 commits) pythongh-102056: Fix a few bugs in error handling of exception printing code (python#102078) pythongh-102011: use sys.exception() instead of sys.exc_info() in docs where possible (python#102012) pythongh-101566: Sync with zipp 3.14. (pythonGH-102018) pythonGH-99818: improve the documentation for zipfile.Path and Traversable (pythonGH-101589) pythongh-88233: zipfile: handle extras after a zip64 extra (pythonGH-96161) pythongh-101981: Apply HOMEBREW related environment variables (pythongh-102074) pythongh-101907: Stop using `_Py_OPCODE` and `_Py_OPARG` macros (pythonGH-101912) pythongh-101819: Adapt _io types to heap types, batch 1 (pythonGH-101949) pythongh-101981: Build macOS as recommended by the devguide (pythonGH-102070) pythongh-97786: Fix compiler warnings in pytime.c (python#101826) pythongh-101578: Amend PyErr_{Set,Get}RaisedException docs (python#101962) Misc improvements to the float tutorial (pythonGH-102052) pythongh-85417: Clarify behaviour on branch cuts in cmath module (python#102046) pythongh-100425: Update tutorial docs related to sum() accuracy (FH-101854) Add missing 'is' to `cmath.log()` docstring (python#102049) pythongh-100210: Correct the comment link for unescaping HTML (python#100212) pythongh-97930: Also include subdirectory in makefile. (python#102030) pythongh-99735: Use required=True in argparse subparsers example (python#100927) Fix incorrectly documented attribute in csv docs (python#101250) pythonGH-84783: Make the slice object hashable (pythonGH-101264) ...
|
GH-102150 is a backport of this pull request to the 3.10 branch. |
* main: (225 commits) pythongh-102056: Fix a few bugs in error handling of exception printing code (python#102078) pythongh-102011: use sys.exception() instead of sys.exc_info() in docs where possible (python#102012) pythongh-101566: Sync with zipp 3.14. (pythonGH-102018) pythonGH-99818: improve the documentation for zipfile.Path and Traversable (pythonGH-101589) pythongh-88233: zipfile: handle extras after a zip64 extra (pythonGH-96161) pythongh-101981: Apply HOMEBREW related environment variables (pythongh-102074) pythongh-101907: Stop using `_Py_OPCODE` and `_Py_OPARG` macros (pythonGH-101912) pythongh-101819: Adapt _io types to heap types, batch 1 (pythonGH-101949) pythongh-101981: Build macOS as recommended by the devguide (pythonGH-102070) pythongh-97786: Fix compiler warnings in pytime.c (python#101826) pythongh-101578: Amend PyErr_{Set,Get}RaisedException docs (python#101962) Misc improvements to the float tutorial (pythonGH-102052) pythongh-85417: Clarify behaviour on branch cuts in cmath module (python#102046) pythongh-100425: Update tutorial docs related to sum() accuracy (FH-101854) Add missing 'is' to `cmath.log()` docstring (python#102049) pythongh-100210: Correct the comment link for unescaping HTML (python#100212) pythongh-97930: Also include subdirectory in makefile. (python#102030) pythongh-99735: Use required=True in argparse subparsers example (python#100927) Fix incorrectly documented attribute in csv docs (python#101250) pythonGH-84783: Make the slice object hashable (pythonGH-101264) ...
Since #101826 was merged, the internal macro `_Py_InIntegralTypeRange` is unused, as are its supporting macros `_Py_IntegralTypeMax` and `_Py_IntegralTypeMin`. This PR removes them. Note that `_Py_InIntegralTypeRange` doesn't actually work as advertised - it's not a safe way to avoid undefined behaviour in an integer to double conversion.
Since python#101826 was merged, the internal macro `_Py_InIntegralTypeRange` is unused, as are its supporting macros `_Py_IntegralTypeMax` and `_Py_IntegralTypeMin`. This PR removes them. Note that `_Py_InIntegralTypeRange` doesn't actually work as advertised - it's not a safe way to avoid undefined behaviour in an integer to double conversion.
|
Thanks for fixing this old annoying compiler bug, @mdickinson! |
|
I added math.nextafter() and math.ulp() to Python 3.9 to help me to understand this issue :-) But then I failed to come up with a fix for this warning. |
|
|
This PR fixes some compiler warnings in
pytime.c, and at the same time fixes our out-of-range double-to-integer checks to properly avoid undefined behaviour.It's hard to write strictly portable code for this kind of thing, but the new check should work under a set of (very) mild assumptions, that are highly unlikely to be violated on any actual platform that we care about:
time_tis a two's complement signed integer type with no trap representation. (The weakest part of this is the assumption thattime_tis a signed integer type at all, but we're already making that assumption.)_PyTime_tis also a two's complement signed integer type with no trap representation. (This is already true with the currenttypedef int64_t _PyTime_t;declaration.)PY_TIME_T_MINand_PyTime_MINare within the range of a C double. These days we're assuming IEEE 754 binary64 doubles, so we're safe so long as the integer types_PyTime_tandtime_tdon't have a width of more than 1024.Here's the underlying logic for the changes, for the record:
xis within the range of a (potentially unknown) signed integer typeIwith min and max valuesIMINandIMAX. More specifically, we want to be sure that a conversion fromxto typeIwill not invoke undefined behaviour; this means that the integer part ofx(discarding the fractional part, if any) must be in[IMIN, IMAX].Iuses two's complement with no trap representation,IMINmust be the negation of a power of two, andIMAX == -1 - IMIN.IMINis exactly representable as adouble(assuming that it's not larger than2**1023), and(double)IMINdoes not change the value.(double)IMIN <= x, does exactly what we want it to.x <= IMAXis problematic since the implicit conversion ofIMAXto typedoublemay change the value. But assuming thatxis an integer (which it is for all cases in this PR),x <= IMAXis mathematically equivalent tox < (IMAX + 1), which with our two's complement assumption is equivalent tox < -IMIN, and we can express this asx < -(double)IMIN._Py_InIntegralTypeRange#97786