Skip to content

Commit 3294f2e

Browse files
Add is_subclass to PyWeakReference and do not clear those weakrefs in GC
1 parent bc0297c commit 3294f2e

File tree

3 files changed

+5
-0
lines changed

3 files changed

+5
-0
lines changed

Include/cpython/weakrefobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct _PyWeakReference {
3838
*/
3939
PyMutex *weakrefs_lock;
4040
#endif
41+
uint8_t is_subclass;
4142
};
4243

4344
PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);

Objects/typeobject.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9265,6 +9265,9 @@ add_subclass(PyTypeObject *base, PyTypeObject *type)
92659265
return -1;
92669266
}
92679267

9268+
((PyWeakReference*)ref)->is_subclass = 1;
9269+
9270+
92689271
// Only get tp_subclasses after creating the key and value.
92699272
// PyWeakref_NewRef() can trigger a garbage collection which can execute
92709273
// arbitrary Python code and so modify base->tp_subclasses.

Objects/weakrefobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ init_weakref(PyWeakReference *self, PyObject *ob, PyObject *callback)
7272
_PyObject_SetMaybeWeakref(ob);
7373
_PyObject_SetMaybeWeakref((PyObject *)self);
7474
#endif
75+
self->is_subclass = 0;
7576
}
7677

7778
// Clear the weakref and steal its callback into `callback`, if provided.

0 commit comments

Comments
 (0)