Skip to content

Allow anonymous unions in public headers, using _Py_ANONYMOUS #137283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Include/Python.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 */
16 changes: 1 addition & 15 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -153,9 +142,6 @@ struct _object {
#endif
_Py_ALIGNED_DEF(_PyObject_MIN_ALIGNMENT, char) _aligner;
};
#ifdef _MSC_VER
__pragma(warning(pop))
#endif

PyTypeObject *ob_type;
};
Expand Down
22 changes: 22 additions & 0 deletions Include/pymacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,28 @@
# endif
#endif


// _Py_ANONYMOUS: modifier for declaring an anonymous union.
// Usage: _Py_ANONYMOUS union { ... };
// Standards/compiler support:
// - 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.
// - 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))

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy