gh-70450: Fix MSVC /fp:strict builds - #154849
Open
yhay81 wants to merge 2 commits into
Open
Conversation
Replace floating-point expressions that MSVC rejects in static initializers under strict semantics. Initialize cmath NaN table entries once because NAN is not a constant expression in MSVC C mode.
yhay81
marked this pull request as ready for review
July 29, 2026 05:51
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #70450.
Summary
cmathspecial-value tables read-only and decode their finite NaN sentinel when a value is readmathandcmathNaN constants withnan(""), avoiding floating-point exceptions during strict-mode importsRoot cause
MSVC's strict floating-point mode does not accept the affected arithmetic expressions as static initializers. In C mode, the UCRT
NANexpansion also performs runtime floating-point arithmetic under/fp:strict; using it during module initialization setFE_INVALID | FE_OVERFLOW | FE_INEXACT.The
cmathtables therefore retain a finite sentinel in static storage and convert only the selected result to a quiet NaN withnan(""). This keeps the tablesconst, avoids process-global initialization state, and preserves two-dimensional array bounds.Validation
PCbuild/pythoncore.vcxprojin Debug x64 withICCBuild=true(selectingFloatingPointModel=Strict): passedmathandcmath: both remained0x0, with errno 0python_d.exe -m test --timeout 300 -j1 test_math test_cmath test_float test_strtod: 186 tests passed, 4 skippedTools/c-analyzer/check-c-globals.py --format summary --traceback Modules/cmathmodule.cwith the GCC preprocessor: passedTools/patchcheck/patchcheck.py, andgit diff --check: passedAI assistance: This patch was developed with assistance from OpenAI Codex; I reviewed and tested the final changes.