blob: e33cba2a3dd81f2cd0de04d1ca7d4e5afafdafb9 [file] [log] [blame]
Fred Drake41deb1e2001-02-01 05:27:45 +00001#include "Python.h"
Victor Stinner38aefc52020-04-06 14:07:02 +02002#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
Fred Drake41deb1e2001-02-01 05:27:45 +00003
4
5#define GET_WEAKREFS_LISTPTR(o) \
Victor Stinner38aefc52020-04-06 14:07:02 +02006 ((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o))
Fred Drake41deb1e2001-02-01 05:27:45 +00007
Larry Hastings61272b72014-01-07 12:41:53 -08008/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07009module _weakref
Larry Hastings61272b72014-01-07 12:41:53 -080010[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080011/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ffec73b85846596d]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -080012
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030013#include "clinic/_weakref.c.h"
14
Larry Hastings61272b72014-01-07 12:41:53 -080015/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -070016
17_weakref.getweakrefcount -> Py_ssize_t
18
19 object: object
20 /
21
22Return the number of weak references to 'object'.
Larry Hastings61272b72014-01-07 12:41:53 -080023[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -070024
Larry Hastings31826802013-10-19 00:09:25 -070025static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030026_weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
27/*[clinic end generated code: output=301806d59558ff3e input=cedb69711b6a2507]*/
Larry Hastings31826802013-10-19 00:09:25 -070028{
29 PyWeakReference **list;
Fred Drake7fdc0a12001-08-16 14:11:30 +000030
Larry Hastings31826802013-10-19 00:09:25 -070031 if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)))
32 return 0;
Larry Hastings61272b72014-01-07 12:41:53 -080033
Larry Hastings31826802013-10-19 00:09:25 -070034 list = GET_WEAKREFS_LISTPTR(object);
35 return _PyWeakref_GetWeakrefCount(*list);
Fred Drake41deb1e2001-02-01 05:27:45 +000036}
37
38
Antoine Pitroue10ca3a2016-12-27 14:19:20 +010039static int
40is_dead_weakref(PyObject *value)
41{
42 if (!PyWeakref_Check(value)) {
43 PyErr_SetString(PyExc_TypeError, "not a weakref");
44 return -1;
45 }
46 return PyWeakref_GET_OBJECT(value) == Py_None;
47}
48
49/*[clinic input]
50
51_weakref._remove_dead_weakref -> object
52
53 dct: object(subclass_of='&PyDict_Type')
54 key: object
55 /
56
57Atomically remove key from dict if it points to a dead weakref.
58[clinic start generated code]*/
59
60static PyObject *
61_weakref__remove_dead_weakref_impl(PyObject *module, PyObject *dct,
62 PyObject *key)
63/*[clinic end generated code: output=d9ff53061fcb875c input=19fc91f257f96a1d]*/
64{
65 if (_PyDict_DelItemIf(dct, key, is_dead_weakref) < 0) {
66 if (PyErr_ExceptionMatches(PyExc_KeyError))
67 /* This function is meant to allow safe weak-value dicts
68 with GC in another thread (see issue #28427), so it's
69 ok if the key doesn't exist anymore.
70 */
71 PyErr_Clear();
72 else
73 return NULL;
74 }
75 Py_RETURN_NONE;
76}
77
78
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000079PyDoc_STRVAR(weakref_getweakrefs__doc__,
Fred Drake7855aba2001-02-18 05:20:18 +000080"getweakrefs(object) -- return a list of all weak reference objects\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000081"that point to 'object'.");
Fred Drake7855aba2001-02-18 05:20:18 +000082
Fred Drake41deb1e2001-02-01 05:27:45 +000083static PyObject *
Fred Drake7fdc0a12001-08-16 14:11:30 +000084weakref_getweakrefs(PyObject *self, PyObject *object)
Fred Drake41deb1e2001-02-01 05:27:45 +000085{
86 PyObject *result = NULL;
Fred Drake41deb1e2001-02-01 05:27:45 +000087
Christian Heimes90aa7642007-12-19 02:45:37 +000088 if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
Fred Drake7fdc0a12001-08-16 14:11:30 +000089 PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000090 Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
Fred Drake41deb1e2001-02-01 05:27:45 +000091
Fred Drake7fdc0a12001-08-16 14:11:30 +000092 result = PyList_New(count);
93 if (result != NULL) {
94 PyWeakReference *current = *list;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095 Py_ssize_t i;
Fred Drake7fdc0a12001-08-16 14:11:30 +000096 for (i = 0; i < count; ++i) {
97 PyList_SET_ITEM(result, i, (PyObject *) current);
98 Py_INCREF(current);
99 current = current->wr_next;
Fred Drake41deb1e2001-02-01 05:27:45 +0000100 }
101 }
Fred Drake7fdc0a12001-08-16 14:11:30 +0000102 }
103 else {
104 result = PyList_New(0);
Fred Drake41deb1e2001-02-01 05:27:45 +0000105 }
106 return result;
107}
108
109
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000110PyDoc_STRVAR(weakref_proxy__doc__,
Fred Drake7855aba2001-02-18 05:20:18 +0000111"proxy(object[, callback]) -- create a proxy object that weakly\n"
112"references 'object'. 'callback', if given, is called with a\n"
Fred Drake610291c2002-08-02 20:23:40 +0000113"reference to the proxy when 'object' is about to be finalized.");
Fred Drake7855aba2001-02-18 05:20:18 +0000114
Fred Drake41deb1e2001-02-01 05:27:45 +0000115static PyObject *
116weakref_proxy(PyObject *self, PyObject *args)
117{
118 PyObject *object;
119 PyObject *callback = NULL;
Fred Drakef7f8cad2001-10-05 22:00:24 +0000120 PyObject *result = NULL;
Fred Drake41deb1e2001-02-01 05:27:45 +0000121
Fred Drake0d429e82001-10-23 21:12:47 +0000122 if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) {
Fred Drakef7f8cad2001-10-05 22:00:24 +0000123 result = PyWeakref_NewProxy(object, callback);
Fred Drake41deb1e2001-02-01 05:27:45 +0000124 }
Fred Drakef7f8cad2001-10-05 22:00:24 +0000125 return result;
Fred Drake41deb1e2001-02-01 05:27:45 +0000126}
127
128
129static PyMethodDef
130weakref_functions[] = {
Larry Hastings31826802013-10-19 00:09:25 -0700131 _WEAKREF_GETWEAKREFCOUNT_METHODDEF
Antoine Pitroue10ca3a2016-12-27 14:19:20 +0100132 _WEAKREF__REMOVE_DEAD_WEAKREF_METHODDEF
Fred Drake7fdc0a12001-08-16 14:11:30 +0000133 {"getweakrefs", weakref_getweakrefs, METH_O,
Fred Drake7855aba2001-02-18 05:20:18 +0000134 weakref_getweakrefs__doc__},
135 {"proxy", weakref_proxy, METH_VARARGS,
136 weakref_proxy__doc__},
Fred Drake41deb1e2001-02-01 05:27:45 +0000137 {NULL, NULL, 0, NULL}
138};
139
Victor Stinner93460d02020-03-24 18:31:19 +0100140static int
141weakref_exec(PyObject *module)
142{
143 Py_INCREF(&_PyWeakref_RefType);
144 if (PyModule_AddObject(module, "ref", (PyObject *) &_PyWeakref_RefType) < 0) {
145 Py_DECREF(&_PyWeakref_RefType);
146 return -1;
147 }
148 Py_INCREF(&_PyWeakref_RefType);
149 if (PyModule_AddObject(module, "ReferenceType",
150 (PyObject *) &_PyWeakref_RefType) < 0) {
151 Py_DECREF(&_PyWeakref_RefType);
152 return -1;
153 }
154 Py_INCREF(&_PyWeakref_ProxyType);
155 if (PyModule_AddObject(module, "ProxyType",
156 (PyObject *) &_PyWeakref_ProxyType) < 0) {
157 Py_DECREF(&_PyWeakref_ProxyType);
158 return -1;
159 }
160 Py_INCREF(&_PyWeakref_CallableProxyType);
161 if (PyModule_AddObject(module, "CallableProxyType",
162 (PyObject *) &_PyWeakref_CallableProxyType) < 0) {
163 Py_DECREF(&_PyWeakref_CallableProxyType);
164 return -1;
165 }
166
167 return 0;
168}
169
170static struct PyModuleDef_Slot weakref_slots[] = {
171 {Py_mod_exec, weakref_exec},
172 {0, NULL}
173};
Fred Drake41deb1e2001-02-01 05:27:45 +0000174
Martin v. Löwis1a214512008-06-11 05:26:20 +0000175static struct PyModuleDef weakrefmodule = {
Serhiy Storchaka598ceae2017-11-28 17:56:10 +0200176 PyModuleDef_HEAD_INIT,
177 "_weakref",
178 "Weak-reference support module.",
Victor Stinner93460d02020-03-24 18:31:19 +0100179 0,
Serhiy Storchaka598ceae2017-11-28 17:56:10 +0200180 weakref_functions,
Victor Stinner93460d02020-03-24 18:31:19 +0100181 weakref_slots,
Serhiy Storchaka598ceae2017-11-28 17:56:10 +0200182 NULL,
183 NULL,
184 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000185};
186
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000187PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000188PyInit__weakref(void)
Fred Drake41deb1e2001-02-01 05:27:45 +0000189{
Victor Stinner93460d02020-03-24 18:31:19 +0100190 return PyModuleDef_Init(&weakrefmodule);
Fred Drake41deb1e2001-02-01 05:27:45 +0000191}