From 21e81dd2e0d5acea63265a8de561050011e4f604 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 4 Mar 2025 16:34:34 +0100 Subject: [PATCH 1/3] Add _Py_ANONYMOUS to mark anonymous unions --- Include/Python.h | 13 +++++++++++++ Include/object.h | 13 +------------ Include/pymacro.h | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/Include/Python.h b/Include/Python.h index 3f49b78947c9a6..501a975731cb84 100644 --- a/Include/Python.h +++ b/Include/Python.h @@ -59,6 +59,15 @@ # endif #endif // Py_GIL_DISABLED +#ifdef _MSC_VER +// Ignore MSC warning C4201: "nonstandard extension used: nameless +// struct/union". (Only generated for C standard versions less than C11, which +// we don't *officially* support.) +__pragma(warning(push)) +__pragma(warning(disable: 4201)) +#endif + + // Include Python header files #include "pyport.h" #include "pymacro.h" @@ -138,4 +147,8 @@ #include "cpython/pyfpe.h" #include "cpython/tracemalloc.h" +#ifdef _MSC_VER +__pragma(warning(pop)) // warning(disable: 4201) +#endif + #endif /* !Py_PYTHON_H */ diff --git a/Include/object.h b/Include/object.h index b1bcc9481871b4..45e640814c1d82 100644 --- a/Include/object.h +++ b/Include/object.h @@ -123,18 +123,7 @@ whose size is determined when the object is allocated. /* PyObject is opaque */ #elif !defined(Py_GIL_DISABLED) struct _object { -#if (defined(__GNUC__) || defined(__clang__)) \ - && !(defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L) - // On C99 and older, anonymous union is a GCC and clang extension - __extension__ -#endif -#ifdef _MSC_VER - // Ignore MSC warning C4201: "nonstandard extension used: - // nameless struct/union" - __pragma(warning(push)) - __pragma(warning(disable: 4201)) -#endif - union { + _Py_ANONYMOUS union { #if SIZEOF_VOID_P > 4 PY_INT64_T ob_refcnt_full; /* This field is needed for efficient initialization with Clang on ARM */ struct { diff --git a/Include/pymacro.h b/Include/pymacro.h index b2886ddac5d17e..807c9748e5b147 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -86,6 +86,28 @@ # endif #endif + +// _Py_ANONYMOUS: modifier for declaring an anonymous struct/union. +// Usage: _Py_ANONYMOUS union { ... }; +// Standards/compiler support: +// - nothing needed in C++ +// - nothing needed in C11 and above +// - MSVC has warning(disable: 4201) "nonstandard extension used : nameless +// struct/union". This is specific enough that we disable it for all of +// Python.h. +// - GCC & clang needs __extension__ before C11 +// To allow unsupported platforms which need other spellings, we use a +// predefined value of _Py_ANONYMOUS if it exists. +#ifndef _Py_ANONYMOUS +# if (defined(__GNUC__) || defined(__clang__)) \ + && !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) +# define _Py_ANONYMOUS __extension__ +# else +# define _Py_ANONYMOUS +# endif +#endif + + /* Minimum value between x and y */ #define Py_MIN(x, y) (((x) > (y)) ? (y) : (x)) From c478d94194b5e2c77635b44cb6a68b902718a499 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 4 Mar 2025 16:34:34 +0100 Subject: [PATCH 2/3] Remove extra pop --- Include/object.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/Include/object.h b/Include/object.h index 45e640814c1d82..1f45e7df388ec7 100644 --- a/Include/object.h +++ b/Include/object.h @@ -142,9 +142,6 @@ struct _object { #endif _Py_ALIGNED_DEF(_PyObject_MIN_ALIGNMENT, char) _aligner; }; -#ifdef _MSC_VER - __pragma(warning(pop)) -#endif PyTypeObject *ob_type; }; From 3eb81773717d00f3fba6d3867a3e01a113b11548 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Mon, 4 Aug 2025 11:03:12 +0200 Subject: [PATCH 3/3] Unions only --- Include/pymacro.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Include/pymacro.h b/Include/pymacro.h index 807c9748e5b147..857cdf12db9bf2 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -87,11 +87,11 @@ #endif -// _Py_ANONYMOUS: modifier for declaring an anonymous struct/union. +// _Py_ANONYMOUS: modifier for declaring an anonymous union. // Usage: _Py_ANONYMOUS union { ... }; // Standards/compiler support: -// - nothing needed in C++ -// - nothing needed in C11 and above +// - C++ allows anonymous unions, but not structs +// - C11 and above allows anonymous unions and structs // - MSVC has warning(disable: 4201) "nonstandard extension used : nameless // struct/union". This is specific enough that we disable it for all of // Python.h.