weakrefobject.h 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* Weak references objects for Python. */

#ifndef Py_WEAKREFOBJECT_H
#define Py_WEAKREFOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif


typedef struct _PyWeakReference PyWeakReference;

12 13 14
/* PyWeakReference is the base struct for the Python ReferenceType, ProxyType,
 * and CallableProxyType.
 */
15
#ifndef Py_LIMITED_API
16 17
struct _PyWeakReference {
    PyObject_HEAD
18 19 20 21 22

    /* The object to which this is a weak reference, or Py_None if none.
     * Note that this is a stealth reference:  wr_object's refcount is
     * not incremented to reflect this pointer.
     */
23
    PyObject *wr_object;
24 25

    /* A callable to invoke when wr_object dies, or NULL if none. */
26
    PyObject *wr_callback;
27 28 29 30

    /* A cache for wr_object's hash code.  As usual for hashes, this is -1
     * if the hash code isn't known yet.
     */
31
    Py_hash_t hash;
32 33 34 35 36 37

    /* If wr_object is weakly referenced, wr_object has a doubly-linked NULL-
     * terminated list of weak references to it.  These are the list pointers.
     * If wr_object goes away, wr_object is set to Py_None, and these pointers
     * have no meaning then.
     */
38 39 40
    PyWeakReference *wr_prev;
    PyWeakReference *wr_next;
};
41
#endif
42

43 44 45
PyAPI_DATA(PyTypeObject) _PyWeakref_RefType;
PyAPI_DATA(PyTypeObject) _PyWeakref_ProxyType;
PyAPI_DATA(PyTypeObject) _PyWeakref_CallableProxyType;
46

47 48
#define PyWeakref_CheckRef(op) PyObject_TypeCheck(op, &_PyWeakref_RefType)
#define PyWeakref_CheckRefExact(op) \
49
        (Py_TYPE(op) == &_PyWeakref_RefType)
50
#define PyWeakref_CheckProxy(op) \
51 52
        ((Py_TYPE(op) == &_PyWeakref_ProxyType) || \
         (Py_TYPE(op) == &_PyWeakref_CallableProxyType))
53

54 55 56 57
#define PyWeakref_Check(op) \
        (PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op))


58
PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob,
59
                                              PyObject *callback);
60
PyAPI_FUNC(PyObject *) PyWeakref_NewProxy(PyObject *ob,
61
                                                PyObject *callback);
62
PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref);
63

64
#ifndef Py_LIMITED_API
65
PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);
66

67
PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
68
#endif
69

70 71 72 73 74 75 76 77 78 79 80
/* Explanation for the Py_REFCNT() check: when a weakref's target is part
   of a long chain of deallocations which triggers the trashcan mechanism,
   clearing the weakrefs can be delayed long after the target's refcount
   has dropped to zero.  In the meantime, code accessing the weakref will
   be able to "see" the target object even though it is supposed to be
   unreachable.  See issue #16602. */

#define PyWeakref_GET_OBJECT(ref)                           \
    (Py_REFCNT(((PyWeakReference *)(ref))->wr_object) > 0   \
     ? ((PyWeakReference *)(ref))->wr_object                \
     : Py_None)
81 82 83 84 85 86


#ifdef __cplusplus
}
#endif
#endif /* !Py_WEAKREFOBJECT_H */