From 2363a517868069e286a1aa9e30ea907d340185b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Fri, 24 Jul 2026 22:40:30 +0200 Subject: [PATCH 1/3] gh-154643: Only use _Py_STDC_VERSION when defined MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 . 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 --- Include/cpython/pyatomic.h | 2 +- Include/pyport.h | 2 +- Lib/test/test_cext/__init__.py | 5 +++++ .../C_API/2026-07-24-22-30-40.gh-issue-154643.z-ZutK.rst | 2 ++ 4 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/C_API/2026-07-24-22-30-40.gh-issue-154643.z-ZutK.rst diff --git a/Include/cpython/pyatomic.h b/Include/cpython/pyatomic.h index 71a76bdc82ce47..0a11f7781f07e0 100644 --- a/Include/cpython/pyatomic.h +++ b/Include/cpython/pyatomic.h @@ -585,7 +585,7 @@ static inline void _Py_atomic_fence_release(void); # define Py_ATOMIC_GCC_H # include "pyatomic_gcc.h" # undef Py_ATOMIC_GCC_H -#elif _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_ATOMICS__) +#elif defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_ATOMICS__) # define Py_ATOMIC_STD_H # include "pyatomic_std.h" # undef Py_ATOMIC_STD_H diff --git a/Include/pyport.h b/Include/pyport.h index 3de7fb3d317521..ee1d8c0e2483df 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -491,7 +491,7 @@ extern "C" { #ifdef WITH_THREAD # ifdef thread_local # define _Py_thread_local thread_local -# elif _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_THREADS__) +# elif defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L && !defined(__STDC_NO_THREADS__) # define _Py_thread_local _Thread_local # elif defined(_MSC_VER) /* AKA NT_THREADS */ # define _Py_thread_local __declspec(thread) diff --git a/Lib/test/test_cext/__init__.py b/Lib/test/test_cext/__init__.py index 185f1091137034..cbea9978a4ae8a 100644 --- a/Lib/test/test_cext/__init__.py +++ b/Lib/test/test_cext/__init__.py @@ -174,6 +174,11 @@ def test_build_abi3t(self): # Test with Py_TARGET_ABI3T self.check_build('_test_cppext_abi3t', abi3t=True) + # gh-154643: Test that headers don't use undefined macros in #if + @unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support -Werror=undef") + def test_build_werror_undef(self): + self.check_build('_testcppext_undef', extra_cflags='-Werror=undef') + class TestInternalC(BaseTests, unittest.TestCase): LANGUAGE = 'C' diff --git a/Misc/NEWS.d/next/C_API/2026-07-24-22-30-40.gh-issue-154643.z-ZutK.rst b/Misc/NEWS.d/next/C_API/2026-07-24-22-30-40.gh-issue-154643.z-ZutK.rst new file mode 100644 index 00000000000000..86476602131bb0 --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-07-24-22-30-40.gh-issue-154643.z-ZutK.rst @@ -0,0 +1,2 @@ +In ``pyport.h`` and ``pyatomic.h`` don't use ``_Py_STDC_VERSION`` when it is +not defined, fixing build errors for C++ code built with ``-Werror=undef``. From e81f0f69cb1f99d07041c1f5eebaba5f877f946c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Fri, 6 Nov 2026 13:06:20 +0100 Subject: [PATCH 2/3] Also add test for C --- Lib/test/test_cext/__init__.py | 5 +++++ .../C_API/2026-07-24-22-30-40.gh-issue-154643.z-ZutK.rst | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_cext/__init__.py b/Lib/test/test_cext/__init__.py index cbea9978a4ae8a..4c7c7ef68e0f85 100644 --- a/Lib/test/test_cext/__init__.py +++ b/Lib/test/test_cext/__init__.py @@ -133,6 +133,11 @@ def test_build_abi3t(self): # Test with Py_TARGET_ABI3T self.check_build('_test_cext_abi3t', abi3t=True) + # gh-154643: Test that headers don't use undefined macros in #if + @unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support -Werror=undef") + def test_build_werror_undef(self): + self.check_build('_test_cext_undef', extra_cflags='-Werror=undef') + class TestPublicCpp(BaseTests, unittest.TestCase): LANGUAGE = 'C++' diff --git a/Misc/NEWS.d/next/C_API/2026-07-24-22-30-40.gh-issue-154643.z-ZutK.rst b/Misc/NEWS.d/next/C_API/2026-07-24-22-30-40.gh-issue-154643.z-ZutK.rst index 86476602131bb0..17c2a58555e7ae 100644 --- a/Misc/NEWS.d/next/C_API/2026-07-24-22-30-40.gh-issue-154643.z-ZutK.rst +++ b/Misc/NEWS.d/next/C_API/2026-07-24-22-30-40.gh-issue-154643.z-ZutK.rst @@ -1,2 +1,2 @@ In ``pyport.h`` and ``pyatomic.h`` don't use ``_Py_STDC_VERSION`` when it is -not defined, fixing build errors for C++ code built with ``-Werror=undef``. +not defined, fixing build errors for C or C++ code built with ``-Werror=undef``. From f29d61083d6c71037858d993b1b300752b7e8e29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Fri, 6 Nov 2026 15:38:07 +0100 Subject: [PATCH 3/3] Add -Werror=undef to PUBLIC_CFLAGS & PEDANTIC_CXXFLAGS in setup.py --- Lib/test/test_cext/setup.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/test/test_cext/setup.py b/Lib/test/test_cext/setup.py index 10a3887931932a..3b613ee729f52c 100644 --- a/Lib/test/test_cext/setup.py +++ b/Lib/test/test_cext/setup.py @@ -42,6 +42,9 @@ # Ask for strict(er) compliance with the standard '-pedantic-errors', + + # gh-154643: Headers should not use undefined macros in #if + '-Werror=undef', ] if not support.Py_GIL_DISABLED: PUBLIC_CFLAGS.append( @@ -94,6 +97,9 @@ # We also use `long long`, a C++11 feature we can enable individually. '-Wno-long-long', + + # gh-154643: Headers should not use undefined macros in #if + '-Werror=undef', ] INTERNAL_CXXFLAGS = [ # Do not complain about unused parameteres in the internal C API