python-2.7.6/include/weakrefobject.h Source File

weakrefobject.h
Go to the documentation of this file.
1 /* Weak references objects for Python. */
2 
3 #ifndef Py_WEAKREFOBJECT_H
4 #define Py_WEAKREFOBJECT_H
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8 
9 
11 
12 /* PyWeakReference is the base struct for the Python ReferenceType, ProxyType,
13  * and CallableProxyType.
14  */
17 
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.
21  */
23 
24  /* A callable to invoke when wr_object dies, or NULL if none. */
26 
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.
29  */
30  long hash;
31 
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.
36  */
39 };
40 
41 PyAPI_DATA(PyTypeObject) _PyWeakref_RefType;
42 PyAPI_DATA(PyTypeObject) _PyWeakref_ProxyType;
43 PyAPI_DATA(PyTypeObject) _PyWeakref_CallableProxyType;
44 
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))
51 
52 #define PyWeakref_Check(op) \
53  (PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op))
54 
55 
56 PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob,
58 PyAPI_FUNC(PyObject *) PyWeakref_NewProxy(PyObject *ob,
59  PyObject *callback);
60 PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref);
61 
62 PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);
63 
64 PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
65 
66 /* Explanation for the Py_REFCNT() check: when a weakref's target is part
67  of a long chain of deallocations which triggers the trashcan mechanism,
68  clearing the weakrefs can be delayed long after the target's refcount
69  has dropped to zero. In the meantime, code accessing the weakref will
70  be able to "see" the target object even though it is supposed to be
71  unreachable. See issue #16602. */
72 
73 #define PyWeakref_GET_OBJECT(ref) \
74  (Py_REFCNT(((PyWeakReference *)(ref))->wr_object) > 0 \
75  ? ((PyWeakReference *)(ref))->wr_object \
76  : Py_None)
77 
78 
79 #ifdef __cplusplus
80 }
81 #endif
82 #endif /* !Py_WEAKREFOBJECT_H */
PyWeakReference * wr_prev
Definition: weakrefobject.h:37
PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob
Py_intptr_t Py_ssize_t
Definition: pyport.h:185
PyAPI_DATA(PyTypeObject) _PyWeakref_RefType
PyWeakReference * wr_next
Definition: weakrefobject.h:38
PyObject * callback
Definition: weakrefobject.h:57
PyObject * ob
Definition: abstract.h:1195
PyObject_HEAD PyObject * wr_object
Definition: weakrefobject.h:22
PyMethodDef const char PyObject * self
Definition: modsupport.h:117
PyObject * wr_callback
Definition: weakrefobject.h:25
#define PyObject_HEAD
Definition: object.h:78