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