https://github.com/python/cpython/commit/001b21d1c500857fb3721b019eeaf014b5ad76e8
commit: 001b21d1c500857fb3721b019eeaf014b5ad76e8
branch: main
author: mpage <[email protected]>
committer: colesbury <[email protected]>
date: 2024-03-15T09:56:13-04:00
summary:

gh-111926: Simplify weakref creation logic (#116843)

Since 3.12, allocating a GC object cannot immediately trigger GC. This
allows us to simplify the logic for creating the canonical callback-less
weakref.

files:
M Objects/weakrefobject.c

diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c
index df74be6aba7244..6264b1825eda73 100644
--- a/Objects/weakrefobject.c
+++ b/Objects/weakrefobject.c
@@ -801,24 +801,14 @@ PyWeakref_NewRef(PyObject *ob, PyObject *callback)
     if (result != NULL)
         Py_INCREF(result);
     else {
-        /* Note: new_weakref() can trigger cyclic GC, so the weakref
-           list on ob can be mutated.  This means that the ref and
-           proxy pointers we got back earlier may have been collected,
-           so we need to compute these values again before we use
-           them. */
+        /* We do not need to recompute ref/proxy; new_weakref() cannot
+           trigger GC.
+        */
         result = new_weakref(ob, callback);
         if (result != NULL) {
-            get_basic_refs(*list, &ref, &proxy);
             if (callback == NULL) {
-                if (ref == NULL)
-                    insert_head(result, list);
-                else {
-                    /* Someone else added a ref without a callback
-                       during GC.  Return that one instead of this one
-                       to avoid violating the invariants of the list
-                       of weakrefs for ob. */
-                    Py_SETREF(result, (PyWeakReference*)Py_NewRef(ref));
-                }
+                assert(ref == NULL);
+                insert_head(result, list);
             }
             else {
                 PyWeakReference *prev;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to