-
-
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
Limited API way of assigning to immutable types and/or making a class immutable #121654
Comments
Hmm. Would it work to have a slot like API for changing a mutable type to immutable sounds doable. It might even be a better way to create types: create the object, then set slots/attributes (using setattr, or an “apply slots” function to reduce API calls), then “freeze” it if it's to be immutable. See markshannon/New-C-API-for-Python#4 (comment) for some previous discussion. |
I think that would work. It's not how we currently do it, but I don't think anything we do relies on the class existing first.
I'm not completely sure - we currently use statically allocated specs, and tables of slots for these (which wouldn't work, for this case). However I don't think it's a strict requirement. |
I would prefer this approach, rather than leaving "holes" where an "immutable" type can be modified on purpose. Can you propose an API for that? |
Looks like the API could be simple, but I haven't started trying to implement it:
To make it more usable, we should add:
and perhaps some parts of “new type creation” and GUPOL. |
That was going to be my suggestion. I think this makes more sense than a general mechanism for changing type flags.
I don't think Cython would need this - everything we want to do before immortalization can be done with |
I wrote PR gh-122457 to implement PyType_Freeze(). |
I've moved away from the I still think But if you're ever considering modifying |
@da-woods — I'm curious about the motivation of making Cython's classes immutable by default. Is this to benefit from performance improvements related to the specializing interpreter? Or simply to avoid bugs due to accidental modification of a type. |
Mostly this. Also because the existing non-Limited API version uses static types which are automatically immutable and we find it simpler if the two modes of compilation have the same behaviour. |
Co-authored-by: Petr Viktorin <[email protected]>
PyType_Freeze() is now implemented by change db96327. |
Feature or enhancement
Proposal:
This is one of the uglier bits of Cython's limited API implementation.
When we create our extension type ("cdef classes") we want them to be presented to users as immutable (mainly because that's what they are in non-limited-API modes and we want it to remain the same).
However, we do want to assign things to the type dict just after type creation (i.e. before it's presented to the user). That's for 2 things:
We make
f
our own function type (to get better introspection that the default Python C function), so need to be able to assign those functions into the class namespace (i.e. we don't want to just usePyMethodDef
).What doesn't work:
PyObject_SetAttr
- fails because the type is immutable__dict__
- returns a read-only proxyPyType_GetDict
- not in the Limited API.My current workaround is to use
PyObject_GenericSetAttr
. This is supposed to be used as a good initial value oftp_setattro
for mutable types. However, it doesn't check the mutability of the type, and does succeed in modifying immutable types. However, it feels like I'm misusing non-documented unintended behaviour in something.Solutions I think could work:
PyType_GetDict
,GenericGetAttr
way is actually fine),@markshannon @encukou @vstinner tagging you as people who might have an interest in this. Not urgent though - we have a functioning workaround.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
The text was updated successfully, but these errors were encountered: