Skip to content

Fill newly created tuple with Py_None instead of NULL #145914

Closed as not planned
Closed as not planned

Description

@sergey-miryanov

Feature or enhancement

Proposal:

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.

cc @markshannon

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. picnixz commented on Mar 14, 2026

    @picnixz
    Member

    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).

  2. picnixz commented on Mar 14, 2026

    @picnixz
    Member
  3. deleted a comment from khalidsaidi on Mar 14, 2026
  4. encukou commented on Mar 17, 2026

    @encukou
    Member

    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?

  5. sergey-miryanov commented on Mar 17, 2026

    @sergey-miryanov
    ContributorAuthor

    I can only think of one example where the user's code could be broken:

    PyObject *tuple = PyTuple_New(n);
    // ... skip lines
    if (PyTuple_GET_ITEMS(tuple, i) != NULL) {
      PyTuple_SET_ITEM(tuple, i, object);
    }
    

    is it worth looking for what might break?

    Do you mean look at the top of the pypi packages?

  6. serhiy-storchaka commented on Mar 17, 2026

    @serhiy-storchaka
    Member

    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.

  7. sergey-miryanov commented on Mar 17, 2026

    @sergey-miryanov
    ContributorAuthor

    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.

    It is about the check on 149 line:

    for (i = 0; i < n; i++) {
    PyObject *elt = PyTuple_GET_ITEM(t, i);
    /* Tuple with NULL elements aren't
    fully constructed, don't untrack
    them yet. */
    if (!elt ||
    _PyObject_GC_MAY_BE_TRACKED(elt))
    return;
    }

  8. added
    pendingThe issue will be closed if no feedback is provided
    on Mar 17, 2026
  9. picnixz commented on Mar 17, 2026

    @picnixz
    Member

    Do you mean look at the top of the pypi packages?

    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).

  10. sergey-miryanov commented on Mar 17, 2026

    @sergey-miryanov
    ContributorAuthor

    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.

  11. markshannon commented on Mar 18, 2026

    @markshannon
    Member

    I'm +1 on deprecating PyTuple_New, but it be many years before we can remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedtopic-C-APItype-featureA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions