gh-138143: Allow anonymous unions in public headers, using _Py_ANONYMOUS (GH-137283)

We already use an anonymous union for PyObject. This makes the workarounds available in all public headers:

- MSVC: `__pragma(warning(disable: 4201))` (with push/pop). Warning 4201 is specifically for anonymous unions, so let's disable for all of `<Python.h>`
- GCC/clang, pedantic old C standards: define `_Py_ANONYMOUS` as `__extension__`
- otherwise, define `_Py_ANONYMOUS` as nothing 

(Note that this is only for public headers -- CPython internals use C11, which has anonymous structs/unions.)

C API WG vote: https://github.com/capi-workgroup/decisions/issues/74
This commit is contained in:
Petr Viktorin 2025-08-26 11:14:35 +02:00 committed by GitHub
parent 73fb155ba7
commit ce1a877a38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 15 deletions

View file

@ -60,6 +60,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"
@ -139,4 +148,8 @@
#include "cpython/pyfpe.h"
#include "cpython/tracemalloc.h"
#ifdef _MSC_VER
__pragma(warning(pop)) // warning(disable: 4201)
#endif
#endif /* !Py_PYTHON_H */