[Python-Dev] Guarantee the success of some object creation C API functions (original) (raw)

Serhiy Storchaka storchaka at gmail.com
Tue May 2 01:49:41 EDT 2017


On 02.05.17 00:52, Chris Angelico wrote:

Aside from straight-up bugs, how can one of these functions fail? Is memory allocation failure the only way?

Yes, memory allocation failure is the only way. In normal Python build this can happen only at early stage of the interpreter initialization, until the first call of the function allocate a singleton in dynamic memory, or, for some instances, at late stage of the interpreter finalization after deallocating singletons. In custom Python build with disabled free lists for small ints, tuples, etc (if define NSMALLPOSINTS or PyTuple_MAXSAVESIZE to 0) this can happen at any time.

Incidentally, this guarantee, if implemented the obvious way, will also mean that (), "", 0, etc are singletons. People talk casually about the "empty tuple singleton", but I don't think it's actually guaranteed anywhere.

As noted Nick this guarantee doesn't go so far. With C API you can modify just created tuple in-place and set its size to 0. This gives you an empty tuple different from the "empty tuple singleton". Some C API functions actually can return non-singletons equal to singleton objects, but usually we try to avoid this. Not because there are any guaranties, but for optimization. References to singletons consume less memory and compare faster.



More information about the Python-Dev mailing list