Repository navigation
Conversation
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.
|
I checked manually the change by looking at xx_modexec() assembly code on x86-64 of the xxlimited_35 extension (built with C code: gdb: |
|
Ah, the change also replaces See #158401 (comment) for the rationale of this change. |
da-woods
left a comment
There was a problem hiding this comment.
My overall view:
It obviously simplifies it a bit to go through function calls for all Limited API versions, and it helps with any future changes to reference counting.
If you compile with the Python 3.14 headers (on any Limited API version) then it does handle immortal reference counting correctly (because the logic is inlined). So this doesn't really fix anything. But it might be worthwhile for the simplification.
Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com>
|
@ngoldbaum suggests backporting the change to all bugfix branches and even security branches: https://discuss.python.org/t/limited-c-api-implement-py-incref-and-py-decref-as-function-calls/27592/22. |
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.