weakrefobject.h 2.8 KB

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