@@ -14,8 +14,10 @@ Post-History: `06-Jan-2026 <https://discuss.python.org/t/105552>`__
1414Abstract
1515========
1616
17- Replace type and module slots with a new, more type-safe structure that allows
18- adding new slots in a more forward-compatible way.
17+ Replace type and module slots with a new structure: a tagged anonymous
18+ union with flags.
19+ This improves type safety and allows adding new slots
20+ in a more forward-compatible way.
1921
2022API added in 3.15 (:external+py3.15:c:func: `PyModule_FromSlotsAndSpec ` and the
2123new :external+py3.15:ref: `extension export hook <extension-export-hook >`)
@@ -662,6 +664,43 @@ Note that slots are not a good way for *extension modules* to add extra data
662664to types or modules, as there is no API to retrieve the slots used to create
663665a specific object.
664666
667+ Avoiding anonymous unions
668+ -------------------------
669+
670+ This PEP proposes a struct with *anonymous unions *, which are not yet used in
671+ CPython's documented public API.
672+
673+ There is no known issue with adding these, but the following notes may
674+ be relevant:
675+
676+ - Anonymous unions are only supported in C since C11.
677+ But, CPython already requires the feature, and uses it for internal members
678+ of the ``PyObject `` struct.
679+
680+ - Until C++20, which adds C-style designated initializers, C++ initializers
681+ only allow setting the first member of a union.
682+ However, this is an issue for *named * unions as well.
683+ Avoiding unions entirely would mean losing most of the type-safety
684+ improvements of this PEP.
685+
686+ Note that the proposed flag ``PySlot_INTPTR ``, and the workaround macros
687+ ``PySlot_PTR `` & ``PySlot_PTR_STATIC ``, allow using this API in
688+ code that needs to be compatible with C++11 or has similar union-related
689+ limitations.
690+
691+ - C++ doesn't have anonymous *structs *.
692+ This might surprise C programmers for whom anonymous structs/unions are
693+ a single language feature.
694+
695+ - Non-C/C++ language wrappers may need to give the union a name.
696+ This is fine.
697+ (Dear reader: if you need this, please open a CPython issue about
698+ exposing a preferred name in headers and documentation.)
699+
700+ For a bigger picture: anonymous unions can be a helpful tool for implemeting
701+ tagged unions and for evolving public API in backwards-compatible ways.
702+ This PEP intentionally opens the door to using them more often.
703+
665704
666705Open Issues
667706===========
@@ -672,8 +711,8 @@ None yet.
672711Acknowledgements
673712================
674713
675- Thanks to Da Woods and Antoine Pitrou for substantial input on this iteration
676- of the proposal.
714+ Thanks to Da Woods, Antoine Pitrou and Mark Shannon
715+ for substantial input on this iteration of the proposal.
677716
678717
679718Copyright
0 commit comments