gh-117482: Preserve the Mapping Between Type Slot Name and Attributes #122866
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In Objects/typeobject.c you'll find a static array named "slotdefs", which provides all the information needed to expose a type's "tp slots" (
PyTypeObject
fields) to Python code as "dunder" methods. The array is initialized using a bunch of macros that identify the tp slot and attribute name that go together. The array ends up holding that attribute name, but the slot name is not preserved currently. Having that mapping would be helpful for a test I've been working on (as well as various personal scripts I've been using).An alternative approach would be to have the macros actually preserve the slot name in each array item. That seems better than the after-the-fact way I've done it here. However, I tried modifying typeobject.c along those lines and it ended up being a bit messy. The approach here is less disruptive, so I've gone that route. Given that my current needs here are strictly related to tests, the testing-focused solution here should be fine.
(I may still circle back to the better approach later, to see if I can make it less disruptive.)
int.__str__
behaviour inside sub-interpreters #117482