1 /* Weak references objects for Python. */
3 #ifndef Py_WEAKREFOBJECT_H
4 #define Py_WEAKREFOBJECT_H
10 typedef struct _PyWeakReference PyWeakReference
;
12 /* PyWeakReference is the base struct for the Python ReferenceType, ProxyType,
13 * and CallableProxyType.
15 struct _PyWeakReference
{
18 /* The object to which this is a weak reference, or Py_None if none.
19 * Note that this is a stealth reference: wr_object's refcount is
20 * not incremented to reflect this pointer.
24 /* A callable to invoke when wr_object dies, or NULL if none. */
25 PyObject
*wr_callback
;
27 /* A cache for wr_object's hash code. As usual for hashes, this is -1
28 * if the hash code isn't known yet.
32 /* If wr_object is weakly referenced, wr_object has a doubly-linked NULL-
33 * terminated list of weak references to it. These are the list pointers.
34 * If wr_object goes away, wr_object is set to Py_None, and these pointers
35 * have no meaning then.
37 PyWeakReference
*wr_prev
;
38 PyWeakReference
*wr_next
;
41 PyAPI_DATA(PyTypeObject
) _PyWeakref_RefType
;
42 PyAPI_DATA(PyTypeObject
) _PyWeakref_ProxyType
;
43 PyAPI_DATA(PyTypeObject
) _PyWeakref_CallableProxyType
;
45 #define PyWeakref_CheckRef(op) PyObject_TypeCheck(op, &_PyWeakref_RefType)
46 #define PyWeakref_CheckRefExact(op) \
47 (Py_TYPE(op) == &_PyWeakref_RefType)
48 #define PyWeakref_CheckProxy(op) \
49 ((Py_TYPE(op) == &_PyWeakref_ProxyType) || \
50 (Py_TYPE(op) == &_PyWeakref_CallableProxyType))
52 /* This macro calls PyWeakref_CheckRef() last since that can involve a
53 function call; this makes it more likely that the function call
55 #define PyWeakref_Check(op) \
56 (PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op))
59 PyAPI_FUNC(PyObject
*) PyWeakref_NewRef(PyObject
*ob
,
61 PyAPI_FUNC(PyObject
*) PyWeakref_NewProxy(PyObject
*ob
,
63 PyAPI_FUNC(PyObject
*) PyWeakref_GetObject(PyObject
*ref
);
65 PyAPI_FUNC(Py_ssize_t
) _PyWeakref_GetWeakrefCount(PyWeakReference
*head
);
67 PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference
*self
);
69 #define PyWeakref_GET_OBJECT(ref) (((PyWeakReference *)(ref))->wr_object)
75 #endif /* !Py_WEAKREFOBJECT_H */