https://github.com/python/cpython/commit/de9a4fb5586cc9e60e8b75a3f69033c4a2d6c61b
commit: de9a4fb5586cc9e60e8b75a3f69033c4a2d6c61b
branch: main
author: Miro Hrončok <[email protected]>
committer: encukou <[email protected]>
date: 2026-11-11T11:12:36+01:00
summary:

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

files:
A Misc/NEWS.d/next/C_API/2026-07-24-22-30-40.gh-issue-154643.z-ZutK.rst
M Include/cpython/pyatomic.h
M Include/pyport.h
M Lib/test/test_cext/setup.py

diff --git a/Include/cpython/pyatomic.h b/Include/cpython/pyatomic.h
index 71a76bdc82ce475..0a11f7781f07e00 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 718a9b50bae1039..de1240be76b3dad 100644
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -490,7 +490,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/setup.py b/Lib/test/test_cext/setup.py
index 10a3887931932ac..3b613ee729f52c4 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
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..17c2a58555e7ae9
--- /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 or C++ code built with 
``-Werror=undef``.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to