Skip to content

[3.15] gh-158401: Implement Py_INCREF() as function call in stable ABI - #162325

Open
vstinner wants to merge 1 commit into
python:3.15from
vstinner:limited_incref15
Open

vstinner wants to merge 1 commit into
python:3.15from
vstinner:limited_incref15

Conversation

@vstinner

@vstinner vstinner commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Py_INCREF(), Py_XINCREF(), Py_DECREF() and Py_XDECREF() are now implemented as function calls on all limited C API versions. Previously, they were only implemented as function calls on the limited C API version 3.12 or newer.

Previously, if a C extension was built with the limited C API version 3.11 or newer, Py_DECREF() was implemented as "op->ob_refcnt--". But Python 3.12 made Py_DECREF() more complicated to implement immortal objects in an efficient way. "op->ob_refcnt--" can convert an immortal object to a mortal object which causes various kinds of nasty bugs.

Backport to 3.15: mention the change in "Notable changes in 3.15.3" of What's New in Python 3.15.

(cherry picked from commit d1e3cae)

…python#162120)

Py_INCREF(), Py_XINCREF(), Py_DECREF() and Py_XDECREF() are now
implemented as function calls on all limited C API versions.
Previously, they were only implemented as function calls on the
limited C API version 3.12 or newer.

Previously, if a C extension was built with the limited C API version
3.11 or newer, Py_DECREF() was implemented as "op->ob_refcnt--". But
Python 3.12 made Py_DECREF() more complicated to implement immortal
objects in an efficient way. "op->ob_refcnt--" can convert an
immortal object to a mortal object which causes various kinds of
nasty bugs.

Backport to 3.15: mention the change in "Notable changes in 3.15.3"
of What's New in Python 3.15.

Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
(cherry picked from commit d1e3cae)
@vstinner

Copy link
Copy Markdown
Member Author

@encukou @da-woods: I propose backporting the limited C API change to 3.14 and 3.15 changes. Here is a first change to 3.15.

If this change is merged, I will then forward port the What's New in Python 3.15 change to the main branch (and same for What's New in Python 3.14).

I'm not sure about backporting the change to 3.11, 3.12 and 3.13 security branches.

@read-the-docs-community

Copy link
Copy Markdown

@da-woods

Copy link
Copy Markdown
Contributor

I propose backporting the limited C API change to 3.14 and 3.15

I don't really mind either way. The next Cython releases will call Py_IncRef/Py_DecRef directly in the Stable ABI so should be immune to this whatever you do.

I largely agree with Mark that the "real" fix is to the special-casing of the true/false checks. But I don't think it's a bad thing to compile to the opaque function calls as widely as possible.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants