Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 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 | |
| 10 | typedef struct _PyWeakReference PyWeakReference; |
| 11 | |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 12 | /* PyWeakReference is the base struct for the Python ReferenceType, ProxyType, |
| 13 | * and CallableProxyType. |
| 14 | */ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 15 | struct _PyWeakReference { |
| 16 | PyObject_HEAD |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 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 | */ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 22 | PyObject *wr_object; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 23 | |
| 24 | /* A callable to invoke when wr_object dies, or NULL if none. */ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 25 | PyObject *wr_callback; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 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 | */ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 30 | long hash; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 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 | */ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 37 | PyWeakReference *wr_prev; |
| 38 | PyWeakReference *wr_next; |
| 39 | }; |
| 40 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 41 | PyAPI_DATA(PyTypeObject) _PyWeakref_RefType; |
| 42 | PyAPI_DATA(PyTypeObject) _PyWeakref_ProxyType; |
| 43 | PyAPI_DATA(PyTypeObject) _PyWeakref_CallableProxyType; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 44 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 45 | #define PyWeakref_CheckRef(op) PyObject_TypeCheck(op, &_PyWeakref_RefType) |
| 46 | #define PyWeakref_CheckRefExact(op) \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 47 | (Py_TYPE(op) == &_PyWeakref_RefType) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 48 | #define PyWeakref_CheckProxy(op) \ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 49 | ((Py_TYPE(op) == &_PyWeakref_ProxyType) || \ |
| 50 | (Py_TYPE(op) == &_PyWeakref_CallableProxyType)) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 51 | |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 52 | #define PyWeakref_Check(op) \ |
| 53 | (PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op)) |
| 54 | |
| 55 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 56 | PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob, |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 57 | PyObject *callback); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 58 | PyAPI_FUNC(PyObject *) PyWeakref_NewProxy(PyObject *ob, |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 59 | PyObject *callback); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 60 | PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 61 | |
Neal Norwitz | c5e060d | 2006-08-02 06:14:22 +0000 | [diff] [blame] | 62 | PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 63 | |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 64 | PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self); |
| 65 | |
Antoine Pitrou | d38c990 | 2012-12-08 21:15:26 +0100 | [diff] [blame] | 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) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 77 | |
| 78 | |
| 79 | #ifdef __cplusplus |
| 80 | } |
| 81 | #endif |
| 82 | #endif /* !Py_WEAKREFOBJECT_H */ |