Skip to content

Avoid boilerplate for traversing a mutable heap type #138342

Closed

Description

@ZeroIntensity

Throughout the codebase, instances of mutable heap types must always implement the GC protocol to traverse their type. This leads to the following being repeated a lot:

static int
object_visit(PyObject *op, visitproc visit, void *arg)
{
    Py_VISIT(Py_TYPE(op));
    return 0;
}

To reduce code duplication, I think it would be beneficial to have a common helper for this case. I'm going to call this _PyObject_VisitType for now.

cc @picnixz

Linked PRs

Activity

  1. serhiy-storchaka commented on Sep 1, 2025

    @serhiy-storchaka
    Member

    When this boilerplate was added at first place, I suggested to do this automatically for each heap type with the Py_TPFLAGS_HAVE_GC flag. It belongs to the garbage collector (which does type-specific things in any case), no need to pollute the source code with Py_VISIT(Py_TYPE(op)). For some reasons my suggestion was ignored.

  2. picnixz commented on Sep 1, 2025

    @picnixz
    Member

    I think you were talking about #84398 right?

  3. serhiy-storchaka commented on Sep 1, 2025

    @serhiy-storchaka
    Member

    Exactly. Now, after adding all these Py_VISIT(Py_TYPE(op)), we cannot use that approach and are doomed to add Py_VISIT(Py_TYPE(op)) in each tp_traverse.

  4. added a commit that references this issue on Sep 1, 2025
  5. added a commit that references this issue on Sep 9, 2025
  6. added 2 commits that reference this issue on Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions