Skip to content

Define _USE_MATH_DEFINES before including Python.h - #870

Open
fjankovi wants to merge 1 commit into
PyWavelets:mainfrom
fjankovi:fix/use-math-defines-before-python-h
Open

Define _USE_MATH_DEFINES before including Python.h#870
fjankovi wants to merge 1 commit into
PyWavelets:mainfrom
fjankovi:fix/use-math-defines-before-python-h

Conversation

@fjankovi

@fjankovi fjankovi commented Sep 2, 2026

Copy link
Copy Markdown

✳️ Assisted by Claude Opus 5

cwt.template.c:18 defines _USE_MATH_DEFINES immediately before #include "math.h", but <math.h> has already been included by that point, so the define has no effect:

cwt.c:1       #include "cwt.h"
cwt.h:3       #include "common.h"
common.h:23   #include "Python.h"
Python.h:24   #include <math.h>     <-- first include; _USE_MATH_DEFINES not yet defined

cwt.template.c is not a translation unit of its own — it is textually included from cwt.c (lines 9 and 13), after that chain has already run. The include guard is set, so line 19 is a no-op and M_PI at line 65 is undeclared.

Whether that matters is platform-dependent:

  • Linux/glibc — unaffected. CPython's autoconf pyconfig.h defines _XOPEN_SOURCE 700, and glibc exposes M_PI under __USE_XOPEN regardless of _USE_MATH_DEFINES.
  • Windows — fails. PC/pyconfig.h defines none of _XOPEN_SOURCE, _POSIX_C_SOURCE, _GNU_SOURCE or _USE_MATH_DEFINES, and mingw-w64's math.h gates the M_* block on #if !defined(__STRICT_ANSI__) || defined(_POSIX_C_SOURCE) || ... || defined(_USE_MATH_DEFINES). meson.build sets c_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):

../pywt/_extensions/c/cwt.template.c: In function 'float_pi':
../pywt/_extensions/c/cwt.template.c:65:16: error: 'M_PI' undeclared (first use in this function)
   65 |         return M_PI;

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_DEFINES in _extensions/meson.build's c_args; I chose the source-level define so it also holds for builds that don't go through meson. The existing define in cwt.template.c is 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.

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>

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

Thanks for the PR @fjankovi! I'd prefer to have the define in meson.build in c_args: - could you please move it there?

It doesn't require a code comment, a one-line fix is enough.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants