[issue43688] [C API] Fix Py_INCREF and Py_DECREF in the limited C API for Python built in debug mode

STINNER Victor report at bugs.python.org
Thu Apr 1 02:47:31 EDT 2021


New submission from STINNER Victor <vstinner at python.org>:

Currently, setup.py doesn't build xxlimited and xxlimited_35 extension modules with the limited C API if Python is built in debug mode. I only found two functions affected by Py_DEBUG macro in the limited C API: Py_INCREF() and Py_DECREF().

Example:
---
#if defined(Py_DEBUG) && !defined(Py_REF_DEBUG)
#define Py_REF_DEBUG
#endif

static inline void _Py_INCREF(PyObject *op)
{
#ifdef Py_REF_DEBUG
    _Py_RefTotal++;
#endif
    op->ob_refcnt++;
}
#define Py_INCREF(op) _Py_INCREF(_PyObject_CAST(op))
---

If Py_DEBUG is defined (Python built in debug mode), Py_INCREF() increments the private _Py_RefTotal variable.

The limited C API is supposed to provide a stable ABI, but Py_INCREF() leaks _Py_RefTotal implementation if Python is built in debug mode.

I propose to modify Py_INCREF() and Py_DECREF() of the limited C API to always declare them as opaque function calls. The regular (non limited) C API will continue to use the same static inline functions.

See also https://github.com/python/cpython/pull/25115 and bpo-41111 "[C API] Convert a few stdlib extensions to the limited C API (PEP 384)".

Note: Py_XINCREF() and Py_XDECREF() should be fine.

----------
components: C API
messages: 389956
nosy: vstinner
priority: normal
severity: normal
status: open
title: [C API] Fix Py_INCREF and Py_DECREF in the limited C API for Python built in debug mode
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43688>
_______________________________________


More information about the Python-bugs-list mailing list