Skip to content

Commit 0de928f

Browse files
encukoumiss-islington
authored andcommitted
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 8335bcb) Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent fb6ca74 commit 0de928f

5 files changed

Lines changed: 19 additions & 22 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 __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
588+
#elif _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/pymacro.h‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#ifndef Py_PYMACRO_H
22
#define Py_PYMACRO_H
33

4+
// _Py_STDC_VERSION: the version of the C standard.
5+
// gh-160208: The C++ standard allows __STDC_VERSION__ to be defined to an
6+
// arbitrary value, which means we can't use __STDC_VERSION__ on C++.
7+
#if !defined(__cplusplus) && defined(__STDC_VERSION__)
8+
# define _Py_STDC_VERSION __STDC_VERSION__
9+
#endif
10+
411
// gh-91782: On FreeBSD 12, if the _POSIX_C_SOURCE and _XOPEN_SOURCE macros are
512
// defined, <sys/cdefs.h> disables C11 support and <assert.h> does not define
613
// the static_assert() macro.
@@ -18,8 +25,8 @@
1825
// behaviour. So only define if building as C, not C++ (if __cplusplus is
1926
// not defined), and only for C11-17.
2027
#if !defined(static_assert) && (defined(__GNUC__) || defined(__clang__)) \
21-
&& !defined(__cplusplus) && defined(__STDC_VERSION__) \
22-
&& __STDC_VERSION__ >= 201112L && __STDC_VERSION__ <= 201710L
28+
&& !defined(_Py_STDC_VERSION) \
29+
&& _Py_STDC_VERSION >= 201112L && _Py_STDC_VERSION <= 201710L
2330
# define static_assert _Static_assert
2431
#endif
2532

@@ -75,9 +82,9 @@
7582
# endif
7683
# elif defined(_MSC_VER)
7784
# define _Py_ALIGNED_DEF(N, T) __declspec(align(N)) T
78-
# elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
85+
# elif defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 202311L
7986
# define _Py_ALIGNED_DEF(N, T) alignas(N) alignas(T) T
80-
# elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
87+
# elif defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L
8188
# define _Py_ALIGNED_DEF(N, T) _Alignas(N) _Alignas(T) T
8289
# elif (defined(__GNUC__) || defined(__clang__))
8390
# define _Py_ALIGNED_DEF(N, T) __attribute__((aligned(N))) T
@@ -100,7 +107,7 @@
100107
// predefined value of _Py_ANONYMOUS if it exists.
101108
#ifndef _Py_ANONYMOUS
102109
# if (defined(__GNUC__) || defined(__clang__)) \
103-
&& !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
110+
&& !(defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L)
104111
# define _Py_ANONYMOUS __extension__
105112
# else
106113
# define _Py_ANONYMOUS
@@ -137,7 +144,7 @@
137144
/* Argument must be a char or an int in [-128, 127] or [0, 255]. */
138145
#define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))
139146

140-
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
147+
#if (defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L \
141148
&& !defined(__cplusplus) && !defined(_MSC_VER))
142149
# define Py_BUILD_ASSERT_EXPR(cond) \
143150
((void)sizeof(struct { int dummy; _Static_assert(cond, #cond); }), \
@@ -160,7 +167,7 @@
160167
(sizeof(char [1 - 2*!(cond)]) - 1)
161168
#endif
162169

163-
#if ((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) \
170+
#if ((defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 201112L) \
164171
|| (defined(__cplusplus) && __cplusplus >= 201103L))
165172
// Use static_assert() on C11 and newer
166173
# define Py_BUILD_ASSERT(cond) \

‎Include/pyport.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
// to prevent C++ compiler warnings. On C23 and newer and on C++11 and newer,
5151
// _Py_NULL is defined as nullptr.
5252
#if !defined(_MSC_VER) && \
53-
((defined (__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) \
53+
((defined (_Py_STDC_VERSION) && _Py_STDC_VERSION >= 202311L) \
5454
|| (defined(__cplusplus) && __cplusplus >= 201103))
5555
# define _Py_NULL nullptr
5656
#else
@@ -491,7 +491,7 @@ extern "C" {
491491
#ifdef WITH_THREAD
492492
# ifdef thread_local
493493
# define _Py_thread_local thread_local
494-
# elif __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
494+
# elif _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)
@@ -543,7 +543,7 @@ extern "C" {
543543
//
544544
// gh-157649: Do not use decltype() on C++, since it produces invalid code in
545545
// Py_CLEAR()/Py_SETREF().
546-
#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
546+
#if defined (_Py_STDC_VERSION) && _Py_STDC_VERSION >= 202311L
547547
# define _Py_TYPEOF(expr) typeof(expr)
548548
#elif (defined(__GNUC__) || defined(__clang__) \
549549
|| (defined(_MSC_VER) && _MSC_VER >= 1939 && !defined(__cplusplus)))

‎Modules/_ctypes/_ctypes_test.c‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,6 @@
1313

1414
#include <Python.h>
1515

16-
#ifdef thread_local
17-
# define _Py_thread_local thread_local
18-
#elif __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
19-
# define _Py_thread_local _Thread_local
20-
#elif defined(_MSC_VER) /* AKA NT_THREADS */
21-
# define _Py_thread_local __declspec(thread)
22-
#elif defined(__GNUC__) /* includes clang */
23-
# define _Py_thread_local __thread
24-
#endif
25-
2616
#if defined(_Py_FFI_SUPPORT_C_COMPLEX)
2717
# include <complex.h> // csqrt()
2818
# undef I // for _ctypes_test_generated.c.h

‎Modules/mathmodule.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2516,7 +2516,7 @@ static PyObject *
25162516
math_issubnormal_impl(PyObject *module, double x)
25172517
/*[clinic end generated code: output=4e76ac98ddcae761 input=9a20aba7107d0d95]*/
25182518
{
2519-
#if !defined(_MSC_VER) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
2519+
#if !defined(_MSC_VER) && defined(_Py_STDC_VERSION) && _Py_STDC_VERSION >= 202311L
25202520
return PyBool_FromLong(issubnormal(x));
25212521
#else
25222522
return PyBool_FromLong(isfinite(x) && x && !isnormal(x));

0 commit comments

Comments
 (0)