summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authormpage <mpage@meta.com>2024-03-15 13:56:13 (GMT)
committerGitHub <noreply@github.com>2024-03-15 13:56:13 (GMT)
commit001b21d1c500857fb3721b019eeaf014b5ad76e8 (patch)
tree0a7618952d1add449810b35ab19a38762d96cd43 /Objects
parent2cf18a44303b6d84faa8ecffaecc427b53ae121e (diff)
downloadcpython-001b21d1c500857fb3721b019eeaf014b5ad76e8.zip
cpython-001b21d1c500857fb3721b019eeaf014b5ad76e8.tar.gz
cpython-001b21d1c500857fb3721b019eeaf014b5ad76e8.tar.bz2
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.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/weakrefobject.c20
1 files changed, 5 insertions, 15 deletions
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c
index df74be6..6264b18 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;