+++ /dev/null
-#include "Python.h"\r
-#include "structmember.h"\r
-\r
-\r
-#define GET_WEAKREFS_LISTPTR(o) \\r
- ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))\r
-\r
-\r
-Py_ssize_t\r
-_PyWeakref_GetWeakrefCount(PyWeakReference *head)\r
-{\r
- Py_ssize_t count = 0;\r
-\r
- while (head != NULL) {\r
- ++count;\r
- head = head->wr_next;\r
- }\r
- return count;\r
-}\r
-\r
-\r
-static void\r
-init_weakref(PyWeakReference *self, PyObject *ob, PyObject *callback)\r
-{\r
- self->hash = -1;\r
- self->wr_object = ob;\r
- Py_XINCREF(callback);\r
- self->wr_callback = callback;\r
-}\r
-\r
-static PyWeakReference *\r
-new_weakref(PyObject *ob, PyObject *callback)\r
-{\r
- PyWeakReference *result;\r
-\r
- result = PyObject_GC_New(PyWeakReference, &_PyWeakref_RefType);\r
- if (result) {\r
- init_weakref(result, ob, callback);\r
- PyObject_GC_Track(result);\r
- }\r
- return result;\r
-}\r
-\r
-\r
-/* This function clears the passed-in reference and removes it from the\r
- * list of weak references for the referent. This is the only code that\r
- * removes an item from the doubly-linked list of weak references for an\r
- * object; it is also responsible for clearing the callback slot.\r
- */\r
-static void\r
-clear_weakref(PyWeakReference *self)\r
-{\r
- PyObject *callback = self->wr_callback;\r
-\r
- if (PyWeakref_GET_OBJECT(self) != Py_None) {\r
- PyWeakReference **list = GET_WEAKREFS_LISTPTR(\r
- PyWeakref_GET_OBJECT(self));\r
-\r
- if (*list == self)\r
- /* If 'self' is the end of the list (and thus self->wr_next == NULL)\r
- then the weakref list itself (and thus the value of *list) will\r
- end up being set to NULL. */\r
- *list = self->wr_next;\r
- self->wr_object = Py_None;\r
- if (self->wr_prev != NULL)\r
- self->wr_prev->wr_next = self->wr_next;\r
- if (self->wr_next != NULL)\r
- self->wr_next->wr_prev = self->wr_prev;\r
- self->wr_prev = NULL;\r
- self->wr_next = NULL;\r
- }\r
- if (callback != NULL) {\r
- Py_DECREF(callback);\r
- self->wr_callback = NULL;\r
- }\r
-}\r
-\r
-/* Cyclic gc uses this to *just* clear the passed-in reference, leaving\r
- * the callback intact and uncalled. It must be possible to call self's\r
- * tp_dealloc() after calling this, so self has to be left in a sane enough\r
- * state for that to work. We expect tp_dealloc to decref the callback\r
- * then. The reason for not letting clear_weakref() decref the callback\r
- * right now is that if the callback goes away, that may in turn trigger\r
- * another callback (if a weak reference to the callback exists) -- running\r
- * arbitrary Python code in the middle of gc is a disaster. The convolution\r
- * here allows gc to delay triggering such callbacks until the world is in\r
- * a sane state again.\r
- */\r
-void\r
-_PyWeakref_ClearRef(PyWeakReference *self)\r
-{\r
- PyObject *callback;\r
-\r
- assert(self != NULL);\r
- assert(PyWeakref_Check(self));\r
- /* Preserve and restore the callback around clear_weakref. */\r
- callback = self->wr_callback;\r
- self->wr_callback = NULL;\r
- clear_weakref(self);\r
- self->wr_callback = callback;\r
-}\r
-\r
-static void\r
-weakref_dealloc(PyObject *self)\r
-{\r
- PyObject_GC_UnTrack(self);\r
- clear_weakref((PyWeakReference *) self);\r
- Py_TYPE(self)->tp_free(self);\r
-}\r
-\r
-\r
-static int\r
-gc_traverse(PyWeakReference *self, visitproc visit, void *arg)\r
-{\r
- Py_VISIT(self->wr_callback);\r
- return 0;\r
-}\r
-\r
-\r
-static int\r
-gc_clear(PyWeakReference *self)\r
-{\r
- clear_weakref(self);\r
- return 0;\r
-}\r
-\r
-\r
-static PyObject *\r
-weakref_call(PyWeakReference *self, PyObject *args, PyObject *kw)\r
-{\r
- static char *kwlist[] = {NULL};\r
-\r
- if (PyArg_ParseTupleAndKeywords(args, kw, ":__call__", kwlist)) {\r
- PyObject *object = PyWeakref_GET_OBJECT(self);\r
- Py_INCREF(object);\r
- return (object);\r
- }\r
- return NULL;\r
-}\r
-\r
-\r
-static long\r
-weakref_hash(PyWeakReference *self)\r
-{\r
- if (self->hash != -1)\r
- return self->hash;\r
- if (PyWeakref_GET_OBJECT(self) == Py_None) {\r
- PyErr_SetString(PyExc_TypeError, "weak object has gone away");\r
- return -1;\r
- }\r
- self->hash = PyObject_Hash(PyWeakref_GET_OBJECT(self));\r
- return self->hash;\r
-}\r
-\r
-\r
-static PyObject *\r
-weakref_repr(PyWeakReference *self)\r
-{\r
- char buffer[256];\r
- if (PyWeakref_GET_OBJECT(self) == Py_None) {\r
- PyOS_snprintf(buffer, sizeof(buffer), "<weakref at %p; dead>", self);\r
- }\r
- else {\r
- char *name = NULL;\r
- PyObject *nameobj = PyObject_GetAttrString(PyWeakref_GET_OBJECT(self),\r
- "__name__");\r
- if (nameobj == NULL)\r
- PyErr_Clear();\r
- else if (PyString_Check(nameobj))\r
- name = PyString_AS_STRING(nameobj);\r
- PyOS_snprintf(buffer, sizeof(buffer),\r
- name ? "<weakref at %p; to '%.50s' at %p (%s)>"\r
- : "<weakref at %p; to '%.50s' at %p>",\r
- self,\r
- Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name,\r
- PyWeakref_GET_OBJECT(self),\r
- name);\r
- Py_XDECREF(nameobj);\r
- }\r
- return PyString_FromString(buffer);\r
-}\r
-\r
-/* Weak references only support equality, not ordering. Two weak references\r
- are equal if the underlying objects are equal. If the underlying object has\r
- gone away, they are equal if they are identical. */\r
-\r
-static PyObject *\r
-weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)\r
-{\r
- if (op != Py_EQ || self->ob_type != other->ob_type) {\r
- Py_INCREF(Py_NotImplemented);\r
- return Py_NotImplemented;\r
- }\r
- if (PyWeakref_GET_OBJECT(self) == Py_None\r
- || PyWeakref_GET_OBJECT(other) == Py_None) {\r
- PyObject *res = self==other ? Py_True : Py_False;\r
- Py_INCREF(res);\r
- return res;\r
- }\r
- return PyObject_RichCompare(PyWeakref_GET_OBJECT(self),\r
- PyWeakref_GET_OBJECT(other), op);\r
-}\r
-\r
-/* Given the head of an object's list of weak references, extract the\r
- * two callback-less refs (ref and proxy). Used to determine if the\r
- * shared references exist and to determine the back link for newly\r
- * inserted references.\r
- */\r
-static void\r
-get_basic_refs(PyWeakReference *head,\r
- PyWeakReference **refp, PyWeakReference **proxyp)\r
-{\r
- *refp = NULL;\r
- *proxyp = NULL;\r
-\r
- if (head != NULL && head->wr_callback == NULL) {\r
- /* We need to be careful that the "basic refs" aren't\r
- subclasses of the main types. That complicates this a\r
- little. */\r
- if (PyWeakref_CheckRefExact(head)) {\r
- *refp = head;\r
- head = head->wr_next;\r
- }\r
- if (head != NULL\r
- && head->wr_callback == NULL\r
- && PyWeakref_CheckProxy(head)) {\r
- *proxyp = head;\r
- /* head = head->wr_next; */\r
- }\r
- }\r
-}\r
-\r
-/* Insert 'newref' in the list after 'prev'. Both must be non-NULL. */\r
-static void\r
-insert_after(PyWeakReference *newref, PyWeakReference *prev)\r
-{\r
- newref->wr_prev = prev;\r
- newref->wr_next = prev->wr_next;\r
- if (prev->wr_next != NULL)\r
- prev->wr_next->wr_prev = newref;\r
- prev->wr_next = newref;\r
-}\r
-\r
-/* Insert 'newref' at the head of the list; 'list' points to the variable\r
- * that stores the head.\r
- */\r
-static void\r
-insert_head(PyWeakReference *newref, PyWeakReference **list)\r
-{\r
- PyWeakReference *next = *list;\r
-\r
- newref->wr_prev = NULL;\r
- newref->wr_next = next;\r
- if (next != NULL)\r
- next->wr_prev = newref;\r
- *list = newref;\r
-}\r
-\r
-static int\r
-parse_weakref_init_args(char *funcname, PyObject *args, PyObject *kwargs,\r
- PyObject **obp, PyObject **callbackp)\r
-{\r
- /* XXX Should check that kwargs == NULL or is empty. */\r
- return PyArg_UnpackTuple(args, funcname, 1, 2, obp, callbackp);\r
-}\r
-\r
-static PyObject *\r
-weakref___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)\r
-{\r
- PyWeakReference *self = NULL;\r
- PyObject *ob, *callback = NULL;\r
-\r
- if (parse_weakref_init_args("__new__", args, kwargs, &ob, &callback)) {\r
- PyWeakReference *ref, *proxy;\r
- PyWeakReference **list;\r
-\r
- if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {\r
- PyErr_Format(PyExc_TypeError,\r
- "cannot create weak reference to '%s' object",\r
- Py_TYPE(ob)->tp_name);\r
- return NULL;\r
- }\r
- if (callback == Py_None)\r
- callback = NULL;\r
- list = GET_WEAKREFS_LISTPTR(ob);\r
- get_basic_refs(*list, &ref, &proxy);\r
- if (callback == NULL && type == &_PyWeakref_RefType) {\r
- if (ref != NULL) {\r
- /* We can re-use an existing reference. */\r
- Py_INCREF(ref);\r
- return (PyObject *)ref;\r
- }\r
- }\r
- /* We have to create a new reference. */\r
- /* Note: the tp_alloc() can trigger cyclic GC, so the weakref\r
- list on ob can be mutated. This means that the ref and\r
- proxy pointers we got back earlier may have been collected,\r
- so we need to compute these values again before we use\r
- them. */\r
- self = (PyWeakReference *) (type->tp_alloc(type, 0));\r
- if (self != NULL) {\r
- init_weakref(self, ob, callback);\r
- if (callback == NULL && type == &_PyWeakref_RefType) {\r
- insert_head(self, list);\r
- }\r
- else {\r
- PyWeakReference *prev;\r
-\r
- get_basic_refs(*list, &ref, &proxy);\r
- prev = (proxy == NULL) ? ref : proxy;\r
- if (prev == NULL)\r
- insert_head(self, list);\r
- else\r
- insert_after(self, prev);\r
- }\r
- }\r
- }\r
- return (PyObject *)self;\r
-}\r
-\r
-static int\r
-weakref___init__(PyObject *self, PyObject *args, PyObject *kwargs)\r
-{\r
- PyObject *tmp;\r
-\r
- if (parse_weakref_init_args("__init__", args, kwargs, &tmp, &tmp))\r
- return 0;\r
- else\r
- return -1;\r
-}\r
-\r
-\r
-PyTypeObject\r
-_PyWeakref_RefType = {\r
- PyVarObject_HEAD_INIT(&PyType_Type, 0)\r
- "weakref",\r
- sizeof(PyWeakReference),\r
- 0,\r
- weakref_dealloc, /*tp_dealloc*/\r
- 0, /*tp_print*/\r
- 0, /*tp_getattr*/\r
- 0, /*tp_setattr*/\r
- 0, /*tp_compare*/\r
- (reprfunc)weakref_repr, /*tp_repr*/\r
- 0, /*tp_as_number*/\r
- 0, /*tp_as_sequence*/\r
- 0, /*tp_as_mapping*/\r
- (hashfunc)weakref_hash, /*tp_hash*/\r
- (ternaryfunc)weakref_call, /*tp_call*/\r
- 0, /*tp_str*/\r
- 0, /*tp_getattro*/\r
- 0, /*tp_setattro*/\r
- 0, /*tp_as_buffer*/\r
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_RICHCOMPARE\r
- | Py_TPFLAGS_BASETYPE, /*tp_flags*/\r
- 0, /*tp_doc*/\r
- (traverseproc)gc_traverse, /*tp_traverse*/\r
- (inquiry)gc_clear, /*tp_clear*/\r
- (richcmpfunc)weakref_richcompare, /*tp_richcompare*/\r
- 0, /*tp_weaklistoffset*/\r
- 0, /*tp_iter*/\r
- 0, /*tp_iternext*/\r
- 0, /*tp_methods*/\r
- 0, /*tp_members*/\r
- 0, /*tp_getset*/\r
- 0, /*tp_base*/\r
- 0, /*tp_dict*/\r
- 0, /*tp_descr_get*/\r
- 0, /*tp_descr_set*/\r
- 0, /*tp_dictoffset*/\r
- weakref___init__, /*tp_init*/\r
- PyType_GenericAlloc, /*tp_alloc*/\r
- weakref___new__, /*tp_new*/\r
- PyObject_GC_Del, /*tp_free*/\r
-};\r
-\r
-\r
-static int\r
-proxy_checkref(PyWeakReference *proxy)\r
-{\r
- if (PyWeakref_GET_OBJECT(proxy) == Py_None) {\r
- PyErr_SetString(PyExc_ReferenceError,\r
- "weakly-referenced object no longer exists");\r
- return 0;\r
- }\r
- return 1;\r
-}\r
-\r
-\r
-/* If a parameter is a proxy, check that it is still "live" and wrap it,\r
- * replacing the original value with the raw object. Raises ReferenceError\r
- * if the param is a dead proxy.\r
- */\r
-#define UNWRAP(o) \\r
- if (PyWeakref_CheckProxy(o)) { \\r
- if (!proxy_checkref((PyWeakReference *)o)) \\r
- return NULL; \\r
- o = PyWeakref_GET_OBJECT(o); \\r
- }\r
-\r
-#define UNWRAP_I(o) \\r
- if (PyWeakref_CheckProxy(o)) { \\r
- if (!proxy_checkref((PyWeakReference *)o)) \\r
- return -1; \\r
- o = PyWeakref_GET_OBJECT(o); \\r
- }\r
-\r
-#define WRAP_UNARY(method, generic) \\r
- static PyObject * \\r
- method(PyObject *proxy) { \\r
- UNWRAP(proxy); \\r
- return generic(proxy); \\r
- }\r
-\r
-#define WRAP_BINARY(method, generic) \\r
- static PyObject * \\r
- method(PyObject *x, PyObject *y) { \\r
- UNWRAP(x); \\r
- UNWRAP(y); \\r
- return generic(x, y); \\r
- }\r
-\r
-/* Note that the third arg needs to be checked for NULL since the tp_call\r
- * slot can receive NULL for this arg.\r
- */\r
-#define WRAP_TERNARY(method, generic) \\r
- static PyObject * \\r
- method(PyObject *proxy, PyObject *v, PyObject *w) { \\r
- UNWRAP(proxy); \\r
- UNWRAP(v); \\r
- if (w != NULL) \\r
- UNWRAP(w); \\r
- return generic(proxy, v, w); \\r
- }\r
-\r
-#define WRAP_METHOD(method, special) \\r
- static PyObject * \\r
- method(PyObject *proxy) { \\r
- UNWRAP(proxy); \\r
- return PyObject_CallMethod(proxy, special, ""); \\r
- }\r
-\r
-\r
-/* direct slots */\r
-\r
-WRAP_BINARY(proxy_getattr, PyObject_GetAttr)\r
-WRAP_UNARY(proxy_str, PyObject_Str)\r
-WRAP_TERNARY(proxy_call, PyEval_CallObjectWithKeywords)\r
-\r
-static PyObject *\r
-proxy_repr(PyWeakReference *proxy)\r
-{\r
- char buf[160];\r
- PyOS_snprintf(buf, sizeof(buf),\r
- "<weakproxy at %p to %.100s at %p>", proxy,\r
- Py_TYPE(PyWeakref_GET_OBJECT(proxy))->tp_name,\r
- PyWeakref_GET_OBJECT(proxy));\r
- return PyString_FromString(buf);\r
-}\r
-\r
-\r
-static int\r
-proxy_setattr(PyWeakReference *proxy, PyObject *name, PyObject *value)\r
-{\r
- if (!proxy_checkref(proxy))\r
- return -1;\r
- return PyObject_SetAttr(PyWeakref_GET_OBJECT(proxy), name, value);\r
-}\r
-\r
-static int\r
-proxy_compare(PyObject *proxy, PyObject *v)\r
-{\r
- UNWRAP_I(proxy);\r
- UNWRAP_I(v);\r
- return PyObject_Compare(proxy, v);\r
-}\r
-\r
-/* number slots */\r
-WRAP_BINARY(proxy_add, PyNumber_Add)\r
-WRAP_BINARY(proxy_sub, PyNumber_Subtract)\r
-WRAP_BINARY(proxy_mul, PyNumber_Multiply)\r
-WRAP_BINARY(proxy_div, PyNumber_Divide)\r
-WRAP_BINARY(proxy_floor_div, PyNumber_FloorDivide)\r
-WRAP_BINARY(proxy_true_div, PyNumber_TrueDivide)\r
-WRAP_BINARY(proxy_mod, PyNumber_Remainder)\r
-WRAP_BINARY(proxy_divmod, PyNumber_Divmod)\r
-WRAP_TERNARY(proxy_pow, PyNumber_Power)\r
-WRAP_UNARY(proxy_neg, PyNumber_Negative)\r
-WRAP_UNARY(proxy_pos, PyNumber_Positive)\r
-WRAP_UNARY(proxy_abs, PyNumber_Absolute)\r
-WRAP_UNARY(proxy_invert, PyNumber_Invert)\r
-WRAP_BINARY(proxy_lshift, PyNumber_Lshift)\r
-WRAP_BINARY(proxy_rshift, PyNumber_Rshift)\r
-WRAP_BINARY(proxy_and, PyNumber_And)\r
-WRAP_BINARY(proxy_xor, PyNumber_Xor)\r
-WRAP_BINARY(proxy_or, PyNumber_Or)\r
-WRAP_UNARY(proxy_int, PyNumber_Int)\r
-WRAP_UNARY(proxy_long, PyNumber_Long)\r
-WRAP_UNARY(proxy_float, PyNumber_Float)\r
-WRAP_BINARY(proxy_iadd, PyNumber_InPlaceAdd)\r
-WRAP_BINARY(proxy_isub, PyNumber_InPlaceSubtract)\r
-WRAP_BINARY(proxy_imul, PyNumber_InPlaceMultiply)\r
-WRAP_BINARY(proxy_idiv, PyNumber_InPlaceDivide)\r
-WRAP_BINARY(proxy_ifloor_div, PyNumber_InPlaceFloorDivide)\r
-WRAP_BINARY(proxy_itrue_div, PyNumber_InPlaceTrueDivide)\r
-WRAP_BINARY(proxy_imod, PyNumber_InPlaceRemainder)\r
-WRAP_TERNARY(proxy_ipow, PyNumber_InPlacePower)\r
-WRAP_BINARY(proxy_ilshift, PyNumber_InPlaceLshift)\r
-WRAP_BINARY(proxy_irshift, PyNumber_InPlaceRshift)\r
-WRAP_BINARY(proxy_iand, PyNumber_InPlaceAnd)\r
-WRAP_BINARY(proxy_ixor, PyNumber_InPlaceXor)\r
-WRAP_BINARY(proxy_ior, PyNumber_InPlaceOr)\r
-WRAP_UNARY(proxy_index, PyNumber_Index)\r
-\r
-static int\r
-proxy_nonzero(PyWeakReference *proxy)\r
-{\r
- PyObject *o = PyWeakref_GET_OBJECT(proxy);\r
- if (!proxy_checkref(proxy))\r
- return -1;\r
- return PyObject_IsTrue(o);\r
-}\r
-\r
-static void\r
-proxy_dealloc(PyWeakReference *self)\r
-{\r
- if (self->wr_callback != NULL)\r
- PyObject_GC_UnTrack((PyObject *)self);\r
- clear_weakref(self);\r
- PyObject_GC_Del(self);\r
-}\r
-\r
-/* sequence slots */\r
-\r
-static PyObject *\r
-proxy_slice(PyWeakReference *proxy, Py_ssize_t i, Py_ssize_t j)\r
-{\r
- if (!proxy_checkref(proxy))\r
- return NULL;\r
- return PySequence_GetSlice(PyWeakref_GET_OBJECT(proxy), i, j);\r
-}\r
-\r
-static int\r
-proxy_ass_slice(PyWeakReference *proxy, Py_ssize_t i, Py_ssize_t j, PyObject *value)\r
-{\r
- if (!proxy_checkref(proxy))\r
- return -1;\r
- return PySequence_SetSlice(PyWeakref_GET_OBJECT(proxy), i, j, value);\r
-}\r
-\r
-static int\r
-proxy_contains(PyWeakReference *proxy, PyObject *value)\r
-{\r
- if (!proxy_checkref(proxy))\r
- return -1;\r
- return PySequence_Contains(PyWeakref_GET_OBJECT(proxy), value);\r
-}\r
-\r
-\r
-/* mapping slots */\r
-\r
-static Py_ssize_t\r
-proxy_length(PyWeakReference *proxy)\r
-{\r
- if (!proxy_checkref(proxy))\r
- return -1;\r
- return PyObject_Length(PyWeakref_GET_OBJECT(proxy));\r
-}\r
-\r
-WRAP_BINARY(proxy_getitem, PyObject_GetItem)\r
-\r
-static int\r
-proxy_setitem(PyWeakReference *proxy, PyObject *key, PyObject *value)\r
-{\r
- if (!proxy_checkref(proxy))\r
- return -1;\r
-\r
- if (value == NULL)\r
- return PyObject_DelItem(PyWeakref_GET_OBJECT(proxy), key);\r
- else\r
- return PyObject_SetItem(PyWeakref_GET_OBJECT(proxy), key, value);\r
-}\r
-\r
-/* iterator slots */\r
-\r
-static PyObject *\r
-proxy_iter(PyWeakReference *proxy)\r
-{\r
- if (!proxy_checkref(proxy))\r
- return NULL;\r
- return PyObject_GetIter(PyWeakref_GET_OBJECT(proxy));\r
-}\r
-\r
-static PyObject *\r
-proxy_iternext(PyWeakReference *proxy)\r
-{\r
- if (!proxy_checkref(proxy))\r
- return NULL;\r
- return PyIter_Next(PyWeakref_GET_OBJECT(proxy));\r
-}\r
-\r
-\r
-WRAP_METHOD(proxy_unicode, "__unicode__");\r
-\r
-\r
-static PyMethodDef proxy_methods[] = {\r
- {"__unicode__", (PyCFunction)proxy_unicode, METH_NOARGS},\r
- {NULL, NULL}\r
-};\r
-\r
-\r
-static PyNumberMethods proxy_as_number = {\r
- proxy_add, /*nb_add*/\r
- proxy_sub, /*nb_subtract*/\r
- proxy_mul, /*nb_multiply*/\r
- proxy_div, /*nb_divide*/\r
- proxy_mod, /*nb_remainder*/\r
- proxy_divmod, /*nb_divmod*/\r
- proxy_pow, /*nb_power*/\r
- proxy_neg, /*nb_negative*/\r
- proxy_pos, /*nb_positive*/\r
- proxy_abs, /*nb_absolute*/\r
- (inquiry)proxy_nonzero, /*nb_nonzero*/\r
- proxy_invert, /*nb_invert*/\r
- proxy_lshift, /*nb_lshift*/\r
- proxy_rshift, /*nb_rshift*/\r
- proxy_and, /*nb_and*/\r
- proxy_xor, /*nb_xor*/\r
- proxy_or, /*nb_or*/\r
- 0, /*nb_coerce*/\r
- proxy_int, /*nb_int*/\r
- proxy_long, /*nb_long*/\r
- proxy_float, /*nb_float*/\r
- 0, /*nb_oct*/\r
- 0, /*nb_hex*/\r
- proxy_iadd, /*nb_inplace_add*/\r
- proxy_isub, /*nb_inplace_subtract*/\r
- proxy_imul, /*nb_inplace_multiply*/\r
- proxy_idiv, /*nb_inplace_divide*/\r
- proxy_imod, /*nb_inplace_remainder*/\r
- proxy_ipow, /*nb_inplace_power*/\r
- proxy_ilshift, /*nb_inplace_lshift*/\r
- proxy_irshift, /*nb_inplace_rshift*/\r
- proxy_iand, /*nb_inplace_and*/\r
- proxy_ixor, /*nb_inplace_xor*/\r
- proxy_ior, /*nb_inplace_or*/\r
- proxy_floor_div, /*nb_floor_divide*/\r
- proxy_true_div, /*nb_true_divide*/\r
- proxy_ifloor_div, /*nb_inplace_floor_divide*/\r
- proxy_itrue_div, /*nb_inplace_true_divide*/\r
- proxy_index, /*nb_index*/\r
-};\r
-\r
-static PySequenceMethods proxy_as_sequence = {\r
- (lenfunc)proxy_length, /*sq_length*/\r
- 0, /*sq_concat*/\r
- 0, /*sq_repeat*/\r
- 0, /*sq_item*/\r
- (ssizessizeargfunc)proxy_slice, /*sq_slice*/\r
- 0, /*sq_ass_item*/\r
- (ssizessizeobjargproc)proxy_ass_slice, /*sq_ass_slice*/\r
- (objobjproc)proxy_contains, /* sq_contains */\r
-};\r
-\r
-static PyMappingMethods proxy_as_mapping = {\r
- (lenfunc)proxy_length, /*mp_length*/\r
- proxy_getitem, /*mp_subscript*/\r
- (objobjargproc)proxy_setitem, /*mp_ass_subscript*/\r
-};\r
-\r
-\r
-PyTypeObject\r
-_PyWeakref_ProxyType = {\r
- PyVarObject_HEAD_INIT(&PyType_Type, 0)\r
- "weakproxy",\r
- sizeof(PyWeakReference),\r
- 0,\r
- /* methods */\r
- (destructor)proxy_dealloc, /* tp_dealloc */\r
- 0, /* tp_print */\r
- 0, /* tp_getattr */\r
- 0, /* tp_setattr */\r
- proxy_compare, /* tp_compare */\r
- (reprfunc)proxy_repr, /* tp_repr */\r
- &proxy_as_number, /* tp_as_number */\r
- &proxy_as_sequence, /* tp_as_sequence */\r
- &proxy_as_mapping, /* tp_as_mapping */\r
- 0, /* tp_hash */\r
- 0, /* tp_call */\r
- proxy_str, /* tp_str */\r
- proxy_getattr, /* tp_getattro */\r
- (setattrofunc)proxy_setattr, /* tp_setattro */\r
- 0, /* tp_as_buffer */\r
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC\r
- | Py_TPFLAGS_CHECKTYPES, /* tp_flags */\r
- 0, /* tp_doc */\r
- (traverseproc)gc_traverse, /* tp_traverse */\r
- (inquiry)gc_clear, /* tp_clear */\r
- 0, /* tp_richcompare */\r
- 0, /* tp_weaklistoffset */\r
- (getiterfunc)proxy_iter, /* tp_iter */\r
- (iternextfunc)proxy_iternext, /* tp_iternext */\r
- proxy_methods, /* tp_methods */\r
-};\r
-\r
-\r
-PyTypeObject\r
-_PyWeakref_CallableProxyType = {\r
- PyVarObject_HEAD_INIT(&PyType_Type, 0)\r
- "weakcallableproxy",\r
- sizeof(PyWeakReference),\r
- 0,\r
- /* methods */\r
- (destructor)proxy_dealloc, /* tp_dealloc */\r
- 0, /* tp_print */\r
- 0, /* tp_getattr */\r
- 0, /* tp_setattr */\r
- proxy_compare, /* tp_compare */\r
- (unaryfunc)proxy_repr, /* tp_repr */\r
- &proxy_as_number, /* tp_as_number */\r
- &proxy_as_sequence, /* tp_as_sequence */\r
- &proxy_as_mapping, /* tp_as_mapping */\r
- 0, /* tp_hash */\r
- proxy_call, /* tp_call */\r
- proxy_str, /* tp_str */\r
- proxy_getattr, /* tp_getattro */\r
- (setattrofunc)proxy_setattr, /* tp_setattro */\r
- 0, /* tp_as_buffer */\r
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC\r
- | Py_TPFLAGS_CHECKTYPES, /* tp_flags */\r
- 0, /* tp_doc */\r
- (traverseproc)gc_traverse, /* tp_traverse */\r
- (inquiry)gc_clear, /* tp_clear */\r
- 0, /* tp_richcompare */\r
- 0, /* tp_weaklistoffset */\r
- (getiterfunc)proxy_iter, /* tp_iter */\r
- (iternextfunc)proxy_iternext, /* tp_iternext */\r
-};\r
-\r
-\r
-\r
-PyObject *\r
-PyWeakref_NewRef(PyObject *ob, PyObject *callback)\r
-{\r
- PyWeakReference *result = NULL;\r
- PyWeakReference **list;\r
- PyWeakReference *ref, *proxy;\r
-\r
- if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {\r
- PyErr_Format(PyExc_TypeError,\r
- "cannot create weak reference to '%s' object",\r
- Py_TYPE(ob)->tp_name);\r
- return NULL;\r
- }\r
- list = GET_WEAKREFS_LISTPTR(ob);\r
- get_basic_refs(*list, &ref, &proxy);\r
- if (callback == Py_None)\r
- callback = NULL;\r
- if (callback == NULL)\r
- /* return existing weak reference if it exists */\r
- result = ref;\r
- if (result != NULL)\r
- Py_INCREF(result);\r
- else {\r
- /* Note: new_weakref() can trigger cyclic GC, so the weakref\r
- list on ob can be mutated. This means that the ref and\r
- proxy pointers we got back earlier may have been collected,\r
- so we need to compute these values again before we use\r
- them. */\r
- result = new_weakref(ob, callback);\r
- if (result != NULL) {\r
- get_basic_refs(*list, &ref, &proxy);\r
- if (callback == NULL) {\r
- if (ref == NULL)\r
- insert_head(result, list);\r
- else {\r
- /* Someone else added a ref without a callback\r
- during GC. Return that one instead of this one\r
- to avoid violating the invariants of the list\r
- of weakrefs for ob. */\r
- Py_DECREF(result);\r
- Py_INCREF(ref);\r
- result = ref;\r
- }\r
- }\r
- else {\r
- PyWeakReference *prev;\r
-\r
- prev = (proxy == NULL) ? ref : proxy;\r
- if (prev == NULL)\r
- insert_head(result, list);\r
- else\r
- insert_after(result, prev);\r
- }\r
- }\r
- }\r
- return (PyObject *) result;\r
-}\r
-\r
-\r
-PyObject *\r
-PyWeakref_NewProxy(PyObject *ob, PyObject *callback)\r
-{\r
- PyWeakReference *result = NULL;\r
- PyWeakReference **list;\r
- PyWeakReference *ref, *proxy;\r
-\r
- if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {\r
- PyErr_Format(PyExc_TypeError,\r
- "cannot create weak reference to '%s' object",\r
- Py_TYPE(ob)->tp_name);\r
- return NULL;\r
- }\r
- list = GET_WEAKREFS_LISTPTR(ob);\r
- get_basic_refs(*list, &ref, &proxy);\r
- if (callback == Py_None)\r
- callback = NULL;\r
- if (callback == NULL)\r
- /* attempt to return an existing weak reference if it exists */\r
- result = proxy;\r
- if (result != NULL)\r
- Py_INCREF(result);\r
- else {\r
- /* Note: new_weakref() can trigger cyclic GC, so the weakref\r
- list on ob can be mutated. This means that the ref and\r
- proxy pointers we got back earlier may have been collected,\r
- so we need to compute these values again before we use\r
- them. */\r
- result = new_weakref(ob, callback);\r
- if (result != NULL) {\r
- PyWeakReference *prev;\r
-\r
- if (PyCallable_Check(ob))\r
- Py_TYPE(result) = &_PyWeakref_CallableProxyType;\r
- else\r
- Py_TYPE(result) = &_PyWeakref_ProxyType;\r
- get_basic_refs(*list, &ref, &proxy);\r
- if (callback == NULL) {\r
- if (proxy != NULL) {\r
- /* Someone else added a proxy without a callback\r
- during GC. Return that one instead of this one\r
- to avoid violating the invariants of the list\r
- of weakrefs for ob. */\r
- Py_DECREF(result);\r
- Py_INCREF(result = proxy);\r
- goto skip_insert;\r
- }\r
- prev = ref;\r
- }\r
- else\r
- prev = (proxy == NULL) ? ref : proxy;\r
-\r
- if (prev == NULL)\r
- insert_head(result, list);\r
- else\r
- insert_after(result, prev);\r
- skip_insert:\r
- ;\r
- }\r
- }\r
- return (PyObject *) result;\r
-}\r
-\r
-\r
-PyObject *\r
-PyWeakref_GetObject(PyObject *ref)\r
-{\r
- if (ref == NULL || !PyWeakref_Check(ref)) {\r
- PyErr_BadInternalCall();\r
- return NULL;\r
- }\r
- return PyWeakref_GET_OBJECT(ref);\r
-}\r
-\r
-/* Note that there's an inlined copy-paste of handle_callback() in gcmodule.c's\r
- * handle_weakrefs().\r
- */\r
-static void\r
-handle_callback(PyWeakReference *ref, PyObject *callback)\r
-{\r
- PyObject *cbresult = PyObject_CallFunctionObjArgs(callback, ref, NULL);\r
-\r
- if (cbresult == NULL)\r
- PyErr_WriteUnraisable(callback);\r
- else\r
- Py_DECREF(cbresult);\r
-}\r
-\r
-/* This function is called by the tp_dealloc handler to clear weak references.\r
- *\r
- * This iterates through the weak references for 'object' and calls callbacks\r
- * for those references which have one. It returns when all callbacks have\r
- * been attempted.\r
- */\r
-void\r
-PyObject_ClearWeakRefs(PyObject *object)\r
-{\r
- PyWeakReference **list;\r
-\r
- if (object == NULL\r
- || !PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))\r
- || object->ob_refcnt != 0) {\r
- PyErr_BadInternalCall();\r
- return;\r
- }\r
- list = GET_WEAKREFS_LISTPTR(object);\r
- /* Remove the callback-less basic and proxy references */\r
- if (*list != NULL && (*list)->wr_callback == NULL) {\r
- clear_weakref(*list);\r
- if (*list != NULL && (*list)->wr_callback == NULL)\r
- clear_weakref(*list);\r
- }\r
- if (*list != NULL) {\r
- PyWeakReference *current = *list;\r
- Py_ssize_t count = _PyWeakref_GetWeakrefCount(current);\r
- int restore_error = PyErr_Occurred() ? 1 : 0;\r
- PyObject *err_type = NULL,\r
- *err_value = NULL,\r
- *err_tb = NULL;\r
-\r
- if (restore_error)\r
- PyErr_Fetch(&err_type, &err_value, &err_tb);\r
- if (count == 1) {\r
- PyObject *callback = current->wr_callback;\r
-\r
- current->wr_callback = NULL;\r
- clear_weakref(current);\r
- if (callback != NULL) {\r
- if (current->ob_refcnt > 0)\r
- handle_callback(current, callback);\r
- Py_DECREF(callback);\r
- }\r
- }\r
- else {\r
- PyObject *tuple;\r
- Py_ssize_t i = 0;\r
-\r
- tuple = PyTuple_New(count * 2);\r
- if (tuple == NULL) {\r
- if (restore_error)\r
- PyErr_Fetch(&err_type, &err_value, &err_tb);\r
- return;\r
- }\r
-\r
- for (i = 0; i < count; ++i) {\r
- PyWeakReference *next = current->wr_next;\r
-\r
- if (current->ob_refcnt > 0)\r
- {\r
- Py_INCREF(current);\r
- PyTuple_SET_ITEM(tuple, i * 2, (PyObject *) current);\r
- PyTuple_SET_ITEM(tuple, i * 2 + 1, current->wr_callback);\r
- }\r
- else {\r
- Py_DECREF(current->wr_callback);\r
- }\r
- current->wr_callback = NULL;\r
- clear_weakref(current);\r
- current = next;\r
- }\r
- for (i = 0; i < count; ++i) {\r
- PyObject *callback = PyTuple_GET_ITEM(tuple, i * 2 + 1);\r
-\r
- /* The tuple may have slots left to NULL */\r
- if (callback != NULL) {\r
- PyObject *item = PyTuple_GET_ITEM(tuple, i * 2);\r
- handle_callback((PyWeakReference *)item, callback);\r
- }\r
- }\r
- Py_DECREF(tuple);\r
- }\r
- if (restore_error)\r
- PyErr_Restore(err_type, err_value, err_tb);\r
- }\r
-}\r