Skip to content

Commit de9a4fb

Browse files
authored
gh-154643: Only use _Py_STDC_VERSION when defined (GH-154650)
* gh-154643: Only use _Py_STDC_VERSION when defined This fixes: $ g++ -I/usr/include/python3.15 -c -Werror=undef test.cpp In file included from /usr/include/python3.15/Python.h:68, from test.cpp:1: /usr/include/python3.15/pyport.h:494:9: error: ‘_Py_STDC_VERSION’ is not defined, evaluates to ‘0’ [-Werror=undef] 494 | # elif _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_THREADS__) | ^~~~~~~~~~~~~~~~ cc1plus: some warnings being treated as errors For any test.cpp that has #include <Python.h>. For locating the place for the added test, I used LLM. The main issue I could reproduce was in Include/pyport.h. Include/cpython/pyatomic.h was updated for completeness. Include/internal/mimalloc/mimalloc/atomic.h also has this for __STDC_VERSION__ but was left untouched as it is code from a bundled project and is likely not exposed publicly. Assisted-By: Claude Opus 4.6 * Also add test for C * Add -Werror=undef to PUBLIC_CFLAGS & PEDANTIC_CXXFLAGS in setup.py * Drop tests, we use PUBLIC_CFLAGS & PEDANTIC_CXXFLAGS instead
1 parent 4b79d90 commit de9a4fb

4 files changed

Lines changed: 10 additions & 2 deletions

File tree

‎Include/cpython/pyatomic.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ static inline void _Py_atomic_fence_release(void);
585585
# define Py_ATOMIC_GCC_H
586586
# include "pyatomic_gcc.h"
587587
# undef Py_ATOMIC_GCC_H
588-
#elif _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_ATOMICS__)
588+
#elif defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_ATOMICS__)
589589
# define Py_ATOMIC_STD_H
590590
# include "pyatomic_std.h"
591591
# undef Py_ATOMIC_STD_H

‎Include/pyport.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ extern "C" {
490490
#ifdef WITH_THREAD
491491
# ifdef thread_local
492492
# define _Py_thread_local thread_local
493-
# elif _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_THREADS__)
493+
# elif defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_THREADS__)
494494
# define _Py_thread_local _Thread_local
495495
# elif defined(_MSC_VER) /* AKA NT_THREADS */
496496
# define _Py_thread_local __declspec(thread)

‎Lib/test/test_cext/setup.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242

4343
# Ask for strict(er) compliance with the standard
4444
'-pedantic-errors',
45+
46+
# gh-154643: Headers should not use undefined macros in #if
47+
'-Werror=undef',
4548
]
4649
if not support.Py_GIL_DISABLED:
4750
PUBLIC_CFLAGS.append(
@@ -94,6 +97,9 @@
9497

9598
# We also use `long long`, a C++11 feature we can enable individually.
9699
'-Wno-long-long',
100+
101+
# gh-154643: Headers should not use undefined macros in #if
102+
'-Werror=undef',
97103
]
98104
INTERNAL_CXXFLAGS = [
99105
# Do not complain about unused parameteres in the internal C API
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In ``pyport.h`` and ``pyatomic.h`` don't use ``_Py_STDC_VERSION`` when it is
2+
not defined, fixing build errors for C or C++ code built with ``-Werror=undef``.

0 commit comments

Comments
 (0)