[Python-checkins] gh-105927: Add _PyWeakref_IS_DEAD() function (#105992)

vstinner webhook-mailer at python.org
Thu Jun 22 15:56:48 EDT 2023


https://github.com/python/cpython/commit/c38da1e3e19a7bf1053c6d52e730e970efeceff6
commit: c38da1e3e19a7bf1053c6d52e730e970efeceff6
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2023-06-22T21:56:44+02:00
summary:

gh-105927: Add _PyWeakref_IS_DEAD() function (#105992)

* Add _PyWeakref_IS_DEAD() internal function.
* Modify is_dead_weakref() of Modules/_weakref.c and
  _pysqlite_drop_unused_cursor_references() to replace
  PyWeakref_GET_OBJECT() with _PyWeakref_IS_DEAD().
* Replace "int i" with "Py_ssize_t i" to iterate on cursors
  in _pysqlite_drop_unused_cursor_references().

files:
M Include/internal/pycore_weakref.h
M Modules/_sqlite/connection.c
M Modules/_weakref.c

diff --git a/Include/internal/pycore_weakref.h b/Include/internal/pycore_weakref.h
index 4e2b979c91928..f31b12463bb3e 100644
--- a/Include/internal/pycore_weakref.h
+++ b/Include/internal/pycore_weakref.h
@@ -33,6 +33,19 @@ static inline PyObject* _PyWeakref_GET_REF(PyObject *ref_obj) {
     return Py_NewRef(obj);
 }
 
+static inline int _PyWeakref_IS_DEAD(PyObject *ref_obj) {
+    assert(PyWeakref_Check(ref_obj));
+    PyWeakReference *ref = _Py_CAST(PyWeakReference*, ref_obj);
+    PyObject *obj = ref->wr_object;
+    if (obj == Py_None) {
+        // clear_weakref() was called
+        return 1;
+    }
+
+    // See _PyWeakref_GET_REF() for the rationale of this test
+    return (Py_REFCNT(obj) == 0);
+}
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index d12d65500c654..967ba2812080e 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -21,6 +21,10 @@
  * 3. This notice may not be removed or altered from any source distribution.
  */
 
+#ifndef Py_BUILD_CORE_BUILTIN
+#  define Py_BUILD_CORE_MODULE 1
+#endif
+
 #include "module.h"
 #include "structmember.h"         // PyMemberDef
 #include "connection.h"
@@ -29,6 +33,7 @@
 #include "blob.h"
 #include "prepare_protocol.h"
 #include "util.h"
+#include "pycore_weakref.h"       // _PyWeakref_IS_DEAD()
 
 #include <stdbool.h>
 
@@ -969,10 +974,6 @@ final_callback(sqlite3_context *context)
 
 static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
 {
-    PyObject* new_list;
-    PyObject* weakref;
-    int i;
-
     /* we only need to do this once in a while */
     if (self->created_cursors++ < 200) {
         return;
@@ -980,18 +981,19 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
 
     self->created_cursors = 0;
 
-    new_list = PyList_New(0);
+    PyObject* new_list = PyList_New(0);
     if (!new_list) {
         return;
     }
 
-    for (i = 0; i < PyList_Size(self->cursors); i++) {
-        weakref = PyList_GetItem(self->cursors, i);
-        if (PyWeakref_GetObject(weakref) != Py_None) {
-            if (PyList_Append(new_list, weakref) != 0) {
-                Py_DECREF(new_list);
-                return;
-            }
+    for (Py_ssize_t i = 0; i < PyList_Size(self->cursors); i++) {
+        PyObject* weakref = PyList_GetItem(self->cursors, i);
+        if (_PyWeakref_IS_DEAD(weakref)) {
+            continue;
+        }
+        if (PyList_Append(new_list, weakref) != 0) {
+            Py_DECREF(new_list);
+            return;
         }
     }
 
diff --git a/Modules/_weakref.c b/Modules/_weakref.c
index 387b8fa9d0a6f..21ff0e2b7a984 100644
--- a/Modules/_weakref.c
+++ b/Modules/_weakref.c
@@ -1,5 +1,6 @@
 #include "Python.h"
-#include "pycore_object.h"   // _PyObject_GET_WEAKREFS_LISTPTR
+#include "pycore_object.h"        // _PyObject_GET_WEAKREFS_LISTPTR
+#include "pycore_weakref.h"       // _PyWeakref_IS_DEAD()
 
 
 #define GET_WEAKREFS_LISTPTR(o) \
@@ -43,7 +44,7 @@ is_dead_weakref(PyObject *value)
         PyErr_SetString(PyExc_TypeError, "not a weakref");
         return -1;
     }
-    return PyWeakref_GET_OBJECT(value) == Py_None;
+    return _PyWeakref_IS_DEAD(value);
 }
 
 /*[clinic input]



More information about the Python-checkins mailing list