-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
gh-121654: Add Py_tp_create_callback slot #124789
Conversation
cc @zooba |
Yep, this is the idea I had in mind. It looks weird until you remember that we intend for the spec to be static, which means each type will have its own callback that knows how to add additional members. You could use the type object to look up dynamic state, but that's not the intent of the entire API to begin with. |
I honestly like this PR, and the |
It's not clear what kind of restrictions are in place for the callback. For example, are you allowed to instantiate the type in the callback? (I imagine a singleton class could want to instantiate itself and set Arbitrary code between
That's most common, but they could also be filled dynamically and live on the stack. (And so you can't get a spec back from a type.) |
Yeah, it's a good point, and it probably would be good to allow that if it's doable (at great risk of getting attribute errors in any code that may run). It's why my alternate proposals focused around working with the type dict, rather than the type object itself.
Yeah, agreed, which is why I preferred providing an array of The callback pointer could become a struct with a pointer and a userdata |
Honestly, I prefer PyType_Freeze() API. It's more natural and convenient, and I'm not convinced that |
It means we can change the way the type is constructed or allocated based on the fact that it's going to be immutable. It also avoids adding a new API, which proves the forwards-compatibility of |
That's a good point, thank you! I don't think this point is worth replacing imperative code with a callback, though. We don't have a good track records with those: as years roll by, callbacks always seem to be needing a bit more context than they're getting.
Modifying
I think this is a good idea regardless. Especially if we add a
... and now the spec isn't static any more :( |
The C API Working Group approved |
I think having both is good though as there can be use cases to having both options. Especially if the Freeze call is not in itself part of the limited API but the spec slot is and having it internally call |
I close this PR since the C API Working Group approved PyType_Freeze() API: capi-workgroup/decisions#40 |
📚 Documentation preview 📚: https://cpython-previews--124789.org.readthedocs.build/