diff options
author | mpage <mpage@meta.com> | 2024-03-15 13:58:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-15 13:58:40 (GMT) |
commit | ce2c996b2f645cd886d05f6ef8f1ba60ced7d4b7 (patch) | |
tree | 2a5e7f8742e2913d69154d5142c3a40189cb5bfd /Objects | |
parent | 001b21d1c500857fb3721b019eeaf014b5ad76e8 (diff) | |
download | cpython-ce2c996b2f645cd886d05f6ef8f1ba60ced7d4b7.zip cpython-ce2c996b2f645cd886d05f6ef8f1ba60ced7d4b7.tar.gz cpython-ce2c996b2f645cd886d05f6ef8f1ba60ced7d4b7.tar.bz2 |
gh-111926: Simplify proxy creation logic (#116844)
Since 3.12, allocating a GC-able object cannot trigger GC. This allows
us to simplify the logic for creating the canonical callback-less
proxy object.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/weakrefobject.c | 19 |
1 files changed, 3 insertions, 16 deletions
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 6264b18..b7b2906 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -848,11 +848,9 @@ PyWeakref_NewProxy(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) { PyWeakReference *prev; @@ -863,16 +861,7 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback) else { Py_SET_TYPE(result, &_PyWeakref_ProxyType); } - get_basic_refs(*list, &ref, &proxy); if (callback == NULL) { - if (proxy != NULL) { - /* Someone else added a proxy 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(proxy)); - goto skip_insert; - } prev = ref; } else @@ -882,8 +871,6 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback) insert_head(result, list); else insert_after(result, prev); - skip_insert: - ; } } return (PyObject *) result; |