Define _USE_MATH_DEFINES before including Python.h - #870
Open
fjankovi wants to merge 1 commit into
Open
Conversation
cwt.template.c defines _USE_MATH_DEFINES immediately before including math.h, but by then math.h has already been included: cwt.c includes cwt.h -> common.h -> Python.h, and Python.h:24 includes <math.h>. The include guard makes the second include a no-op, so on toolchains where M_PI is gated on _USE_MATH_DEFINES the constant is never defined and cwt.template.c fails to compile. Moving the define ahead of the first include fixes it for every TYPE instantiation. Co-Authored-By: Claude Opus 5 <noreply@anthropic.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
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.
✳️ Assisted by Claude Opus 5
cwt.template.c:18defines_USE_MATH_DEFINESimmediately before#include "math.h", but<math.h>has already been included by that point, so the define has no effect:cwt.template.cis not a translation unit of its own — it is textually included fromcwt.c(lines 9 and 13), after that chain has already run. The include guard is set, so line 19 is a no-op andM_PIat line 65 is undeclared.Whether that matters is platform-dependent:
pyconfig.hdefines_XOPEN_SOURCE 700, and glibc exposesM_PIunder__USE_XOPENregardless of_USE_MATH_DEFINES.PC/pyconfig.hdefines none of_XOPEN_SOURCE,_POSIX_C_SOURCE,_GNU_SOURCEor_USE_MATH_DEFINES, and mingw-w64'smath.hgates theM_*block on#if !defined(__STRICT_ANSI__) || defined(_POSIX_C_SOURCE) || ... || defined(_USE_MATH_DEFINES).meson.buildsetsc_std=c17, which defines__STRICT_ANSI__— so every clause is false at the first include.Observed building 1.7.0 from source under Python 3.14 on Windows (that version has no cp314 wheel):
1.9.0 ships cp314 wheels, so the source build is rarely exercised right now — but the code is unchanged on
main, and this resurfaces in the window before wheels exist for each new CPython.This PR moves the define ahead of the first include in
cwt.c. The alternative is-D_USE_MATH_DEFINESin_extensions/meson.build'sc_args; I chose the source-level define so it also holds for builds that don't go through meson. The existing define incwt.template.cis left alone — it is now redundant but harmless.Caveat: I have no Windows toolchain here, so the fix is reasoned from the mingw-w64 header rather than compiled. MSVC is untested. Downstream context: ROCm/TheRock#7798.