From 0a4dd390bf653128de8bc2e99da64967c8cdf86e Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Fri, 2 Jul 2004 18:57:45 +0000 Subject: Make weak references subclassable: - weakref.ref and weakref.ReferenceType will become aliases for each other - weakref.ref will be a modern, new-style class with proper __new__ and __init__ methods - weakref.WeakValueDictionary will have a lighter memory footprint, using a new weakref.ref subclass to associate the key with the value, allowing us to have only a single object of overhead for each dictionary entry (currently, there are 3 objects of overhead per entry: a weakref to the value, a weakref to the dictionary, and a function object used as a weakref callback; the weakref to the dictionary could be avoided without this change) - a new macro, PyWeakref_CheckRefExact(), will be added - PyWeakref_CheckRef() will check for subclasses of weakref.ref This closes SF patch #983019. --- Modules/_weakref.c | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) (limited to 'Modules/_weakref.c') diff --git a/Modules/_weakref.c b/Modules/_weakref.c index 21521150ae..2dfdc1424d 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -57,26 +57,6 @@ weakref_getweakrefs(PyObject *self, PyObject *object) } -PyDoc_STRVAR(weakref_ref__doc__, -"ref(object[, callback]) -- create a weak reference to 'object';\n" -"when 'object' is finalized, 'callback' will be called and passed\n" -"a reference to the weak reference object when 'object' is about\n" -"to be finalized."); - -static PyObject * -weakref_ref(PyObject *self, PyObject *args) -{ - PyObject *object; - PyObject *callback = NULL; - PyObject *result = NULL; - - if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) { - result = PyWeakref_NewRef(object, callback); - } - return result; -} - - PyDoc_STRVAR(weakref_proxy__doc__, "proxy(object[, callback]) -- create a proxy object that weakly\n" "references 'object'. 'callback', if given, is called with a\n" @@ -104,8 +84,6 @@ weakref_functions[] = { weakref_getweakrefs__doc__}, {"proxy", weakref_proxy, METH_VARARGS, weakref_proxy__doc__}, - {"ref", weakref_ref, METH_VARARGS, - weakref_ref__doc__}, {NULL, NULL, 0, NULL} }; @@ -119,6 +97,9 @@ init_weakref(void) "Weak-reference support module."); if (m != NULL) { Py_INCREF(&_PyWeakref_RefType); + PyModule_AddObject(m, "ref", + (PyObject *) &_PyWeakref_RefType); + Py_INCREF(&_PyWeakref_RefType); PyModule_AddObject(m, "ReferenceType", (PyObject *) &_PyWeakref_RefType); Py_INCREF(&_PyWeakref_ProxyType); -- cgit v1.2.1