Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:43

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 #ifdef Py_GIL_DISABLED
0035     /* Pointer to the lock used when clearing in free-threaded builds.
0036      * Normally this can be derived from wr_object, but in some cases we need
0037      * to lock after wr_object has been set to Py_None.
0038      */
0039     PyMutex *weakrefs_lock;
0040 #endif
0041 };
0042 
0043 PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
0044 
0045 Py_DEPRECATED(3.13) static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_obj)
0046 {
0047     PyWeakReference *ref;
0048     PyObject *obj;
0049     assert(PyWeakref_Check(ref_obj));
0050     ref = _Py_CAST(PyWeakReference*, ref_obj);
0051     obj = ref->wr_object;
0052     // Explanation for the Py_REFCNT() check: when a weakref's target is part
0053     // of a long chain of deallocations which triggers the trashcan mechanism,
0054     // clearing the weakrefs can be delayed long after the target's refcount
0055     // has dropped to zero.  In the meantime, code accessing the weakref will
0056     // be able to "see" the target object even though it is supposed to be
0057     // unreachable.  See issue gh-60806.
0058     if (Py_REFCNT(obj) > 0) {
0059         return obj;
0060     }
0061     return Py_None;
0062 }
0063 #define PyWeakref_GET_OBJECT(ref) PyWeakref_GET_OBJECT(_PyObject_CAST(ref))