annotate Modules/_weakref.c @ 49955:878d40468c4e legacy-trunk

Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
author Vinay Sajip <vinay_sajip@yahoo.co.uk>
date Wed, 03 Dec 2008 23:22:58 +0000
parents 26384b8180c7
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
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 20634
diff changeset
8 PyDoc_STRVAR(weakref_getweakrefcount__doc__,
16659
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"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 20634
diff changeset
10 "to 'object'.");
16659
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
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
17 if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
19306
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
39351
12ffed927a3d _PyWeakref_GetWeakrefCount() now returns a Py_ssize_t instead of long.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32472
diff changeset
20 result = PyInt_FromSsize_t(_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
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 20634
diff changeset
29 PyDoc_STRVAR(weakref_getweakrefs__doc__,
16659
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"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 20634
diff changeset
31 "that point to 'object'.");
16659
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
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
38 if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
19306
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);
39351
12ffed927a3d _PyWeakref_GetWeakrefCount() now returns a Py_ssize_t instead of long.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32472
diff changeset
40 Py_ssize_t 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;
39351
12ffed927a3d _PyWeakref_GetWeakrefCount() now returns a Py_ssize_t instead of long.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32472
diff changeset
45 Py_ssize_t i;
19306
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
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 20634
diff changeset
60 PyDoc_STRVAR(weakref_proxy__doc__,
16659
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
61 "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
62 "references 'object'. 'callback', if given, is called with a\n"
24503
4897e29e19cc Fix ref(), proxy() docstrings, based on comments from David Abrahams.
Fred Drake <fdrake@acm.org>
parents: 24471
diff changeset
63 "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
64
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 static PyObject *
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 weakref_proxy(PyObject *self, PyObject *args)
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67 {
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 PyObject *object;
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
69 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
70 PyObject *result = NULL;
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
71
20634
899bebc89047 Convert the ref() and proxy() implementations to use the new
Fred Drake <fdrake@acm.org>
parents: 20285
diff changeset
72 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
73 result = PyWeakref_NewProxy(object, callback);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
74 }
20285
ccb7abc714ed Adjust the _weakref module to use the public API for the weak reference
Fred Drake <fdrake@acm.org>
parents: 19581
diff changeset
75 return result;
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
76 }
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 static PyMethodDef
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
80 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
81 {"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
82 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
83 {"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
84 weakref_getweakrefs__doc__},
deec6fa80471 Move docstrings out of function table to constants defined near the
Fred Drake <fdrake@acm.org>
parents: 16434
diff changeset
85 {"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
86 weakref_proxy__doc__},
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
87 {NULL, NULL, 0, NULL}
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
88 };
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
89
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
90
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 23832
diff changeset
91 PyMODINIT_FUNC
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
92 init_weakref(void)
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
93 {
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
94 PyObject *m;
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
95
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
96 m = Py_InitModule3("_weakref", weakref_functions,
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
97 "Weak-reference support module.");
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
98 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
99 Py_INCREF(&_PyWeakref_RefType);
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 24503
diff changeset
100 PyModule_AddObject(m, "ref",
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 24503
diff changeset
101 (PyObject *) &_PyWeakref_RefType);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 24503
diff changeset
102 Py_INCREF(&_PyWeakref_RefType);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
103 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
104 (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
105 Py_INCREF(&_PyWeakref_ProxyType);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
106 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
107 (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
108 Py_INCREF(&_PyWeakref_CallableProxyType);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
109 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
110 (PyObject *) &_PyWeakref_CallableProxyType);
16385
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 }