ยปCore Development>Code coverage>Objects/weakrefobject.c

Python code coverage for Objects/weakrefobject.c

#countcontent
1n/a#include "Python.h"
2n/a#include "structmember.h"
3n/a
4n/a
5n/a#define GET_WEAKREFS_LISTPTR(o) \
6n/a ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
7n/a
8n/a
9n/aPy_ssize_t
10n/a_PyWeakref_GetWeakrefCount(PyWeakReference *head)
11n/a{
12n/a Py_ssize_t count = 0;
13n/a
14n/a while (head != NULL) {
15n/a ++count;
16n/a head = head->wr_next;
17n/a }
18n/a return count;
19n/a}
20n/a
21n/a
22n/astatic void
23n/ainit_weakref(PyWeakReference *self, PyObject *ob, PyObject *callback)
24n/a{
25n/a self->hash = -1;
26n/a self->wr_object = ob;
27n/a Py_XINCREF(callback);
28n/a self->wr_callback = callback;
29n/a}
30n/a
31n/astatic PyWeakReference *
32n/anew_weakref(PyObject *ob, PyObject *callback)
33n/a{
34n/a PyWeakReference *result;
35n/a
36n/a result = PyObject_GC_New(PyWeakReference, &_PyWeakref_RefType);
37n/a if (result) {
38n/a init_weakref(result, ob, callback);
39n/a PyObject_GC_Track(result);
40n/a }
41n/a return result;
42n/a}
43n/a
44n/a
45n/a/* This function clears the passed-in reference and removes it from the
46n/a * list of weak references for the referent. This is the only code that
47n/a * removes an item from the doubly-linked list of weak references for an
48n/a * object; it is also responsible for clearing the callback slot.
49n/a */
50n/astatic void
51n/aclear_weakref(PyWeakReference *self)
52n/a{
53n/a PyObject *callback = self->wr_callback;
54n/a
55n/a if (self->wr_object != Py_None) {
56n/a PyWeakReference **list = GET_WEAKREFS_LISTPTR(self->wr_object);
57n/a
58n/a if (*list == self)
59n/a /* If 'self' is the end of the list (and thus self->wr_next == NULL)
60n/a then the weakref list itself (and thus the value of *list) will
61n/a end up being set to NULL. */
62n/a *list = self->wr_next;
63n/a self->wr_object = Py_None;
64n/a if (self->wr_prev != NULL)
65n/a self->wr_prev->wr_next = self->wr_next;
66n/a if (self->wr_next != NULL)
67n/a self->wr_next->wr_prev = self->wr_prev;
68n/a self->wr_prev = NULL;
69n/a self->wr_next = NULL;
70n/a }
71n/a if (callback != NULL) {
72n/a Py_DECREF(callback);
73n/a self->wr_callback = NULL;
74n/a }
75n/a}
76n/a
77n/a/* Cyclic gc uses this to *just* clear the passed-in reference, leaving
78n/a * the callback intact and uncalled. It must be possible to call self's
79n/a * tp_dealloc() after calling this, so self has to be left in a sane enough
80n/a * state for that to work. We expect tp_dealloc to decref the callback
81n/a * then. The reason for not letting clear_weakref() decref the callback
82n/a * right now is that if the callback goes away, that may in turn trigger
83n/a * another callback (if a weak reference to the callback exists) -- running
84n/a * arbitrary Python code in the middle of gc is a disaster. The convolution
85n/a * here allows gc to delay triggering such callbacks until the world is in
86n/a * a sane state again.
87n/a */
88n/avoid
89n/a_PyWeakref_ClearRef(PyWeakReference *self)
90n/a{
91n/a PyObject *callback;
92n/a
93n/a assert(self != NULL);
94n/a assert(PyWeakref_Check(self));
95n/a /* Preserve and restore the callback around clear_weakref. */
96n/a callback = self->wr_callback;
97n/a self->wr_callback = NULL;
98n/a clear_weakref(self);
99n/a self->wr_callback = callback;
100n/a}
101n/a
102n/astatic void
103n/aweakref_dealloc(PyObject *self)
104n/a{
105n/a PyObject_GC_UnTrack(self);
106n/a clear_weakref((PyWeakReference *) self);
107n/a Py_TYPE(self)->tp_free(self);
108n/a}
109n/a
110n/a
111n/astatic int
112n/agc_traverse(PyWeakReference *self, visitproc visit, void *arg)
113n/a{
114n/a Py_VISIT(self->wr_callback);
115n/a return 0;
116n/a}
117n/a
118n/a
119n/astatic int
120n/agc_clear(PyWeakReference *self)
121n/a{
122n/a clear_weakref(self);
123n/a return 0;
124n/a}
125n/a
126n/a
127n/astatic PyObject *
128n/aweakref_call(PyWeakReference *self, PyObject *args, PyObject *kw)
129n/a{
130n/a static char *kwlist[] = {NULL};
131n/a
132n/a if (PyArg_ParseTupleAndKeywords(args, kw, ":__call__", kwlist)) {
133n/a PyObject *object = PyWeakref_GET_OBJECT(self);
134n/a Py_INCREF(object);
135n/a return (object);
136n/a }
137n/a return NULL;
138n/a}
139n/a
140n/a
141n/astatic Py_hash_t
142n/aweakref_hash(PyWeakReference *self)
143n/a{
144n/a if (self->hash != -1)
145n/a return self->hash;
146n/a if (PyWeakref_GET_OBJECT(self) == Py_None) {
147n/a PyErr_SetString(PyExc_TypeError, "weak object has gone away");
148n/a return -1;
149n/a }
150n/a self->hash = PyObject_Hash(PyWeakref_GET_OBJECT(self));
151n/a return self->hash;
152n/a}
153n/a
154n/a
155n/astatic PyObject *
156n/aweakref_repr(PyWeakReference *self)
157n/a{
158n/a PyObject *name, *repr;
159n/a _Py_IDENTIFIER(__name__);
160n/a
161n/a if (PyWeakref_GET_OBJECT(self) == Py_None)
162n/a return PyUnicode_FromFormat("<weakref at %p; dead>", self);
163n/a
164n/a name = _PyObject_GetAttrId(PyWeakref_GET_OBJECT(self), &PyId___name__);
165n/a if (name == NULL || !PyUnicode_Check(name)) {
166n/a if (name == NULL)
167n/a PyErr_Clear();
168n/a repr = PyUnicode_FromFormat(
169n/a "<weakref at %p; to '%s' at %p>",
170n/a self,
171n/a Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name,
172n/a PyWeakref_GET_OBJECT(self));
173n/a }
174n/a else {
175n/a repr = PyUnicode_FromFormat(
176n/a "<weakref at %p; to '%s' at %p (%U)>",
177n/a self,
178n/a Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name,
179n/a PyWeakref_GET_OBJECT(self),
180n/a name);
181n/a }
182n/a Py_XDECREF(name);
183n/a return repr;
184n/a}
185n/a
186n/a/* Weak references only support equality, not ordering. Two weak references
187n/a are equal if the underlying objects are equal. If the underlying object has
188n/a gone away, they are equal if they are identical. */
189n/a
190n/astatic PyObject *
191n/aweakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
192n/a{
193n/a if ((op != Py_EQ && op != Py_NE) ||
194n/a !PyWeakref_Check(self) ||
195n/a !PyWeakref_Check(other)) {
196n/a Py_RETURN_NOTIMPLEMENTED;
197n/a }
198n/a if (PyWeakref_GET_OBJECT(self) == Py_None
199n/a || PyWeakref_GET_OBJECT(other) == Py_None) {
200n/a int res = (self == other);
201n/a if (op == Py_NE)
202n/a res = !res;
203n/a if (res)
204n/a Py_RETURN_TRUE;
205n/a else
206n/a Py_RETURN_FALSE;
207n/a }
208n/a return PyObject_RichCompare(PyWeakref_GET_OBJECT(self),
209n/a PyWeakref_GET_OBJECT(other), op);
210n/a}
211n/a
212n/a/* Given the head of an object's list of weak references, extract the
213n/a * two callback-less refs (ref and proxy). Used to determine if the
214n/a * shared references exist and to determine the back link for newly
215n/a * inserted references.
216n/a */
217n/astatic void
218n/aget_basic_refs(PyWeakReference *head,
219n/a PyWeakReference **refp, PyWeakReference **proxyp)
220n/a{
221n/a *refp = NULL;
222n/a *proxyp = NULL;
223n/a
224n/a if (head != NULL && head->wr_callback == NULL) {
225n/a /* We need to be careful that the "basic refs" aren't
226n/a subclasses of the main types. That complicates this a
227n/a little. */
228n/a if (PyWeakref_CheckRefExact(head)) {
229n/a *refp = head;
230n/a head = head->wr_next;
231n/a }
232n/a if (head != NULL
233n/a && head->wr_callback == NULL
234n/a && PyWeakref_CheckProxy(head)) {
235n/a *proxyp = head;
236n/a /* head = head->wr_next; */
237n/a }
238n/a }
239n/a}
240n/a
241n/a/* Insert 'newref' in the list after 'prev'. Both must be non-NULL. */
242n/astatic void
243n/ainsert_after(PyWeakReference *newref, PyWeakReference *prev)
244n/a{
245n/a newref->wr_prev = prev;
246n/a newref->wr_next = prev->wr_next;
247n/a if (prev->wr_next != NULL)
248n/a prev->wr_next->wr_prev = newref;
249n/a prev->wr_next = newref;
250n/a}
251n/a
252n/a/* Insert 'newref' at the head of the list; 'list' points to the variable
253n/a * that stores the head.
254n/a */
255n/astatic void
256n/ainsert_head(PyWeakReference *newref, PyWeakReference **list)
257n/a{
258n/a PyWeakReference *next = *list;
259n/a
260n/a newref->wr_prev = NULL;
261n/a newref->wr_next = next;
262n/a if (next != NULL)
263n/a next->wr_prev = newref;
264n/a *list = newref;
265n/a}
266n/a
267n/astatic int
268n/aparse_weakref_init_args(const char *funcname, PyObject *args, PyObject *kwargs,
269n/a PyObject **obp, PyObject **callbackp)
270n/a{
271n/a return PyArg_UnpackTuple(args, funcname, 1, 2, obp, callbackp);
272n/a}
273n/a
274n/astatic PyObject *
275n/aweakref___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
276n/a{
277n/a PyWeakReference *self = NULL;
278n/a PyObject *ob, *callback = NULL;
279n/a
280n/a if (parse_weakref_init_args("__new__", args, kwargs, &ob, &callback)) {
281n/a PyWeakReference *ref, *proxy;
282n/a PyWeakReference **list;
283n/a
284n/a if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
285n/a PyErr_Format(PyExc_TypeError,
286n/a "cannot create weak reference to '%s' object",
287n/a Py_TYPE(ob)->tp_name);
288n/a return NULL;
289n/a }
290n/a if (callback == Py_None)
291n/a callback = NULL;
292n/a list = GET_WEAKREFS_LISTPTR(ob);
293n/a get_basic_refs(*list, &ref, &proxy);
294n/a if (callback == NULL && type == &_PyWeakref_RefType) {
295n/a if (ref != NULL) {
296n/a /* We can re-use an existing reference. */
297n/a Py_INCREF(ref);
298n/a return (PyObject *)ref;
299n/a }
300n/a }
301n/a /* We have to create a new reference. */
302n/a /* Note: the tp_alloc() can trigger cyclic GC, so the weakref
303n/a list on ob can be mutated. This means that the ref and
304n/a proxy pointers we got back earlier may have been collected,
305n/a so we need to compute these values again before we use
306n/a them. */
307n/a self = (PyWeakReference *) (type->tp_alloc(type, 0));
308n/a if (self != NULL) {
309n/a init_weakref(self, ob, callback);
310n/a if (callback == NULL && type == &_PyWeakref_RefType) {
311n/a insert_head(self, list);
312n/a }
313n/a else {
314n/a PyWeakReference *prev;
315n/a
316n/a get_basic_refs(*list, &ref, &proxy);
317n/a prev = (proxy == NULL) ? ref : proxy;
318n/a if (prev == NULL)
319n/a insert_head(self, list);
320n/a else
321n/a insert_after(self, prev);
322n/a }
323n/a }
324n/a }
325n/a return (PyObject *)self;
326n/a}
327n/a
328n/astatic int
329n/aweakref___init__(PyObject *self, PyObject *args, PyObject *kwargs)
330n/a{
331n/a PyObject *tmp;
332n/a
333n/a if (!_PyArg_NoKeywords("ref()", kwargs))
334n/a return -1;
335n/a
336n/a if (parse_weakref_init_args("__init__", args, kwargs, &tmp, &tmp))
337n/a return 0;
338n/a else
339n/a return -1;
340n/a}
341n/a
342n/a
343n/astatic PyMemberDef weakref_members[] = {
344n/a {"__callback__", T_OBJECT, offsetof(PyWeakReference, wr_callback), READONLY},
345n/a {NULL} /* Sentinel */
346n/a};
347n/a
348n/aPyTypeObject
349n/a_PyWeakref_RefType = {
350n/a PyVarObject_HEAD_INIT(&PyType_Type, 0)
351n/a "weakref",
352n/a sizeof(PyWeakReference),
353n/a 0,
354n/a weakref_dealloc, /*tp_dealloc*/
355n/a 0, /*tp_print*/
356n/a 0, /*tp_getattr*/
357n/a 0, /*tp_setattr*/
358n/a 0, /*tp_reserved*/
359n/a (reprfunc)weakref_repr, /*tp_repr*/
360n/a 0, /*tp_as_number*/
361n/a 0, /*tp_as_sequence*/
362n/a 0, /*tp_as_mapping*/
363n/a (hashfunc)weakref_hash, /*tp_hash*/
364n/a (ternaryfunc)weakref_call, /*tp_call*/
365n/a 0, /*tp_str*/
366n/a 0, /*tp_getattro*/
367n/a 0, /*tp_setattro*/
368n/a 0, /*tp_as_buffer*/
369n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
370n/a | Py_TPFLAGS_BASETYPE, /*tp_flags*/
371n/a 0, /*tp_doc*/
372n/a (traverseproc)gc_traverse, /*tp_traverse*/
373n/a (inquiry)gc_clear, /*tp_clear*/
374n/a (richcmpfunc)weakref_richcompare, /*tp_richcompare*/
375n/a 0, /*tp_weaklistoffset*/
376n/a 0, /*tp_iter*/
377n/a 0, /*tp_iternext*/
378n/a 0, /*tp_methods*/
379n/a weakref_members, /*tp_members*/
380n/a 0, /*tp_getset*/
381n/a 0, /*tp_base*/
382n/a 0, /*tp_dict*/
383n/a 0, /*tp_descr_get*/
384n/a 0, /*tp_descr_set*/
385n/a 0, /*tp_dictoffset*/
386n/a weakref___init__, /*tp_init*/
387n/a PyType_GenericAlloc, /*tp_alloc*/
388n/a weakref___new__, /*tp_new*/
389n/a PyObject_GC_Del, /*tp_free*/
390n/a};
391n/a
392n/a
393n/astatic int
394n/aproxy_checkref(PyWeakReference *proxy)
395n/a{
396n/a if (PyWeakref_GET_OBJECT(proxy) == Py_None) {
397n/a PyErr_SetString(PyExc_ReferenceError,
398n/a "weakly-referenced object no longer exists");
399n/a return 0;
400n/a }
401n/a return 1;
402n/a}
403n/a
404n/a
405n/a/* If a parameter is a proxy, check that it is still "live" and wrap it,
406n/a * replacing the original value with the raw object. Raises ReferenceError
407n/a * if the param is a dead proxy.
408n/a */
409n/a#define UNWRAP(o) \
410n/a if (PyWeakref_CheckProxy(o)) { \
411n/a if (!proxy_checkref((PyWeakReference *)o)) \
412n/a return NULL; \
413n/a o = PyWeakref_GET_OBJECT(o); \
414n/a }
415n/a
416n/a#define UNWRAP_I(o) \
417n/a if (PyWeakref_CheckProxy(o)) { \
418n/a if (!proxy_checkref((PyWeakReference *)o)) \
419n/a return -1; \
420n/a o = PyWeakref_GET_OBJECT(o); \
421n/a }
422n/a
423n/a#define WRAP_UNARY(method, generic) \
424n/a static PyObject * \
425n/a method(PyObject *proxy) { \
426n/a UNWRAP(proxy); \
427n/a return generic(proxy); \
428n/a }
429n/a
430n/a#define WRAP_BINARY(method, generic) \
431n/a static PyObject * \
432n/a method(PyObject *x, PyObject *y) { \
433n/a UNWRAP(x); \
434n/a UNWRAP(y); \
435n/a return generic(x, y); \
436n/a }
437n/a
438n/a/* Note that the third arg needs to be checked for NULL since the tp_call
439n/a * slot can receive NULL for this arg.
440n/a */
441n/a#define WRAP_TERNARY(method, generic) \
442n/a static PyObject * \
443n/a method(PyObject *proxy, PyObject *v, PyObject *w) { \
444n/a UNWRAP(proxy); \
445n/a UNWRAP(v); \
446n/a if (w != NULL) \
447n/a UNWRAP(w); \
448n/a return generic(proxy, v, w); \
449n/a }
450n/a
451n/a#define WRAP_METHOD(method, special) \
452n/a static PyObject * \
453n/a method(PyObject *proxy) { \
454n/a _Py_IDENTIFIER(special); \
455n/a UNWRAP(proxy); \
456n/a return _PyObject_CallMethodId(proxy, &PyId_##special, NULL); \
457n/a }
458n/a
459n/a
460n/a/* direct slots */
461n/a
462n/aWRAP_BINARY(proxy_getattr, PyObject_GetAttr)
463n/aWRAP_UNARY(proxy_str, PyObject_Str)
464n/aWRAP_TERNARY(proxy_call, PyEval_CallObjectWithKeywords)
465n/a
466n/astatic PyObject *
467n/aproxy_repr(PyWeakReference *proxy)
468n/a{
469n/a return PyUnicode_FromFormat(
470n/a "<weakproxy at %p to %s at %p>",
471n/a proxy,
472n/a Py_TYPE(PyWeakref_GET_OBJECT(proxy))->tp_name,
473n/a PyWeakref_GET_OBJECT(proxy));
474n/a}
475n/a
476n/a
477n/astatic int
478n/aproxy_setattr(PyWeakReference *proxy, PyObject *name, PyObject *value)
479n/a{
480n/a if (!proxy_checkref(proxy))
481n/a return -1;
482n/a return PyObject_SetAttr(PyWeakref_GET_OBJECT(proxy), name, value);
483n/a}
484n/a
485n/astatic PyObject *
486n/aproxy_richcompare(PyObject *proxy, PyObject *v, int op)
487n/a{
488n/a UNWRAP(proxy);
489n/a UNWRAP(v);
490n/a return PyObject_RichCompare(proxy, v, op);
491n/a}
492n/a
493n/a/* number slots */
494n/aWRAP_BINARY(proxy_add, PyNumber_Add)
495n/aWRAP_BINARY(proxy_sub, PyNumber_Subtract)
496n/aWRAP_BINARY(proxy_mul, PyNumber_Multiply)
497n/aWRAP_BINARY(proxy_floor_div, PyNumber_FloorDivide)
498n/aWRAP_BINARY(proxy_true_div, PyNumber_TrueDivide)
499n/aWRAP_BINARY(proxy_mod, PyNumber_Remainder)
500n/aWRAP_BINARY(proxy_divmod, PyNumber_Divmod)
501n/aWRAP_TERNARY(proxy_pow, PyNumber_Power)
502n/aWRAP_UNARY(proxy_neg, PyNumber_Negative)
503n/aWRAP_UNARY(proxy_pos, PyNumber_Positive)
504n/aWRAP_UNARY(proxy_abs, PyNumber_Absolute)
505n/aWRAP_UNARY(proxy_invert, PyNumber_Invert)
506n/aWRAP_BINARY(proxy_lshift, PyNumber_Lshift)
507n/aWRAP_BINARY(proxy_rshift, PyNumber_Rshift)
508n/aWRAP_BINARY(proxy_and, PyNumber_And)
509n/aWRAP_BINARY(proxy_xor, PyNumber_Xor)
510n/aWRAP_BINARY(proxy_or, PyNumber_Or)
511n/aWRAP_UNARY(proxy_int, PyNumber_Long)
512n/aWRAP_UNARY(proxy_float, PyNumber_Float)
513n/aWRAP_BINARY(proxy_iadd, PyNumber_InPlaceAdd)
514n/aWRAP_BINARY(proxy_isub, PyNumber_InPlaceSubtract)
515n/aWRAP_BINARY(proxy_imul, PyNumber_InPlaceMultiply)
516n/aWRAP_BINARY(proxy_ifloor_div, PyNumber_InPlaceFloorDivide)
517n/aWRAP_BINARY(proxy_itrue_div, PyNumber_InPlaceTrueDivide)
518n/aWRAP_BINARY(proxy_imod, PyNumber_InPlaceRemainder)
519n/aWRAP_TERNARY(proxy_ipow, PyNumber_InPlacePower)
520n/aWRAP_BINARY(proxy_ilshift, PyNumber_InPlaceLshift)
521n/aWRAP_BINARY(proxy_irshift, PyNumber_InPlaceRshift)
522n/aWRAP_BINARY(proxy_iand, PyNumber_InPlaceAnd)
523n/aWRAP_BINARY(proxy_ixor, PyNumber_InPlaceXor)
524n/aWRAP_BINARY(proxy_ior, PyNumber_InPlaceOr)
525n/aWRAP_UNARY(proxy_index, PyNumber_Index)
526n/a
527n/astatic int
528n/aproxy_bool(PyWeakReference *proxy)
529n/a{
530n/a PyObject *o = PyWeakref_GET_OBJECT(proxy);
531n/a if (!proxy_checkref(proxy))
532n/a return -1;
533n/a return PyObject_IsTrue(o);
534n/a}
535n/a
536n/astatic void
537n/aproxy_dealloc(PyWeakReference *self)
538n/a{
539n/a if (self->wr_callback != NULL)
540n/a PyObject_GC_UnTrack((PyObject *)self);
541n/a clear_weakref(self);
542n/a PyObject_GC_Del(self);
543n/a}
544n/a
545n/a/* sequence slots */
546n/a
547n/astatic int
548n/aproxy_contains(PyWeakReference *proxy, PyObject *value)
549n/a{
550n/a if (!proxy_checkref(proxy))
551n/a return -1;
552n/a return PySequence_Contains(PyWeakref_GET_OBJECT(proxy), value);
553n/a}
554n/a
555n/a
556n/a/* mapping slots */
557n/a
558n/astatic Py_ssize_t
559n/aproxy_length(PyWeakReference *proxy)
560n/a{
561n/a if (!proxy_checkref(proxy))
562n/a return -1;
563n/a return PyObject_Length(PyWeakref_GET_OBJECT(proxy));
564n/a}
565n/a
566n/aWRAP_BINARY(proxy_getitem, PyObject_GetItem)
567n/a
568n/astatic int
569n/aproxy_setitem(PyWeakReference *proxy, PyObject *key, PyObject *value)
570n/a{
571n/a if (!proxy_checkref(proxy))
572n/a return -1;
573n/a
574n/a if (value == NULL)
575n/a return PyObject_DelItem(PyWeakref_GET_OBJECT(proxy), key);
576n/a else
577n/a return PyObject_SetItem(PyWeakref_GET_OBJECT(proxy), key, value);
578n/a}
579n/a
580n/a/* iterator slots */
581n/a
582n/astatic PyObject *
583n/aproxy_iter(PyWeakReference *proxy)
584n/a{
585n/a if (!proxy_checkref(proxy))
586n/a return NULL;
587n/a return PyObject_GetIter(PyWeakref_GET_OBJECT(proxy));
588n/a}
589n/a
590n/astatic PyObject *
591n/aproxy_iternext(PyWeakReference *proxy)
592n/a{
593n/a if (!proxy_checkref(proxy))
594n/a return NULL;
595n/a return PyIter_Next(PyWeakref_GET_OBJECT(proxy));
596n/a}
597n/a
598n/a
599n/aWRAP_METHOD(proxy_bytes, __bytes__)
600n/a
601n/a
602n/astatic PyMethodDef proxy_methods[] = {
603n/a {"__bytes__", (PyCFunction)proxy_bytes, METH_NOARGS},
604n/a {NULL, NULL}
605n/a};
606n/a
607n/a
608n/astatic PyNumberMethods proxy_as_number = {
609n/a proxy_add, /*nb_add*/
610n/a proxy_sub, /*nb_subtract*/
611n/a proxy_mul, /*nb_multiply*/
612n/a proxy_mod, /*nb_remainder*/
613n/a proxy_divmod, /*nb_divmod*/
614n/a proxy_pow, /*nb_power*/
615n/a proxy_neg, /*nb_negative*/
616n/a proxy_pos, /*nb_positive*/
617n/a proxy_abs, /*nb_absolute*/
618n/a (inquiry)proxy_bool, /*nb_bool*/
619n/a proxy_invert, /*nb_invert*/
620n/a proxy_lshift, /*nb_lshift*/
621n/a proxy_rshift, /*nb_rshift*/
622n/a proxy_and, /*nb_and*/
623n/a proxy_xor, /*nb_xor*/
624n/a proxy_or, /*nb_or*/
625n/a proxy_int, /*nb_int*/
626n/a 0, /*nb_reserved*/
627n/a proxy_float, /*nb_float*/
628n/a proxy_iadd, /*nb_inplace_add*/
629n/a proxy_isub, /*nb_inplace_subtract*/
630n/a proxy_imul, /*nb_inplace_multiply*/
631n/a proxy_imod, /*nb_inplace_remainder*/
632n/a proxy_ipow, /*nb_inplace_power*/
633n/a proxy_ilshift, /*nb_inplace_lshift*/
634n/a proxy_irshift, /*nb_inplace_rshift*/
635n/a proxy_iand, /*nb_inplace_and*/
636n/a proxy_ixor, /*nb_inplace_xor*/
637n/a proxy_ior, /*nb_inplace_or*/
638n/a proxy_floor_div, /*nb_floor_divide*/
639n/a proxy_true_div, /*nb_true_divide*/
640n/a proxy_ifloor_div, /*nb_inplace_floor_divide*/
641n/a proxy_itrue_div, /*nb_inplace_true_divide*/
642n/a proxy_index, /*nb_index*/
643n/a};
644n/a
645n/astatic PySequenceMethods proxy_as_sequence = {
646n/a (lenfunc)proxy_length, /*sq_length*/
647n/a 0, /*sq_concat*/
648n/a 0, /*sq_repeat*/
649n/a 0, /*sq_item*/
650n/a 0, /*sq_slice*/
651n/a 0, /*sq_ass_item*/
652n/a 0, /*sq_ass_slice*/
653n/a (objobjproc)proxy_contains, /* sq_contains */
654n/a};
655n/a
656n/astatic PyMappingMethods proxy_as_mapping = {
657n/a (lenfunc)proxy_length, /*mp_length*/
658n/a proxy_getitem, /*mp_subscript*/
659n/a (objobjargproc)proxy_setitem, /*mp_ass_subscript*/
660n/a};
661n/a
662n/a
663n/aPyTypeObject
664n/a_PyWeakref_ProxyType = {
665n/a PyVarObject_HEAD_INIT(&PyType_Type, 0)
666n/a "weakproxy",
667n/a sizeof(PyWeakReference),
668n/a 0,
669n/a /* methods */
670n/a (destructor)proxy_dealloc, /* tp_dealloc */
671n/a 0, /* tp_print */
672n/a 0, /* tp_getattr */
673n/a 0, /* tp_setattr */
674n/a 0, /* tp_reserved */
675n/a (reprfunc)proxy_repr, /* tp_repr */
676n/a &proxy_as_number, /* tp_as_number */
677n/a &proxy_as_sequence, /* tp_as_sequence */
678n/a &proxy_as_mapping, /* tp_as_mapping */
679n/a 0, /* tp_hash */
680n/a 0, /* tp_call */
681n/a proxy_str, /* tp_str */
682n/a proxy_getattr, /* tp_getattro */
683n/a (setattrofunc)proxy_setattr, /* tp_setattro */
684n/a 0, /* tp_as_buffer */
685n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
686n/a 0, /* tp_doc */
687n/a (traverseproc)gc_traverse, /* tp_traverse */
688n/a (inquiry)gc_clear, /* tp_clear */
689n/a proxy_richcompare, /* tp_richcompare */
690n/a 0, /* tp_weaklistoffset */
691n/a (getiterfunc)proxy_iter, /* tp_iter */
692n/a (iternextfunc)proxy_iternext, /* tp_iternext */
693n/a proxy_methods, /* tp_methods */
694n/a};
695n/a
696n/a
697n/aPyTypeObject
698n/a_PyWeakref_CallableProxyType = {
699n/a PyVarObject_HEAD_INIT(&PyType_Type, 0)
700n/a "weakcallableproxy",
701n/a sizeof(PyWeakReference),
702n/a 0,
703n/a /* methods */
704n/a (destructor)proxy_dealloc, /* tp_dealloc */
705n/a 0, /* tp_print */
706n/a 0, /* tp_getattr */
707n/a 0, /* tp_setattr */
708n/a 0, /* tp_reserved */
709n/a (unaryfunc)proxy_repr, /* tp_repr */
710n/a &proxy_as_number, /* tp_as_number */
711n/a &proxy_as_sequence, /* tp_as_sequence */
712n/a &proxy_as_mapping, /* tp_as_mapping */
713n/a 0, /* tp_hash */
714n/a proxy_call, /* tp_call */
715n/a proxy_str, /* tp_str */
716n/a proxy_getattr, /* tp_getattro */
717n/a (setattrofunc)proxy_setattr, /* tp_setattro */
718n/a 0, /* tp_as_buffer */
719n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
720n/a 0, /* tp_doc */
721n/a (traverseproc)gc_traverse, /* tp_traverse */
722n/a (inquiry)gc_clear, /* tp_clear */
723n/a proxy_richcompare, /* tp_richcompare */
724n/a 0, /* tp_weaklistoffset */
725n/a (getiterfunc)proxy_iter, /* tp_iter */
726n/a (iternextfunc)proxy_iternext, /* tp_iternext */
727n/a};
728n/a
729n/a
730n/a
731n/aPyObject *
732n/aPyWeakref_NewRef(PyObject *ob, PyObject *callback)
733n/a{
734n/a PyWeakReference *result = NULL;
735n/a PyWeakReference **list;
736n/a PyWeakReference *ref, *proxy;
737n/a
738n/a if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
739n/a PyErr_Format(PyExc_TypeError,
740n/a "cannot create weak reference to '%s' object",
741n/a Py_TYPE(ob)->tp_name);
742n/a return NULL;
743n/a }
744n/a list = GET_WEAKREFS_LISTPTR(ob);
745n/a get_basic_refs(*list, &ref, &proxy);
746n/a if (callback == Py_None)
747n/a callback = NULL;
748n/a if (callback == NULL)
749n/a /* return existing weak reference if it exists */
750n/a result = ref;
751n/a if (result != NULL)
752n/a Py_INCREF(result);
753n/a else {
754n/a /* Note: new_weakref() can trigger cyclic GC, so the weakref
755n/a list on ob can be mutated. This means that the ref and
756n/a proxy pointers we got back earlier may have been collected,
757n/a so we need to compute these values again before we use
758n/a them. */
759n/a result = new_weakref(ob, callback);
760n/a if (result != NULL) {
761n/a get_basic_refs(*list, &ref, &proxy);
762n/a if (callback == NULL) {
763n/a if (ref == NULL)
764n/a insert_head(result, list);
765n/a else {
766n/a /* Someone else added a ref without a callback
767n/a during GC. Return that one instead of this one
768n/a to avoid violating the invariants of the list
769n/a of weakrefs for ob. */
770n/a Py_DECREF(result);
771n/a Py_INCREF(ref);
772n/a result = ref;
773n/a }
774n/a }
775n/a else {
776n/a PyWeakReference *prev;
777n/a
778n/a prev = (proxy == NULL) ? ref : proxy;
779n/a if (prev == NULL)
780n/a insert_head(result, list);
781n/a else
782n/a insert_after(result, prev);
783n/a }
784n/a }
785n/a }
786n/a return (PyObject *) result;
787n/a}
788n/a
789n/a
790n/aPyObject *
791n/aPyWeakref_NewProxy(PyObject *ob, PyObject *callback)
792n/a{
793n/a PyWeakReference *result = NULL;
794n/a PyWeakReference **list;
795n/a PyWeakReference *ref, *proxy;
796n/a
797n/a if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
798n/a PyErr_Format(PyExc_TypeError,
799n/a "cannot create weak reference to '%s' object",
800n/a Py_TYPE(ob)->tp_name);
801n/a return NULL;
802n/a }
803n/a list = GET_WEAKREFS_LISTPTR(ob);
804n/a get_basic_refs(*list, &ref, &proxy);
805n/a if (callback == Py_None)
806n/a callback = NULL;
807n/a if (callback == NULL)
808n/a /* attempt to return an existing weak reference if it exists */
809n/a result = proxy;
810n/a if (result != NULL)
811n/a Py_INCREF(result);
812n/a else {
813n/a /* Note: new_weakref() can trigger cyclic GC, so the weakref
814n/a list on ob can be mutated. This means that the ref and
815n/a proxy pointers we got back earlier may have been collected,
816n/a so we need to compute these values again before we use
817n/a them. */
818n/a result = new_weakref(ob, callback);
819n/a if (result != NULL) {
820n/a PyWeakReference *prev;
821n/a
822n/a if (PyCallable_Check(ob))
823n/a Py_TYPE(result) = &_PyWeakref_CallableProxyType;
824n/a else
825n/a Py_TYPE(result) = &_PyWeakref_ProxyType;
826n/a get_basic_refs(*list, &ref, &proxy);
827n/a if (callback == NULL) {
828n/a if (proxy != NULL) {
829n/a /* Someone else added a proxy without a callback
830n/a during GC. Return that one instead of this one
831n/a to avoid violating the invariants of the list
832n/a of weakrefs for ob. */
833n/a Py_DECREF(result);
834n/a Py_INCREF(result = proxy);
835n/a goto skip_insert;
836n/a }
837n/a prev = ref;
838n/a }
839n/a else
840n/a prev = (proxy == NULL) ? ref : proxy;
841n/a
842n/a if (prev == NULL)
843n/a insert_head(result, list);
844n/a else
845n/a insert_after(result, prev);
846n/a skip_insert:
847n/a ;
848n/a }
849n/a }
850n/a return (PyObject *) result;
851n/a}
852n/a
853n/a
854n/aPyObject *
855n/aPyWeakref_GetObject(PyObject *ref)
856n/a{
857n/a if (ref == NULL || !PyWeakref_Check(ref)) {
858n/a PyErr_BadInternalCall();
859n/a return NULL;
860n/a }
861n/a return PyWeakref_GET_OBJECT(ref);
862n/a}
863n/a
864n/a/* Note that there's an inlined copy-paste of handle_callback() in gcmodule.c's
865n/a * handle_weakrefs().
866n/a */
867n/astatic void
868n/ahandle_callback(PyWeakReference *ref, PyObject *callback)
869n/a{
870n/a PyObject *cbresult = PyObject_CallFunctionObjArgs(callback, ref, NULL);
871n/a
872n/a if (cbresult == NULL)
873n/a PyErr_WriteUnraisable(callback);
874n/a else
875n/a Py_DECREF(cbresult);
876n/a}
877n/a
878n/a/* This function is called by the tp_dealloc handler to clear weak references.
879n/a *
880n/a * This iterates through the weak references for 'object' and calls callbacks
881n/a * for those references which have one. It returns when all callbacks have
882n/a * been attempted.
883n/a */
884n/avoid
885n/aPyObject_ClearWeakRefs(PyObject *object)
886n/a{
887n/a PyWeakReference **list;
888n/a
889n/a if (object == NULL
890n/a || !PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))
891n/a || object->ob_refcnt != 0) {
892n/a PyErr_BadInternalCall();
893n/a return;
894n/a }
895n/a list = GET_WEAKREFS_LISTPTR(object);
896n/a /* Remove the callback-less basic and proxy references */
897n/a if (*list != NULL && (*list)->wr_callback == NULL) {
898n/a clear_weakref(*list);
899n/a if (*list != NULL && (*list)->wr_callback == NULL)
900n/a clear_weakref(*list);
901n/a }
902n/a if (*list != NULL) {
903n/a PyWeakReference *current = *list;
904n/a Py_ssize_t count = _PyWeakref_GetWeakrefCount(current);
905n/a PyObject *err_type, *err_value, *err_tb;
906n/a
907n/a PyErr_Fetch(&err_type, &err_value, &err_tb);
908n/a if (count == 1) {
909n/a PyObject *callback = current->wr_callback;
910n/a
911n/a current->wr_callback = NULL;
912n/a clear_weakref(current);
913n/a if (callback != NULL) {
914n/a if (((PyObject *)current)->ob_refcnt > 0)
915n/a handle_callback(current, callback);
916n/a Py_DECREF(callback);
917n/a }
918n/a }
919n/a else {
920n/a PyObject *tuple;
921n/a Py_ssize_t i = 0;
922n/a
923n/a tuple = PyTuple_New(count * 2);
924n/a if (tuple == NULL) {
925n/a _PyErr_ChainExceptions(err_type, err_value, err_tb);
926n/a return;
927n/a }
928n/a
929n/a for (i = 0; i < count; ++i) {
930n/a PyWeakReference *next = current->wr_next;
931n/a
932n/a if (((PyObject *)current)->ob_refcnt > 0)
933n/a {
934n/a Py_INCREF(current);
935n/a PyTuple_SET_ITEM(tuple, i * 2, (PyObject *) current);
936n/a PyTuple_SET_ITEM(tuple, i * 2 + 1, current->wr_callback);
937n/a }
938n/a else {
939n/a Py_DECREF(current->wr_callback);
940n/a }
941n/a current->wr_callback = NULL;
942n/a clear_weakref(current);
943n/a current = next;
944n/a }
945n/a for (i = 0; i < count; ++i) {
946n/a PyObject *callback = PyTuple_GET_ITEM(tuple, i * 2 + 1);
947n/a
948n/a /* The tuple may have slots left to NULL */
949n/a if (callback != NULL) {
950n/a PyObject *item = PyTuple_GET_ITEM(tuple, i * 2);
951n/a handle_callback((PyWeakReference *)item, callback);
952n/a }
953n/a }
954n/a Py_DECREF(tuple);
955n/a }
956n/a assert(!PyErr_Occurred());
957n/a PyErr_Restore(err_type, err_value, err_tb);
958n/a }
959n/a}