https://github.com/python/cpython/commit/47163e53753ea681d2b4bd2683f4ca4833838b89
commit: 47163e53753ea681d2b4bd2683f4ca4833838b89
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: encukou <[email protected]>
date: 2026-10-23T20:31:48+02:00
summary:

[3.15] gh-160208: Add a private wrapper around __STDC_VERSION__ (GH-160376) 
(GH-160437)

gh-160208: Add a private wrapper around __STDC_VERSION__ (GH-160376)

The wrapper is undefined on C++, where it can have an arbitrary
compiler-specific value.
(cherry picked from commit 8335bcbf26bf542b751dab4c0e031aba3672d6e4)

Co-authored-by: Petr Viktorin <[email protected]>

files:
M Include/cpython/pyatomic.h
M Include/pymacro.h
M Include/pyport.h
M Modules/_ctypes/_ctypes_test.c
M Modules/mathmodule.c

diff --git a/Include/cpython/pyatomic.h b/Include/cpython/pyatomic.h
index b4b1a8ca87dd8b..71a76bdc82ce47 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 __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
+#elif _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/pymacro.h b/Include/pymacro.h
index a823d5535665b2..29d20953235651 100644
--- a/Include/pymacro.h
+++ b/Include/pymacro.h
@@ -1,6 +1,13 @@
 #ifndef Py_PYMACRO_H
 #define Py_PYMACRO_H
 
+// _Py_STDC_VERSION: the version of the C standard.
+// gh-160208: The C++ standard allows __STDC_VERSION__ to be defined to an
+// arbitrary value, which means we can't use __STDC_VERSION__ on C++.
+#if !defined(__cplusplus) && defined(__STDC_VERSION__)
+#  define _Py_STDC_VERSION __STDC_VERSION__
+#endif
+
 // gh-91782: On FreeBSD 12, if the _POSIX_C_SOURCE and _XOPEN_SOURCE macros are
 // defined, <sys/cdefs.h> disables C11 support and <assert.h> does not define
 // the static_assert() macro.
@@ -18,8 +25,8 @@
 // behaviour. So only define if building as C, not C++ (if __cplusplus is
 // not defined), and only for C11-17.
 #if !defined(static_assert) && (defined(__GNUC__) || defined(__clang__)) \
-     && !defined(__cplusplus) && defined(__STDC_VERSION__) \
-     && __STDC_VERSION__ >= 201112L && __STDC_VERSION__ <= 201710L
+     && !defined(_Py_STDC_VERSION) \
+     && _Py_STDC_VERSION >= 201112L && _Py_STDC_VERSION <= 201710L
 #  define static_assert _Static_assert
 #endif
 
@@ -75,9 +82,9 @@
 #        endif
 #    elif defined(_MSC_VER)
 #        define _Py_ALIGNED_DEF(N, T) __declspec(align(N)) T
-#    elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
+#    elif defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 202311L
 #        define _Py_ALIGNED_DEF(N, T) alignas(N) alignas(T) T
-#    elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
+#    elif defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L
 #        define _Py_ALIGNED_DEF(N, T)  _Alignas(N) _Alignas(T) T
 #    elif (defined(__GNUC__) || defined(__clang__))
 #        define _Py_ALIGNED_DEF(N, T) __attribute__((aligned(N))) T
@@ -100,7 +107,7 @@
 // predefined value of _Py_ANONYMOUS if it exists.
 #ifndef _Py_ANONYMOUS
 #   if (defined(__GNUC__) || defined(__clang__)) \
-          && !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
+          && !(defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L)
 #       define _Py_ANONYMOUS __extension__
 #   else
 #       define _Py_ANONYMOUS
@@ -137,7 +144,7 @@
 /* Argument must be a char or an int in [-128, 127] or [0, 255]. */
 #define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))
 
-#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
+#if (defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L \
      && !defined(__cplusplus) && !defined(_MSC_VER))
 #  define Py_BUILD_ASSERT_EXPR(cond) \
     ((void)sizeof(struct { int dummy; _Static_assert(cond, #cond); }), \
@@ -160,7 +167,7 @@
     (sizeof(char [1 - 2*!(cond)]) - 1)
 #endif
 
-#if ((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) \
+#if ((defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L) \
      || (defined(__cplusplus) && __cplusplus >= 201103L))
    // Use static_assert() on C11 and newer
 #  define Py_BUILD_ASSERT(cond) \
diff --git a/Include/pyport.h b/Include/pyport.h
index 2206beaa77221c..9b262ff0022aca 100644
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -50,7 +50,7 @@
 // to prevent C++ compiler warnings. On C23 and newer and on C++11 and newer,
 // _Py_NULL is defined as nullptr.
 #if !defined(_MSC_VER) && \
-    ((defined (__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) \
+    ((defined (_Py_STDC_VERSION) && _Py_STDC_VERSION >= 202311L) \
         || (defined(__cplusplus) && __cplusplus >= 201103))
 #  define _Py_NULL nullptr
 #else
@@ -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 _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)
@@ -543,7 +543,7 @@ extern "C" {
 //
 // gh-157649: Do not use decltype() on C++, since it produces invalid code in
 // Py_CLEAR()/Py_SETREF().
-#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
+#if defined (_Py_STDC_VERSION) && _Py_STDC_VERSION >= 202311L
 #  define _Py_TYPEOF(expr) typeof(expr)
 #elif (defined(__GNUC__) || defined(__clang__) \
        || (defined(_MSC_VER) && _MSC_VER >= 1939 && !defined(__cplusplus)))
diff --git a/Modules/_ctypes/_ctypes_test.c b/Modules/_ctypes/_ctypes_test.c
index 6a5b20143f5527..12360b3f5ba76c 100644
--- a/Modules/_ctypes/_ctypes_test.c
+++ b/Modules/_ctypes/_ctypes_test.c
@@ -13,16 +13,6 @@
 
 #include <Python.h>
 
-#ifdef thread_local
-#  define _Py_thread_local thread_local
-#elif __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)
-#elif defined(__GNUC__)  /* includes clang */
-#  define _Py_thread_local __thread
-#endif
-
 #if defined(_Py_FFI_SUPPORT_C_COMPLEX)
 #  include <complex.h>            // csqrt()
 #  undef I                        // for _ctypes_test_generated.c.h
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 5636a00afe10c4..e09136abad01eb 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -2516,7 +2516,7 @@ static PyObject *
 math_issubnormal_impl(PyObject *module, double x)
 /*[clinic end generated code: output=4e76ac98ddcae761 input=9a20aba7107d0d95]*/
 {
-#if !defined(_MSC_VER) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 
202311L
+#if !defined(_MSC_VER) && defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 
202311L
     return PyBool_FromLong(issubnormal(x));
 #else
     return PyBool_FromLong(isfinite(x) && x && !isnormal(x));

_______________________________________________
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