diff --git a/Include/cpython/pyatomic.h b/Include/cpython/pyatomic.h index e85b360c986668c..18842582bf06bd0 100644 --- a/Include/cpython/pyatomic.h +++ b/Include/cpython/pyatomic.h @@ -582,7 +582,7 @@ static inline void _Py_atomic_fence_release(void); # define Py_ATOMIC_GCC_H # include "pyatomic_gcc.h" # undef Py_ATOMIC_GCC_H -#elif __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__) +#elif defined(__STDC_VERSION__) && __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 73a3e6cdaf09200..3936b999a88d505 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 __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__) +# elif defined(__STDC_VERSION__) && __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_cppext/__init__.py b/Lib/test/test_cppext/__init__.py index 967feee6693c037..5bfc4900cd473c8 100644 --- a/Lib/test/test_cppext/__init__.py +++ b/Lib/test/test_cppext/__init__.py @@ -134,6 +134,11 @@ def test_build_cpp14(self): def test_build_intel_asm(self): self.check_build('_testcppext_asm', extra_cflags='-masm=intel') + # 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 TestInteralCAPI(BaseTests, unittest.TestCase): TEST_INTERNAL_C_API = True 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 000000000000000..32c26af7e45b7b4 --- /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 ``__STDC_VERSION__`` when it is +not defined, fixing build errors for C++ code built with ``-Werror=undef``.