]> granicus.if.org Git - python/commitdiff
Fix SF bug #489581: __slots__ leak.
authorGuido van Rossum <guido@python.org>
Wed, 5 Dec 2001 22:45:48 +0000 (22:45 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 5 Dec 2001 22:45:48 +0000 (22:45 +0000)
It was easier than I thought, assuming that no other things contribute
to the instance size besides slots -- a pretty good bet.  With a test
suite, no less!

Lib/test/test_descr.py
Misc/NEWS
Objects/typeobject.c

index b73025b8684467350955d3722aa643e9cfa85bf3..481ca0f7dfb43c31c1c194c8c79cd1ea1d7dc5e8 100644 (file)
@@ -1000,6 +1000,40 @@ def slots():
     vereq(x.b, 2)
     vereq(x.c, 3)
 
+    # Test leaks
+    class Counted(object):
+        counter = 0    # counts the number of instances alive
+        def __init__(self):
+            Counted.counter += 1
+        def __del__(self):
+            Counted.counter -= 1
+    class C(object):
+        __slots__ = ['a', 'b', 'c']
+    x = C()
+    x.a = Counted()
+    x.b = Counted()
+    x.c = Counted()
+    vereq(Counted.counter, 3)
+    del x
+    vereq(Counted.counter, 0)
+    class D(C):
+        pass
+    x = D()
+    x.a = Counted()
+    x.z = Counted()
+    vereq(Counted.counter, 2)
+    del x
+    vereq(Counted.counter, 0)
+    class E(D):
+        __slots__ = ['e']
+    x = E()
+    x.a = Counted()
+    x.z = Counted()
+    x.e = Counted()
+    vereq(Counted.counter, 3)
+    del x
+    vereq(Counted.counter, 0)
+
 def dynamics():
     if verbose: print "Testing class attribute propagation..."
     class D(object):
index 58119ea73037b395eafccd1e69f547e2057f6e29..eabe9b60f561586f9c6158eb9b2f633c21252587 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -4,6 +4,9 @@ XXX Release date: ??-Dec-2001 XXX
 
 Type/class unification and new-style classes
 
+- Fixed a leak: instance variables declared with __slots__ were never
+  deleted!
+
 - The "delete attribute" method of descriptor objects is called
   __delete__, not __del__.  In previous releases, it was mistakenly
   called __del__, which created an unfortunate overloading condition
index 14a7e86c81337279ed5b07fc75a69328f78baeeb..37ab4cb3044e2bcc8e253230cc98ab22acef684b 100644 (file)
@@ -304,7 +304,7 @@ call_finalizer(PyObject *self)
 static void
 subtype_dealloc(PyObject *self)
 {
-       PyTypeObject *type, *base;
+       PyTypeObject *type, *base, *temp;
        destructor f;
 
        /* This exists so we can DECREF self->ob_type */
@@ -314,10 +314,30 @@ subtype_dealloc(PyObject *self)
 
        /* Find the nearest base with a different tp_dealloc */
        type = self->ob_type;
-       base = type->tp_base;
+       base = type;
        while ((f = base->tp_dealloc) == subtype_dealloc) {
+               temp = base;
                base = base->tp_base;
                assert(base);
+               /* While we're at it, clear __slots__ variables */
+               if (temp->tp_basicsize != base->tp_basicsize &&
+                   temp->tp_itemsize == 0)
+               {
+                       char *addr = ((char *)self);
+                       char *p = addr + base->tp_basicsize;
+                       char *q = addr + temp->tp_basicsize;
+                       for (; p < q; p += sizeof(PyObject *)) {
+                               PyObject **pp;
+                               if (p == addr + type->tp_dictoffset ||
+                                   p == addr + type->tp_weaklistoffset)
+                                       continue;
+                               pp = (PyObject **)p;
+                               if (*pp != NULL) {
+                                       Py_DECREF(*pp);
+                                       *pp = NULL;
+                               }
+                       }
+               }
        }
 
        /* If we added a dict, DECREF it */