[Python-ideas] Allow mutable builtin types (optionally)

Petr Viktorin encukou at gmail.com
Mon May 7 15:23:11 EDT 2018

On 05/07/18 11:37, Eloi Gaudry wrote:
> Hi,
> I'd like to bring back this discussion (from 2005, by Greg):
> https://bugs.python.org/issue1229239
> Briefly, non-heap types cannot have their
> attributes changed by Python code. This makes sense for python builtin
> types, but not for the types defined in extension/modules.
> As we have been using this patch for the very same reasons and for more
> than 10 years, I think it might make sense to reconsider the discussion
> that Greg started.
> The main question at that time was "why not using a heap type instead
> ?" (because heap-type do not have this limitation).
> But I think that the right question could have been "why imposing such
> a restriction on non-heap types as defined in (non Python core)
> extensions ?".
> I mean, to my knowledge, there is no reason why a type should be
> allocated on the heap (https://docs.python.org/2/c-api/typeobj.html) to
> be able to change its attributes at Python level.

One reason is sub-interpreter support: you can have multiple 
interpreters per process, and those shouldn't influence each other.
(see https://docs.python.org/3/c-api/init.html#sub-interpreter-support)

With heap types, each sub-interpreter can have its own copy of the type 
object. But with builtins, changes done in one interpreter would be 
visible in all the others.

> I'm not saying that all non-heap types should be able to do so, just
> that it would make sense to allow this behavior, as an option (bit
> flag).
> At the implementation level, the changes needed are really limited
> (about a few lines):
> - Include/object.h
> - Objects/typeobject.c:
> Eloi

More information about the Python-ideas mailing list