Skip to content

Add a public C-API function to iterate over GC’able objects#102013

@jbower-fb

Description

@jbower-fb

Feature or enhancement

An API similar to:

/* Visit all live GC-capable objects, similar to gc.get_objects(None). * * Users should avoid allocating or deallocating objects on the Python heap in * the callback. */ typedef void (*gcvisitobjects_t)(PyObject *, void *); PyAPI_FUNC(void) PyGC_VisitObjects(gcvisitobjects_t callback, void *arg); 

Which could be used as:

void count_functions(PyObject *op, void *arg){if (PyFunction_Check(op)){(*(int*)arg)++} } int get_num_functions(){int count; PyGC_VisitObjects(count_functions, &count); return count} 

Pitch

We have a version of this in Cinder already and right now and use it to identify all generator objects so they can be de-opted when our JIT is shutdown. In future we plan to use it for things like discovering existing PyFunction objects, and then using gh-91049 to mark them as JIT’able. This could facilitate loading the JIT feature at a later time (e.g. as part of a module).

[Edited] In general, there already exists a Python API for iterating over GC’able objects via gc.get_objects(), however there is no good way of doing this from native extensions. While it is technically possible to import the gc module and extract the gc_objects function in C this is cumbersome, and more importantly might lead to unexpected behavior if Python code has replaced the gc_objects function.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions