blob: c1238e00d35f4aa8315cd8d29790a1663a24dbc2 [file] [log] [blame]
Fred Drake41deb1e2001-02-01 05:27:45 +00001#include "Python.h"
Fred Drake41deb1e2001-02-01 05:27:45 +00002
3
4#define GET_WEAKREFS_LISTPTR(o) \
5 ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
6
Larry Hastings61272b72014-01-07 12:41:53 -08007/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07008module _weakref
Larry Hastings61272b72014-01-07 12:41:53 -08009[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080010/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ffec73b85846596d]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -080011
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030012#include "clinic/_weakref.c.h"
13
Larry Hastings61272b72014-01-07 12:41:53 -080014/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -070015
16_weakref.getweakrefcount -> Py_ssize_t
17
18 object: object
19 /
20
21Return the number of weak references to 'object'.
Larry Hastings61272b72014-01-07 12:41:53 -080022[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -070023
Larry Hastings31826802013-10-19 00:09:25 -070024static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030025_weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
26/*[clinic end generated code: output=301806d59558ff3e input=cedb69711b6a2507]*/
Larry Hastings31826802013-10-19 00:09:25 -070027{
28 PyWeakReference **list;
Fred Drake7fdc0a12001-08-16 14:11:30 +000029
Larry Hastings31826802013-10-19 00:09:25 -070030 if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)))
31 return 0;
Larry Hastings61272b72014-01-07 12:41:53 -080032
Larry Hastings31826802013-10-19 00:09:25 -070033 list = GET_WEAKREFS_LISTPTR(object);
34 return _PyWeakref_GetWeakrefCount(*list);
Fred Drake41deb1e2001-02-01 05:27:45 +000035}
36
37
Antoine Pitroue10ca3a2016-12-27 14:19:20 +010038static int
39is_dead_weakref(PyObject *value)
40{
41 if (!PyWeakref_Check(value)) {
42 PyErr_SetString(PyExc_TypeError, "not a weakref");
43 return -1;
44 }
45 return PyWeakref_GET_OBJECT(value) == Py_None;
46}
47
48/*[clinic input]
49
50_weakref._remove_dead_weakref -> object
51
52 dct: object(subclass_of='&PyDict_Type')
53 key: object
54 /
55
56Atomically remove key from dict if it points to a dead weakref.
57[clinic start generated code]*/
58
59static PyObject *
60_weakref__remove_dead_weakref_impl(PyObject *module, PyObject *dct,
61 PyObject *key)
62/*[clinic end generated code: output=d9ff53061fcb875c input=19fc91f257f96a1d]*/
63{
64 if (_PyDict_DelItemIf(dct, key, is_dead_weakref) < 0) {
65 if (PyErr_ExceptionMatches(PyExc_KeyError))
66 /* This function is meant to allow safe weak-value dicts
67 with GC in another thread (see issue #28427), so it's
68 ok if the key doesn't exist anymore.
69 */
70 PyErr_Clear();
71 else
72 return NULL;
73 }
74 Py_RETURN_NONE;
75}
76
77
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000078PyDoc_STRVAR(weakref_getweakrefs__doc__,
Fred Drake7855aba2001-02-18 05:20:18 +000079"getweakrefs(object) -- return a list of all weak reference objects\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000080"that point to 'object'.");
Fred Drake7855aba2001-02-18 05:20:18 +000081
Fred Drake41deb1e2001-02-01 05:27:45 +000082static PyObject *
Fred Drake7fdc0a12001-08-16 14:11:30 +000083weakref_getweakrefs(PyObject *self, PyObject *object)
Fred Drake41deb1e2001-02-01 05:27:45 +000084{
85 PyObject *result = NULL;
Fred Drake41deb1e2001-02-01 05:27:45 +000086
Christian Heimes90aa7642007-12-19 02:45:37 +000087 if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
Fred Drake7fdc0a12001-08-16 14:11:30 +000088 PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000089 Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
Fred Drake41deb1e2001-02-01 05:27:45 +000090
Fred Drake7fdc0a12001-08-16 14:11:30 +000091 result = PyList_New(count);
92 if (result != NULL) {
93 PyWeakReference *current = *list;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000094 Py_ssize_t i;
Fred Drake7fdc0a12001-08-16 14:11:30 +000095 for (i = 0; i < count; ++i) {
96 PyList_SET_ITEM(result, i, (PyObject *) current);
97 Py_INCREF(current);
98 current = current->wr_next;
Fred Drake41deb1e2001-02-01 05:27:45 +000099 }
100 }
Fred Drake7fdc0a12001-08-16 14:11:30 +0000101 }
102 else {
103 result = PyList_New(0);
Fred Drake41deb1e2001-02-01 05:27:45 +0000104 }
105 return result;
106}
107
108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000109PyDoc_STRVAR(weakref_proxy__doc__,
Fred Drake7855aba2001-02-18 05:20:18 +0000110"proxy(object[, callback]) -- create a proxy object that weakly\n"
111"references 'object'. 'callback', if given, is called with a\n"
Fred Drake610291c2002-08-02 20:23:40 +0000112"reference to the proxy when 'object' is about to be finalized.");
Fred Drake7855aba2001-02-18 05:20:18 +0000113
Fred Drake41deb1e2001-02-01 05:27:45 +0000114static PyObject *
115weakref_proxy(PyObject *self, PyObject *args)
116{
117 PyObject *object;
118 PyObject *callback = NULL;
Fred Drakef7f8cad2001-10-05 22:00:24 +0000119 PyObject *result = NULL;
Fred Drake41deb1e2001-02-01 05:27:45 +0000120
Fred Drake0d429e82001-10-23 21:12:47 +0000121 if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) {
Fred Drakef7f8cad2001-10-05 22:00:24 +0000122 result = PyWeakref_NewProxy(object, callback);
Fred Drake41deb1e2001-02-01 05:27:45 +0000123 }
Fred Drakef7f8cad2001-10-05 22:00:24 +0000124 return result;
Fred Drake41deb1e2001-02-01 05:27:45 +0000125}
126
127
128static PyMethodDef
129weakref_functions[] = {
Larry Hastings31826802013-10-19 00:09:25 -0700130 _WEAKREF_GETWEAKREFCOUNT_METHODDEF
Antoine Pitroue10ca3a2016-12-27 14:19:20 +0100131 _WEAKREF__REMOVE_DEAD_WEAKREF_METHODDEF
Fred Drake7fdc0a12001-08-16 14:11:30 +0000132 {"getweakrefs", weakref_getweakrefs, METH_O,
Fred Drake7855aba2001-02-18 05:20:18 +0000133 weakref_getweakrefs__doc__},
134 {"proxy", weakref_proxy, METH_VARARGS,
135 weakref_proxy__doc__},
Fred Drake41deb1e2001-02-01 05:27:45 +0000136 {NULL, NULL, 0, NULL}
137};
138
139
Martin v. Löwis1a214512008-06-11 05:26:20 +0000140static struct PyModuleDef weakrefmodule = {
Serhiy Storchaka598ceae2017-11-28 17:56:10 +0200141 PyModuleDef_HEAD_INIT,
142 "_weakref",
143 "Weak-reference support module.",
144 -1,
145 weakref_functions,
146 NULL,
147 NULL,
148 NULL,
149 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000150};
151
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000152PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000153PyInit__weakref(void)
Fred Drake41deb1e2001-02-01 05:27:45 +0000154{
155 PyObject *m;
156
Martin v. Löwis1a214512008-06-11 05:26:20 +0000157 m = PyModule_Create(&weakrefmodule);
Larry Hastings31826802013-10-19 00:09:25 -0700158
Fred Drake41deb1e2001-02-01 05:27:45 +0000159 if (m != NULL) {
Fred Drakef7f8cad2001-10-05 22:00:24 +0000160 Py_INCREF(&_PyWeakref_RefType);
Fred Drake0a4dd392004-07-02 18:57:45 +0000161 PyModule_AddObject(m, "ref",
162 (PyObject *) &_PyWeakref_RefType);
163 Py_INCREF(&_PyWeakref_RefType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000164 PyModule_AddObject(m, "ReferenceType",
Fred Drakef7f8cad2001-10-05 22:00:24 +0000165 (PyObject *) &_PyWeakref_RefType);
166 Py_INCREF(&_PyWeakref_ProxyType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000167 PyModule_AddObject(m, "ProxyType",
Fred Drakef7f8cad2001-10-05 22:00:24 +0000168 (PyObject *) &_PyWeakref_ProxyType);
169 Py_INCREF(&_PyWeakref_CallableProxyType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000170 PyModule_AddObject(m, "CallableProxyType",
Fred Drakef7f8cad2001-10-05 22:00:24 +0000171 (PyObject *) &_PyWeakref_CallableProxyType);
Fred Drake41deb1e2001-02-01 05:27:45 +0000172 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000173 return m;
Fred Drake41deb1e2001-02-01 05:27:45 +0000174}