Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:42

0001 #ifndef Py_CPYTHON_WEAKREFOBJECT_H
0002 #  error "this header file must not be included directly"
0003 #endif
0004 
0005 /* PyWeakReference is the base struct for the Python ReferenceType, ProxyType,
0006  * and CallableProxyType.
0007  */
0008 struct _PyWeakReference {
0009     PyObject_HEAD
0010 
0011     /* The object to which this is a weak reference, or Py_None if none.
0012      * Note that this is a stealth reference:  wr_object's refcount is
0013      * not incremented to reflect this pointer.
0014      */
0015     PyObject *wr_object;
0016 
0017     /* A callable to invoke when wr_object dies, or NULL if none. */
0018     PyObject *wr_callback;
0019 
0020     /* A cache for wr_object's hash code.  As usual for hashes, this is -1
0021      * if the hash code isn't known yet.
0022      */
0023     Py_hash_t hash;
0024 
0025     /* If wr_object is weakly referenced, wr_object has a doubly-linked NULL-
0026      * terminated list of weak references to it.  These are the list pointers.
0027      * If wr_object goes away, wr_object is set to Py_None, and these pointers
0028      * have no meaning then.
0029      */
0030     PyWeakReference *wr_prev;
0031     PyWeakReference *wr_next;
0032     vectorcallfunc vectorcall;
0033 };
0034 
0035 PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);
0036 
0037 PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
0038 
0039 static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_obj) {
0040     PyWeakReference *ref;
0041     PyObject *obj;
0042     assert(PyWeakref_Check(ref_obj));
0043     ref = _Py_CAST(PyWeakReference*, ref_obj);
0044     obj = ref->wr_object;
0045     // Explanation for the Py_REFCNT() check: when a weakref's target is part
0046     // of a long chain of deallocations which triggers the trashcan mechanism,
0047     // clearing the weakrefs can be delayed long after the target's refcount
0048     // has dropped to zero.  In the meantime, code accessing the weakref will
0049     // be able to "see" the target object even though it is supposed to be
0050     // unreachable.  See issue gh-60806.
0051     if (Py_REFCNT(obj) > 0) {
0052         return obj;
0053     }
0054     return Py_None;
0055 }
0056 #define PyWeakref_GET_OBJECT(ref) PyWeakref_GET_OBJECT(_PyObject_CAST(ref))