Skip to content

gh-138143: Allow anonymous unions in public headers, using _Py_ANONYMOUS#137283

Merged
encukou merged 4 commits intopython:mainfrom
encukou:anonymous
Aug 26, 2025
Merged

gh-138143: Allow anonymous unions in public headers, using _Py_ANONYMOUS#137283
encukou merged 4 commits intopython:mainfrom
encukou:anonymous

Conversation

@encukou
Copy link
Copy Markdown
Member

@encukou encukou commented Jul 31, 2025

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: capi-workgroup/decisions#74

Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the warning only be silenced for anonymous unions, not for anonymous structs?

Maybe add _Py_ANONYMOUS_UNION, or even _Py_BEGIN_ANONYMOUS_UNION and _Py_END_ANONYMOUS_UNION?

@encukou
Copy link
Copy Markdown
Member Author

encukou commented Aug 4, 2025

I think that would mean more typing for little benefit.
If you put _Py_ANONYMOUS struct in the headers, tests will fail in strict C++ mode.

@encukou encukou changed the title Allow anonymous unions in public headers, using _Py_ANONYMOUS gh-138143: Allow anonymous unions in public headers, using _Py_ANONYMOUS Aug 25, 2025
@encukou encukou marked this pull request as ready for review August 25, 2025 15:57
@encukou encukou merged commit ce1a877 into python:main Aug 26, 2025
79 of 81 checks passed
@encukou encukou deleted the anonymous branch August 26, 2025 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants