bpo-43688: Support the limited C API in debug mode (GH-25131)
The limited C API is now supported if Python is built in debug mode
(if the Py_DEBUG macro is defined). In the limited C API, the
Py_INCREF() and Py_DECREF() functions are now implemented as opaque
function calls, rather than accessing directly the PyObject.ob_refcnt
member, if Python is built in debug mode and the Py_LIMITED_API macro
targets Python 3.10 or newer. It became possible to support the
limited C API in debug mode because the PyObject structure is the
same in release and debug mode since Python 3.8 (see bpo-36465).
The limited C API is still not supported in the --with-trace-refs
special build (Py_TRACE_REFS macro).
diff --git a/Objects/object.c b/Objects/object.c
index 0a8621b..0845966 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -13,6 +13,11 @@
#include "frameobject.h"
#include "interpreteridobject.h"
+#ifdef Py_LIMITED_API
+ // Prevent recursive call _Py_IncRef() <=> Py_INCREF()
+# error "Py_LIMITED_API macro must not be defined"
+#endif
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -136,6 +141,18 @@ Py_DecRef(PyObject *o)
Py_XDECREF(o);
}
+void
+_Py_IncRef(PyObject *o)
+{
+ Py_INCREF(o);
+}
+
+void
+_Py_DecRef(PyObject *o)
+{
+ Py_DECREF(o);
+}
+
PyObject *
PyObject_Init(PyObject *op, PyTypeObject *tp)
{