Skip to content

Commit 21e81dd

Browse files
committed
Add _Py_ANONYMOUS to mark anonymous unions
1 parent d18f73a commit 21e81dd

3 files changed

Lines changed: 36 additions & 12 deletions

File tree

Include/Python.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@
5959
# endif
6060
#endif // Py_GIL_DISABLED
6161

62+
#ifdef _MSC_VER
63+
// Ignore MSC warning C4201: "nonstandard extension used: nameless
64+
// struct/union". (Only generated for C standard versions less than C11, which
65+
// we don't *officially* support.)
66+
__pragma(warning(push))
67+
__pragma(warning(disable: 4201))
68+
#endif
69+
70+
6271
// Include Python header files
6372
#include "pyport.h"
6473
#include "pymacro.h"
@@ -138,4 +147,8 @@
138147
#include "cpython/pyfpe.h"
139148
#include "cpython/tracemalloc.h"
140149

150+
#ifdef _MSC_VER
151+
__pragma(warning(pop)) // warning(disable: 4201)
152+
#endif
153+
141154
#endif /* !Py_PYTHON_H */

Include/object.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,7 @@ whose size is determined when the object is allocated.
123123
/* PyObject is opaque */
124124
#elif !defined(Py_GIL_DISABLED)
125125
struct _object {
126-
#if (defined(__GNUC__) || defined(__clang__)) \
127-
&& !(defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L)
128-
// On C99 and older, anonymous union is a GCC and clang extension
129-
__extension__
130-
#endif
131-
#ifdef _MSC_VER
132-
// Ignore MSC warning C4201: "nonstandard extension used:
133-
// nameless struct/union"
134-
__pragma(warning(push))
135-
__pragma(warning(disable: 4201))
136-
#endif
137-
union {
126+
_Py_ANONYMOUS union {
138127
#if SIZEOF_VOID_P > 4
139128
PY_INT64_T ob_refcnt_full; /* This field is needed for efficient initialization with Clang on ARM */
140129
struct {

Include/pymacro.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,28 @@
8686
# endif
8787
#endif
8888

89+
90+
// _Py_ANONYMOUS: modifier for declaring an anonymous struct/union.
91+
// Usage: _Py_ANONYMOUS union { ... };
92+
// Standards/compiler support:
93+
// - nothing needed in C++
94+
// - nothing needed in C11 and above
95+
// - MSVC has warning(disable: 4201) "nonstandard extension used : nameless
96+
// struct/union". This is specific enough that we disable it for all of
97+
// Python.h.
98+
// - GCC & clang needs __extension__ before C11
99+
// To allow unsupported platforms which need other spellings, we use a
100+
// predefined value of _Py_ANONYMOUS if it exists.
101+
#ifndef _Py_ANONYMOUS
102+
# if (defined(__GNUC__) || defined(__clang__)) \
103+
&& !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
104+
# define _Py_ANONYMOUS __extension__
105+
# else
106+
# define _Py_ANONYMOUS
107+
# endif
108+
#endif
109+
110+
89111
/* Minimum value between x and y */
90112
#define Py_MIN(x, y) (((x) > (y)) ? (y) : (x))
91113

0 commit comments

Comments
 (0)