jpayne@69
|
1 /* Weak references objects for Python. */
|
jpayne@69
|
2
|
jpayne@69
|
3 #ifndef Py_WEAKREFOBJECT_H
|
jpayne@69
|
4 #define Py_WEAKREFOBJECT_H
|
jpayne@69
|
5 #ifdef __cplusplus
|
jpayne@69
|
6 extern "C" {
|
jpayne@69
|
7 #endif
|
jpayne@69
|
8
|
jpayne@69
|
9
|
jpayne@69
|
10 typedef struct _PyWeakReference PyWeakReference;
|
jpayne@69
|
11
|
jpayne@69
|
12 /* PyWeakReference is the base struct for the Python ReferenceType, ProxyType,
|
jpayne@69
|
13 * and CallableProxyType.
|
jpayne@69
|
14 */
|
jpayne@69
|
15 #ifndef Py_LIMITED_API
|
jpayne@69
|
16 struct _PyWeakReference {
|
jpayne@69
|
17 PyObject_HEAD
|
jpayne@69
|
18
|
jpayne@69
|
19 /* The object to which this is a weak reference, or Py_None if none.
|
jpayne@69
|
20 * Note that this is a stealth reference: wr_object's refcount is
|
jpayne@69
|
21 * not incremented to reflect this pointer.
|
jpayne@69
|
22 */
|
jpayne@69
|
23 PyObject *wr_object;
|
jpayne@69
|
24
|
jpayne@69
|
25 /* A callable to invoke when wr_object dies, or NULL if none. */
|
jpayne@69
|
26 PyObject *wr_callback;
|
jpayne@69
|
27
|
jpayne@69
|
28 /* A cache for wr_object's hash code. As usual for hashes, this is -1
|
jpayne@69
|
29 * if the hash code isn't known yet.
|
jpayne@69
|
30 */
|
jpayne@69
|
31 Py_hash_t hash;
|
jpayne@69
|
32
|
jpayne@69
|
33 /* If wr_object is weakly referenced, wr_object has a doubly-linked NULL-
|
jpayne@69
|
34 * terminated list of weak references to it. These are the list pointers.
|
jpayne@69
|
35 * If wr_object goes away, wr_object is set to Py_None, and these pointers
|
jpayne@69
|
36 * have no meaning then.
|
jpayne@69
|
37 */
|
jpayne@69
|
38 PyWeakReference *wr_prev;
|
jpayne@69
|
39 PyWeakReference *wr_next;
|
jpayne@69
|
40 };
|
jpayne@69
|
41 #endif
|
jpayne@69
|
42
|
jpayne@69
|
43 PyAPI_DATA(PyTypeObject) _PyWeakref_RefType;
|
jpayne@69
|
44 PyAPI_DATA(PyTypeObject) _PyWeakref_ProxyType;
|
jpayne@69
|
45 PyAPI_DATA(PyTypeObject) _PyWeakref_CallableProxyType;
|
jpayne@69
|
46
|
jpayne@69
|
47 #define PyWeakref_CheckRef(op) PyObject_TypeCheck(op, &_PyWeakref_RefType)
|
jpayne@69
|
48 #define PyWeakref_CheckRefExact(op) \
|
jpayne@69
|
49 (Py_TYPE(op) == &_PyWeakref_RefType)
|
jpayne@69
|
50 #define PyWeakref_CheckProxy(op) \
|
jpayne@69
|
51 ((Py_TYPE(op) == &_PyWeakref_ProxyType) || \
|
jpayne@69
|
52 (Py_TYPE(op) == &_PyWeakref_CallableProxyType))
|
jpayne@69
|
53
|
jpayne@69
|
54 #define PyWeakref_Check(op) \
|
jpayne@69
|
55 (PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op))
|
jpayne@69
|
56
|
jpayne@69
|
57
|
jpayne@69
|
58 PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob,
|
jpayne@69
|
59 PyObject *callback);
|
jpayne@69
|
60 PyAPI_FUNC(PyObject *) PyWeakref_NewProxy(PyObject *ob,
|
jpayne@69
|
61 PyObject *callback);
|
jpayne@69
|
62 PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref);
|
jpayne@69
|
63
|
jpayne@69
|
64 #ifndef Py_LIMITED_API
|
jpayne@69
|
65 PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);
|
jpayne@69
|
66
|
jpayne@69
|
67 PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
|
jpayne@69
|
68 #endif
|
jpayne@69
|
69
|
jpayne@69
|
70 /* Explanation for the Py_REFCNT() check: when a weakref's target is part
|
jpayne@69
|
71 of a long chain of deallocations which triggers the trashcan mechanism,
|
jpayne@69
|
72 clearing the weakrefs can be delayed long after the target's refcount
|
jpayne@69
|
73 has dropped to zero. In the meantime, code accessing the weakref will
|
jpayne@69
|
74 be able to "see" the target object even though it is supposed to be
|
jpayne@69
|
75 unreachable. See issue #16602. */
|
jpayne@69
|
76
|
jpayne@69
|
77 #define PyWeakref_GET_OBJECT(ref) \
|
jpayne@69
|
78 (Py_REFCNT(((PyWeakReference *)(ref))->wr_object) > 0 \
|
jpayne@69
|
79 ? ((PyWeakReference *)(ref))->wr_object \
|
jpayne@69
|
80 : Py_None)
|
jpayne@69
|
81
|
jpayne@69
|
82
|
jpayne@69
|
83 #ifdef __cplusplus
|
jpayne@69
|
84 }
|
jpayne@69
|
85 #endif
|
jpayne@69
|
86 #endif /* !Py_WEAKREFOBJECT_H */
|