Skip to content

Commit 2363a51

Browse files
committed
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
1 parent 7e5227f commit 2363a51

4 files changed

Lines changed: 9 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
@@ -491,7 +491,7 @@ extern "C" {
491491
#ifdef WITH_THREAD
492492
# ifdef thread_local
493493
# define _Py_thread_local thread_local
494-
# elif _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_THREADS__)
494+
# elif defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_THREADS__)
495495
# define _Py_thread_local _Thread_local
496496
# elif defined(_MSC_VER) /* AKA NT_THREADS */
497497
# define _Py_thread_local __declspec(thread)

‎Lib/test/test_cext/__init__.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ def test_build_abi3t(self):
174174
# Test with Py_TARGET_ABI3T
175175
self.check_build('_test_cppext_abi3t', abi3t=True)
176176

177+
# gh-154643: Test that headers don't use undefined macros in #if
178+
@unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support -Werror=undef")
179+
def test_build_werror_undef(self):
180+
self.check_build('_testcppext_undef', extra_cflags='-Werror=undef')
181+
177182

178183
class TestInternalC(BaseTests, unittest.TestCase):
179184
LANGUAGE = 'C'
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++ code built with ``-Werror=undef``.

0 commit comments

Comments
 (0)