annotate Modules/_weakref.c @ 24502:fe2cfce4e658 2.2

Fix ref(), proxy() docstrings, based on comments from David Abrahams.
author Fred Drake <fdrake@acm.org>
date Fri, 02 Aug 2002 20:17:14 +0000
parents 899bebc89047
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 #include "Python.h"
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4 #define GET_WEAKREFS_LISTPTR(o) \
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
5 ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7
16659
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
8 static char weakref_getweakrefcount__doc__[] =
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
9 "getweakrefcount(object) -- return the number of weak references\n"
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
10 "to 'object'.";
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
11
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12 static PyObject *
19306
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
13 weakref_getweakrefcount(PyObject *self, PyObject *object)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14 {
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15 PyObject *result = NULL;
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16
19306
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
17 if (PyType_SUPPORTS_WEAKREFS(object->ob_type)) {
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
18 PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19
20285
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
20 result = PyInt_FromLong(_PyWeakref_GetWeakrefCount(*list));
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21 }
19306
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
22 else
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
23 result = PyInt_FromLong(0);
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
24
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25 return result;
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26 }
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
27
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
28
16659
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
29 static char weakref_getweakrefs__doc__[] =
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
30 "getweakrefs(object) -- return a list of all weak reference objects\n"
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
31 "that point to 'object'.";
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
32
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33 static PyObject *
19306
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
34 weakref_getweakrefs(PyObject *self, PyObject *object)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35 {
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 PyObject *result = NULL;
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37
19306
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
38 if (PyType_SUPPORTS_WEAKREFS(object->ob_type)) {
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
39 PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
20285
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
40 long count = _PyWeakref_GetWeakrefCount(*list);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41
19306
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
42 result = PyList_New(count);
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
43 if (result != NULL) {
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
44 PyWeakReference *current = *list;
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
45 long i;
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
46 for (i = 0; i < count; ++i) {
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
47 PyList_SET_ITEM(result, i, (PyObject *) current);
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
48 Py_INCREF(current);
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
49 current = current->wr_next;
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 }
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51 }
19306
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
52 }
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
53 else {
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
54 result = PyList_New(0);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 }
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56 return result;
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
57 }
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59
16659
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
60 static char weakref_ref__doc__[] =
24502
fe2cfce4e658 Fix ref(), proxy() docstrings, based on comments from David Abrahams.
Fred Drake <fdrake@acm.org>
parents: 20634
diff changeset
61 "ref(object[, callback]) -- create a weak reference to 'object';\n"
16659
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
62 "when 'object' is finalized, 'callback' will be called and passed\n"
24502
fe2cfce4e658 Fix ref(), proxy() docstrings, based on comments from David Abrahams.
Fred Drake <fdrake@acm.org>
parents: 20634
diff changeset
63 "a reference to the weak reference object when 'object' is about\n"
fe2cfce4e658 Fix ref(), proxy() docstrings, based on comments from David Abrahams.
Fred Drake <fdrake@acm.org>
parents: 20634
diff changeset
64 "to be finalized.";
16659
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
65
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 static PyObject *
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67 weakref_ref(PyObject *self, PyObject *args)
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 {
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
69 PyObject *object;
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
70 PyObject *callback = NULL;
20285
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
71 PyObject *result = NULL;
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
72
20634
899bebc89047 Convert the ref() and proxy() implementations to use the new
Fred Drake <fdrake@acm.org>
parents: 20285
diff changeset
73 if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) {
20285
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
74 result = PyWeakref_NewRef(object, callback);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
75 }
20285
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
76 return result;
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
77 }
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
78
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
79
16659
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
80 static char weakref_proxy__doc__[] =
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
81 "proxy(object[, callback]) -- create a proxy object that weakly\n"
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
82 "references 'object'. 'callback', if given, is called with a\n"
24502
fe2cfce4e658 Fix ref(), proxy() docstrings, based on comments from David Abrahams.
Fred Drake <fdrake@acm.org>
parents: 20634
diff changeset
83 "reference to the proxy when 'object' is about to be finalized.";
16659
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
84
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
85 static PyObject *
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86 weakref_proxy(PyObject *self, PyObject *args)
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
87 {
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
88 PyObject *object;
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
89 PyObject *callback = NULL;
20285
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
90 PyObject *result = NULL;
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
91
20634
899bebc89047 Convert the ref() and proxy() implementations to use the new
Fred Drake <fdrake@acm.org>
parents: 20285
diff changeset
92 if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) {
20285
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
93 result = PyWeakref_NewProxy(object, callback);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
94 }
20285
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
95 return result;
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
96 }
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
97
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
98
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
99 static PyMethodDef
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
100 weakref_functions[] = {
19306
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
101 {"getweakrefcount", weakref_getweakrefcount, METH_O,
16659
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
102 weakref_getweakrefcount__doc__},
19306
a7bbac69c8ef Use METH_O where possible (two functions). This does not lead to real
Fred Drake <fdrake@acm.org>
parents: 17670
diff changeset
103 {"getweakrefs", weakref_getweakrefs, METH_O,
16659
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
104 weakref_getweakrefs__doc__},
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
105 {"proxy", weakref_proxy, METH_VARARGS,
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
106 weakref_proxy__doc__},
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
107 {"ref", weakref_ref, METH_VARARGS,
16659
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
108 weakref_ref__doc__},
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 {NULL, NULL, 0, NULL}
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
110 };
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
111
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
112
16402
08ac556f2d12 Teach Windows build about the _weakref module.
Tim Peters <tim.peters@gmail.com>
parents: 16385
diff changeset
113 DL_EXPORT(void)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
114 init_weakref(void)
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
115 {
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
116 PyObject *m;
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
117
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
118 m = Py_InitModule3("_weakref", weakref_functions,
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
119 "Weak-reference support module.");
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 if (m != NULL) {
20285
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
121 Py_INCREF(&_PyWeakref_RefType);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
122 PyModule_AddObject(m, "ReferenceType",
20285
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
123 (PyObject *) &_PyWeakref_RefType);
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
124 Py_INCREF(&_PyWeakref_ProxyType);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
125 PyModule_AddObject(m, "ProxyType",
20285
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
126 (PyObject *) &_PyWeakref_ProxyType);
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
127 Py_INCREF(&_PyWeakref_CallableProxyType);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
128 PyModule_AddObject(m, "CallableProxyType",
20285
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
129 (PyObject *) &_PyWeakref_CallableProxyType);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
130 }
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
131 }