You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fill newly created tuple with Py_None instead of NULL #145914
Eventually we can remove NULL checks inside _PyTuple_MaybeUntrack if all created tuples will be well-constructed and contain NULL only if the user is doing something wrong.
I would be extra careful here. This is part of the stable ABI and while not explicitly documented, we should be careful that this change won't break downstream. I suggest closing the PR for now and wait for more feedback.
Regarding performances, I don't think it will matter much but I see that for growing tuples, a memset has been replaced by a loop. This may not necessarily be more efficient if we create lots of tuples that are directly discarded or unfilled (though I wonder whether this happens a lot).
Even if it does not break user code, the benefit of this change is questionable. Setting to Py_None is slower then setting to NULL. Checking for Py_None is slower then checking for NULL. If you need to discard the tuple item, you will need to do with refcount and destructor instead of simply comparing the pointer with NULL.
Unfortunately, even if we just look at the top-level packages, changing implementation details of the stable API can break private code or hide nasty bugs. What I found questionable is the memset being replaced by the loop when growing tuples. memset is very fast, but going through a loop is not necessarily the case (I'm actually surprised that don't use memset to initialize the tuple actually).
Thinking twice this change will break _PyTuple_MaybeUntrack's assumptions and could lead to memory leaks. The only way to remove NULL check is to deprecate PyTuple_New and remove it.
Thanks all for your feedback and sorry for bother.
Activity
picnixz commentedon Mar 14, 2026
I would be extra careful here. This is part of the stable ABI and while not explicitly documented, we should be careful that this change won't break downstream. I suggest closing the PR for now and wait for more feedback.
Regarding performances, I don't think it will matter much but I see that for growing tuples, a memset has been replaced by a loop. This may not necessarily be more efficient if we create lots of tuples that are directly discarded or unfilled (though I wonder whether this happens a lot).
picnixz commentedon Mar 14, 2026
cc @encukou @vstinner @serhiy-storchaka
encukou commentedon Mar 17, 2026
Yes, this looks like it can break users.
I don't see the reason behind the change; is it worth looking for what might break?
sergey-miryanov commentedon Mar 17, 2026
I can only think of one example where the user's code could be broken:
Do you mean look at the top of the pypi packages?
serhiy-storchaka commentedon Mar 17, 2026
Even if it does not break user code, the benefit of this change is questionable. Setting to Py_None is slower then setting to NULL. Checking for Py_None is slower then checking for NULL. If you need to discard the tuple item, you will need to do with refcount and destructor instead of simply comparing the pointer with NULL.
I do not it is worth to change this.
sergey-miryanov commentedon Mar 17, 2026
It is about the check on 149 line:
cpython/Objects/tupleobject.c
Lines 147 to 155 in 9d7621b
picnixz commentedon Mar 17, 2026
Unfortunately, even if we just look at the top-level packages, changing implementation details of the stable API can break private code or hide nasty bugs. What I found questionable is the
memsetbeing replaced by the loop when growing tuples.memsetis very fast, but going through a loop is not necessarily the case (I'm actually surprised that don't use memset to initialize the tuple actually).sergey-miryanov commentedon Mar 17, 2026
Thinking twice this change will break
_PyTuple_MaybeUntrack's assumptions and could lead to memory leaks. The only way to remove NULL check is to deprecatePyTuple_Newand remove it.Thanks all for your feedback and sorry for bother.
markshannon commentedon Mar 18, 2026
I'm +1 on deprecating
PyTuple_New, but it be many years before we can remove it.