r79535 - in python/trunk: Include/classobject.h Lib/test/test_sys.py Lib/test/test_weakref.py Misc/NEWS Objects/classobject.c
Author: antoine.pitrou Date: Wed Mar 31 23:32:15 2010 New Revision: 79535 Log: Issue #8268: Old-style classes (not just instances) now support weak references. Modified: python/trunk/Include/classobject.h python/trunk/Lib/test/test_sys.py python/trunk/Lib/test/test_weakref.py python/trunk/Misc/NEWS python/trunk/Objects/classobject.c Modified: python/trunk/Include/classobject.h ============================================================================== --- python/trunk/Include/classobject.h (original) +++ python/trunk/Include/classobject.h Wed Mar 31 23:32:15 2010 @@ -18,6 +18,7 @@ PyObject *cl_getattr; PyObject *cl_setattr; PyObject *cl_delattr; + PyObject *cl_weakreflist; /* List of weak references */ } PyClassObject; typedef struct { Modified: python/trunk/Lib/test/test_sys.py ============================================================================== --- python/trunk/Lib/test/test_sys.py (original) +++ python/trunk/Lib/test/test_sys.py Wed Mar 31 23:32:15 2010 @@ -544,7 +544,7 @@ class class_oldstyle(): def method(): pass - check(class_oldstyle, size(h + '6P')) + check(class_oldstyle, size(h + '7P')) # instance (old-style class) check(class_oldstyle(), size(h + '3P')) # instancemethod (old-style class) Modified: python/trunk/Lib/test/test_weakref.py ============================================================================== --- python/trunk/Lib/test/test_weakref.py (original) +++ python/trunk/Lib/test/test_weakref.py Wed Mar 31 23:32:15 2010 @@ -685,6 +685,26 @@ # No exception should be raised here gc.collect() + def test_classes(self): + # Check that both old-style classes and new-style classes + # are weakrefable. + class A(object): + pass + class B: + pass + l = [] + weakref.ref(int) + a = weakref.ref(A, l.append) + A = None + gc.collect() + self.assertEqual(a(), None) + self.assertEqual(l, [a]) + b = weakref.ref(B, l.append) + B = None + gc.collect() + self.assertEqual(b(), None) + self.assertEqual(l, [a, b]) + class SubclassableWeakrefTestCase(TestBase): Modified: python/trunk/Misc/NEWS ============================================================================== --- python/trunk/Misc/NEWS (original) +++ python/trunk/Misc/NEWS Wed Mar 31 23:32:15 2010 @@ -12,6 +12,9 @@ Core and Builtins ----------------- +- Issue #8268: Old-style classes (not just instances) now support weak + references. + - Issue #8211: Save/restore CFLAGS around AC_PROG_CC in configure.in, compiler optimizations are disabled when --with-pydebug is used. Modified: python/trunk/Objects/classobject.c ============================================================================== --- python/trunk/Objects/classobject.c (original) +++ python/trunk/Objects/classobject.c Wed Mar 31 23:32:15 2010 @@ -123,6 +123,7 @@ op->cl_dict = dict; Py_XINCREF(name); op->cl_name = name; + op->cl_weakreflist = NULL; op->cl_getattr = class_lookup(op, getattrstr, &dummy); op->cl_setattr = class_lookup(op, setattrstr, &dummy); @@ -188,6 +189,8 @@ class_dealloc(PyClassObject *op) { _PyObject_GC_UNTRACK(op); + if (op->cl_weakreflist != NULL) + PyObject_ClearWeakRefs((PyObject *) op); Py_DECREF(op->cl_bases); Py_DECREF(op->cl_dict); Py_XDECREF(op->cl_name); @@ -454,7 +457,7 @@ (traverseproc)class_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ + offsetof(PyClassObject, cl_weakreflist), /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ 0, /* tp_methods */
participants (1)
-
antoine.pitrou