https://github.com/python/cpython/commit/fd545d735d5f9c048f99767c700f72853a9b7acd
commit: fd545d735d5f9c048f99767c700f72853a9b7acd
branch: main
author: Mark Shannon <m...@hotpy.org>
committer: markshannon <m...@hotpy.org>
date: 2025-03-17T17:23:50Z
summary:

GH-127705: Move mortal decrefs to internal header and make sure 
_PyReftracerTrack is called

files:
M Include/internal/pycore_object.h
M Include/refcount.h

diff --git a/Include/internal/pycore_object.h b/Include/internal/pycore_object.h
index ce86f9b93fe7ab..931d9a2bb300c5 100644
--- a/Include/internal/pycore_object.h
+++ b/Include/internal/pycore_object.h
@@ -430,6 +430,74 @@ _Py_DECREF_CODE(PyCodeObject *co)
 }
 #endif
 
+#ifndef Py_GIL_DISABLED
+#ifdef Py_REF_DEBUG
+
+static inline void Py_DECREF_MORTAL(const char *filename, int lineno, PyObject 
*op)
+{
+    if (op->ob_refcnt <= 0) {
+        _Py_NegativeRefcount(filename, lineno, op);
+    }
+    _Py_DECREF_STAT_INC();
+    assert(!_Py_IsStaticImmortal(op));
+    if (!_Py_IsImmortal(op)) {
+        _Py_DECREF_DecRefTotal();
+    }
+    if (--op->ob_refcnt == 0) {
+#ifdef Py_TRACE_REFS
+        _Py_ForgetReference(op);
+#endif
+        _Py_Dealloc(op);
+    }
+}
+#define Py_DECREF_MORTAL(op) Py_DECREF_MORTAL(__FILE__, __LINE__, 
_PyObject_CAST(op))
+
+static inline void _Py_DECREF_MORTAL_SPECIALIZED(const char *filename, int 
lineno, PyObject *op, destructor destruct)
+{
+    if (op->ob_refcnt <= 0) {
+        _Py_NegativeRefcount(filename, lineno, op);
+    }
+    _Py_DECREF_STAT_INC();
+    assert(!_Py_IsStaticImmortal(op));
+    if (!_Py_IsImmortal(op)) {
+        _Py_DECREF_DecRefTotal();
+    }
+    if (--op->ob_refcnt == 0) {
+#ifdef Py_TRACE_REFS
+        _Py_ForgetReference(op);
+#endif
+        _PyReftracerTrack(op, PyRefTracer_DESTROY);
+        destruct(op);
+    }
+}
+#define Py_DECREF_MORTAL_SPECIALIZED(op, destruct) 
_Py_DECREF_MORTAL_SPECIALIZED(__FILE__, __LINE__, op, destruct)
+
+#else
+
+static inline void Py_DECREF_MORTAL(PyObject *op)
+{
+    assert(!_Py_IsStaticImmortal(op));
+    _Py_DECREF_STAT_INC();
+    if (--op->ob_refcnt == 0) {
+        _Py_Dealloc(op);
+    }
+}
+#define Py_DECREF_MORTAL(op) Py_DECREF_MORTAL(_PyObject_CAST(op))
+
+static inline void Py_DECREF_MORTAL_SPECIALIZED(PyObject *op, destructor 
destruct)
+{
+    assert(!_Py_IsStaticImmortal(op));
+    _Py_DECREF_STAT_INC();
+    if (--op->ob_refcnt == 0) {
+        _PyReftracerTrack(op, PyRefTracer_DESTROY);
+        destruct(op);
+    }
+}
+#define Py_DECREF_MORTAL_SPECIALIZED(op, destruct) 
Py_DECREF_MORTAL_SPECIALIZED(_PyObject_CAST(op), destruct)
+
+#endif
+#endif
+
 /* Inline functions trading binary compatibility for speed:
    _PyObject_Init() is the fast version of PyObject_Init(), and
    _PyObject_InitVar() is the fast version of PyObject_InitVar().
diff --git a/Include/refcount.h b/Include/refcount.h
index 417d91bfa0da92..177bbdaf0c5977 100644
--- a/Include/refcount.h
+++ b/Include/refcount.h
@@ -394,42 +394,6 @@ static inline void Py_DECREF(PyObject *op)
 #define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op))
 
 #elif defined(Py_REF_DEBUG)
-static inline void Py_DECREF_MORTAL(const char *filename, int lineno, PyObject 
*op)
-{
-    if (op->ob_refcnt <= 0) {
-        _Py_NegativeRefcount(filename, lineno, op);
-    }
-    _Py_DECREF_STAT_INC();
-    assert(!_Py_IsStaticImmortal(op));
-    if (!_Py_IsImmortal(op)) {
-        _Py_DECREF_DecRefTotal();
-    }
-    if (--op->ob_refcnt == 0) {
-        _Py_Dealloc(op);
-    }
-}
-#define Py_DECREF_MORTAL(op) Py_DECREF_MORTAL(__FILE__, __LINE__, 
_PyObject_CAST(op))
-
-
-
-static inline void _Py_DECREF_MORTAL_SPECIALIZED(const char *filename, int 
lineno, PyObject *op, destructor destruct)
-{
-    if (op->ob_refcnt <= 0) {
-        _Py_NegativeRefcount(filename, lineno, op);
-    }
-    _Py_DECREF_STAT_INC();
-    assert(!_Py_IsStaticImmortal(op));
-    if (!_Py_IsImmortal(op)) {
-        _Py_DECREF_DecRefTotal();
-    }
-    if (--op->ob_refcnt == 0) {
-#ifdef Py_TRACE_REFS
-        _Py_ForgetReference(op);
-#endif
-        destruct(op);
-    }
-}
-#define Py_DECREF_MORTAL_SPECIALIZED(op, destruct) 
_Py_DECREF_MORTAL_SPECIALIZED(__FILE__, __LINE__, op, destruct)
 
 static inline void Py_DECREF(const char *filename, int lineno, PyObject *op)
 {
@@ -455,28 +419,6 @@ static inline void Py_DECREF(const char *filename, int 
lineno, PyObject *op)
 #define Py_DECREF(op) Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
 
 #else
-static inline void Py_DECREF_MORTAL(PyObject *op)
-{
-    assert(!_Py_IsStaticImmortal(op));
-    _Py_DECREF_STAT_INC();
-    if (--op->ob_refcnt == 0) {
-        _Py_Dealloc(op);
-    }
-}
-#define Py_DECREF_MORTAL(op) Py_DECREF_MORTAL(_PyObject_CAST(op))
-
-static inline void Py_DECREF_MORTAL_SPECIALIZED(PyObject *op, destructor 
destruct)
-{
-    assert(!_Py_IsStaticImmortal(op));
-    _Py_DECREF_STAT_INC();
-    if (--op->ob_refcnt == 0) {
-#ifdef Py_TRACE_REFS
-        _Py_ForgetReference(op);
-#endif
-        destruct(op);
-    }
-}
-#define Py_DECREF_MORTAL_SPECIALIZED(op, destruct) 
Py_DECREF_MORTAL_SPECIALIZED(_PyObject_CAST(op), destruct)
 
 static inline Py_ALWAYS_INLINE void Py_DECREF(PyObject *op)
 {

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to