annotate Modules/_weakref.c @ 74006:88198b93ff2f

Issue #13560: Add PyUnicode_EncodeLocale() * Use PyUnicode_EncodeLocale() in time.strftime() if wcsftime() is not available * Document my last changes in Misc/NEWS
author Victor Stinner <victor.stinner@haypocalc.com>
date Sat, 17 Dec 2011 04:13:41 +0100
parents 94f64f11fdd0
children 8fde1a2c94dc
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
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
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
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
20 result = PyLong_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
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 42334
diff changeset
23 result = PyLong_FromLong(0);
19306
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
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
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);
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
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;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
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
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
91 static struct PyModuleDef weakrefmodule = {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
92 PyModuleDef_HEAD_INIT,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
93 "_weakref",
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
94 "Weak-reference support module.",
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
95 -1,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
96 weakref_functions,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
97 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
98 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
99 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
100 NULL
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
101 };
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
102
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 23832
diff changeset
103 PyMODINIT_FUNC
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
104 PyInit__weakref(void)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
105 {
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
106 PyObject *m;
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
107
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
108 m = PyModule_Create(&weakrefmodule);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
109
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
110 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
111 Py_INCREF(&_PyWeakref_RefType);
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 24503
diff changeset
112 PyModule_AddObject(m, "ref",
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 24503
diff changeset
113 (PyObject *) &_PyWeakref_RefType);
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 24503
diff changeset
114 Py_INCREF(&_PyWeakref_RefType);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
115 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
116 (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
117 Py_INCREF(&_PyWeakref_ProxyType);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
118 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
119 (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
120 Py_INCREF(&_PyWeakref_CallableProxyType);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121 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
122 (PyObject *) &_PyWeakref_CallableProxyType);
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
123 }
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 44351
diff changeset
124 return m;
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
125 }