--- /dev/null
+/* Weak references objects for Python. */\r
+\r
+#ifndef Py_WEAKREFOBJECT_H\r
+#define Py_WEAKREFOBJECT_H\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+\r
+typedef struct _PyWeakReference PyWeakReference;\r
+\r
+/* PyWeakReference is the base struct for the Python ReferenceType, ProxyType,\r
+ * and CallableProxyType.\r
+ */\r
+struct _PyWeakReference {\r
+ PyObject_HEAD\r
+\r
+ /* The object to which this is a weak reference, or Py_None if none.\r
+ * Note that this is a stealth reference: wr_object's refcount is\r
+ * not incremented to reflect this pointer.\r
+ */\r
+ PyObject *wr_object;\r
+\r
+ /* A callable to invoke when wr_object dies, or NULL if none. */\r
+ PyObject *wr_callback;\r
+\r
+ /* A cache for wr_object's hash code. As usual for hashes, this is -1\r
+ * if the hash code isn't known yet.\r
+ */\r
+ long hash;\r
+\r
+ /* If wr_object is weakly referenced, wr_object has a doubly-linked NULL-\r
+ * terminated list of weak references to it. These are the list pointers.\r
+ * If wr_object goes away, wr_object is set to Py_None, and these pointers\r
+ * have no meaning then.\r
+ */\r
+ PyWeakReference *wr_prev;\r
+ PyWeakReference *wr_next;\r
+};\r
+\r
+PyAPI_DATA(PyTypeObject) _PyWeakref_RefType;\r
+PyAPI_DATA(PyTypeObject) _PyWeakref_ProxyType;\r
+PyAPI_DATA(PyTypeObject) _PyWeakref_CallableProxyType;\r
+\r
+#define PyWeakref_CheckRef(op) PyObject_TypeCheck(op, &_PyWeakref_RefType)\r
+#define PyWeakref_CheckRefExact(op) \\r
+ (Py_TYPE(op) == &_PyWeakref_RefType)\r
+#define PyWeakref_CheckProxy(op) \\r
+ ((Py_TYPE(op) == &_PyWeakref_ProxyType) || \\r
+ (Py_TYPE(op) == &_PyWeakref_CallableProxyType))\r
+\r
+#define PyWeakref_Check(op) \\r
+ (PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op))\r
+\r
+\r
+PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob,\r
+ PyObject *callback);\r
+PyAPI_FUNC(PyObject *) PyWeakref_NewProxy(PyObject *ob,\r
+ PyObject *callback);\r
+PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref);\r
+\r
+PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);\r
+\r
+PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);\r
+\r
+/* Explanation for the Py_REFCNT() check: when a weakref's target is part\r
+ of a long chain of deallocations which triggers the trashcan mechanism,\r
+ clearing the weakrefs can be delayed long after the target's refcount\r
+ has dropped to zero. In the meantime, code accessing the weakref will\r
+ be able to "see" the target object even though it is supposed to be\r
+ unreachable. See issue #16602. */\r
+\r
+#define PyWeakref_GET_OBJECT(ref) \\r
+ (Py_REFCNT(((PyWeakReference *)(ref))->wr_object) > 0 \\r
+ ? ((PyWeakReference *)(ref))->wr_object \\r
+ : Py_None)\r
+\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+#endif /* !Py_WEAKREFOBJECT_H */\r