FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Please add `PyObject_CallFinalizerFromDealloc()` to stable ABI · Issue #146063 · python/cpython · GitHub

Repository navigation

Please add PyObject_CallFinalizerFromDealloc() to stable ABI #146063

Closed

Description

Feature or enhancement

Proposal:

Please add PyObject_CallFinalizer and PyObject_CallFinalizerFromDealloc to the stable ABI.

The documentation for PyObject_CallFinalizer and PyObject_CallFinalizerFromDealloc state:

Call this function (or PyObject_CallFinalizerFromDealloc()) instead of calling tp_finalize directly because this function may deduplicate multiple calls to tp_finalize. Currently, calls are only deduplicated if the type supports garbage collection (i.e., the Py_TPFLAGS_HAVE_GC flag is set); this may change in the future.

These functions are not yet in the stable ABI. I believe it is possible to approximate their current behavior using PyObject_GC_IsFinalized (which is stable ABI) to decide whether to call the finalizer. Given the documentation states that the logic for deduplicating calls may change in future, it seems better to just add the functions to the stable ABI rather than leave downstream users to recreate their logic.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. added a commit that references this issue on Mar 19, 2026
  2. vstinner commented on Mar 19, 2026

    Member

    I wrote #146172 to add PyObject_CallFinalizer() and PyObject_CallFinalizerFromDealloc() to the limited C API.

  3. colesbury commented on Mar 19, 2026

    Contributor

    Exposing PyObject_CallFinalizerFromDealloc makes sense, but is PyObject_CallFinalizer needed? We have only one internal usage in _PyIOBase_finalize, and I'm not even sure that code path is ever taken.

    I don't really see any external usages, except people re-exposing or binding it: https://grep.app/search?regexp=true&q=PyObject_CallFinalizer%5C%28

  4. davidhewitt commented on Mar 19, 2026

    ContributorAuthor

    I could support having just PyObject_CallFinalizerFromDealloc, that was the indeed the one that we were looking at in PyO3.

  5. changed the title [-]Please add `PyObject_CallFinalizer` (& `FromDealloc` variant) to stable ABI[/-] [+]Please add `PyObject_CallFinalizerFromDealloc()` to stable ABI[/+] on Mar 20, 2026
  6. da-woods commented on Mar 23, 2026

    Contributor

    Cython has missed this API too. I think I came to the conclusion that I could reimplement PyObject_CallFinalizer (with PyType_GetSlot and a function call, but I couldn't see any way of marking an object as finalized after calling it.

    (Edit: Looking at the code, it's actually marked as finalized inside PyObject_CallFinalizer so it wouldn't be possible to reimplement this.... Either way, we have no real need to manually call a finalizer outside of dealloc)

    Here's a previous discussion that didn't get a whole lot of interest: https://discuss.python.org/t/fully-support-using-the-tp-finalize-slot-of-a-pyobject-in-the-limited-api/69729

  7. encukou commented on Apr 14, 2026

    Member
  8. added a commit that references this issue on May 4, 2026
  9. vstinner commented on May 13, 2026

    Member

    PyObject_CallFinalizerFromDealloc() has been added to Python 3.15: https://docs.python.org/dev/c-api/lifecycle.html#c.PyObject_CallFinalizerFromDealloc. The function was added to the stable ABI as well.

  10. davidhewitt commented on May 13, 2026

    ContributorAuthor

    Many thanks!

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


      Back | FazBrowse Home | New Git URL