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 | */ |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 15 | #ifndef Py_LIMITED_API |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 16 | struct _PyWeakReference { |
| 17 | PyObject_HEAD |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 18 | |
| 19 | /* The object to which this is a weak reference, or Py_None if none. |
| 20 | * Note that this is a stealth reference: wr_object's refcount is |
| 21 | * not incremented to reflect this pointer. |
| 22 | */ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 23 | PyObject *wr_object; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 24 | |
| 25 | /* A callable to invoke when wr_object dies, or NULL if none. */ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 26 | PyObject *wr_callback; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 27 | |
| 28 | /* A cache for wr_object's hash code. As usual for hashes, this is -1 |
| 29 | * if the hash code isn't known yet. |
| 30 | */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 31 | Py_hash_t hash; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 32 | |
| 33 | /* If wr_object is weakly referenced, wr_object has a doubly-linked NULL- |
| 34 | * terminated list of weak references to it. These are the list pointers. |
| 35 | * If wr_object goes away, wr_object is set to Py_None, and these pointers |
| 36 | * have no meaning then. |
| 37 | */ |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 38 | PyWeakReference *wr_prev; |
| 39 | PyWeakReference *wr_next; |
| 40 | }; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 41 | #endif |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 42 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 43 | PyAPI_DATA(PyTypeObject) _PyWeakref_RefType; |
| 44 | PyAPI_DATA(PyTypeObject) _PyWeakref_ProxyType; |
| 45 | PyAPI_DATA(PyTypeObject) _PyWeakref_CallableProxyType; |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 46 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 47 | #define PyWeakref_CheckRef(op) PyObject_TypeCheck(op, &_PyWeakref_RefType) |
| 48 | #define PyWeakref_CheckRefExact(op) \ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 49 | (Py_TYPE(op) == &_PyWeakref_RefType) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 50 | #define PyWeakref_CheckProxy(op) \ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 51 | ((Py_TYPE(op) == &_PyWeakref_ProxyType) || \ |
| 52 | (Py_TYPE(op) == &_PyWeakref_CallableProxyType)) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 53 | |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 54 | #define PyWeakref_Check(op) \ |
| 55 | (PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op)) |
| 56 | |
| 57 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 58 | PyAPI_FUNC(PyObject *) PyWeakref_NewRef(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_NewProxy(PyObject *ob, |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 61 | PyObject *callback); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 62 | PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 63 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 64 | #ifndef Py_LIMITED_API |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 65 | PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head); |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 66 | |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 67 | PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 68 | #endif |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | 62a0d6e | 2012-12-08 21:15:26 +0100 | [diff] [blame] | 70 | /* Explanation for the Py_REFCNT() check: when a weakref's target is part |
| 71 | of a long chain of deallocations which triggers the trashcan mechanism, |
| 72 | clearing the weakrefs can be delayed long after the target's refcount |
| 73 | has dropped to zero. In the meantime, code accessing the weakref will |
| 74 | be able to "see" the target object even though it is supposed to be |
| 75 | unreachable. See issue #16602. */ |
| 76 | |
| 77 | #define PyWeakref_GET_OBJECT(ref) \ |
| 78 | (Py_REFCNT(((PyWeakReference *)(ref))->wr_object) > 0 \ |
| 79 | ? ((PyWeakReference *)(ref))->wr_object \ |
| 80 | : Py_None) |
Fred Drake | 8844d52 | 2001-10-05 21:52:26 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | #ifdef __cplusplus |
| 84 | } |
| 85 | #endif |
| 86 | #endif /* !Py_WEAKREFOBJECT_H */ |