]> granicus.if.org Git - python/commitdiff
At the PythonLabs meeting someone mentioned it would make Jim really
authorGuido van Rossum <guido@python.org>
Wed, 5 Dec 2001 19:46:42 +0000 (19:46 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 5 Dec 2001 19:46:42 +0000 (19:46 +0000)
happy if one could delete the __dict__ attribute of an instance.  I
love to make Jim happy, so here goes...

- New-style objects now support deleting their __dict__.  This is for
  all intents and purposes equivalent to assigning a brand new empty
  dictionary, but saves space if the object is not used further.

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

index 48d1138a7d033d73b0ef6296b63bae6db7849e91..b73025b8684467350955d3722aa643e9cfa85bf3 100644 (file)
@@ -2261,12 +2261,7 @@ def setdict():
     cant(a, None)
     cant(a, [])
     cant(a, 1)
-    try:
-        del a.__dict__
-    except TypeError:
-        pass
-    else:
-        raise TestFailed, "shouldn't allow del %r.__dict__" % (a)
+    del a.__dict__ # Deleting __dict__ is allowed
     # Classes don't allow __dict__ assignment
     cant(C, {})
 
index 565c675003db013b4084706ebff0ecc00ba30381..58119ea73037b395eafccd1e69f547e2057f6e29 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -30,6 +30,10 @@ Type/class unification and new-style classes
   dict.__hash__ and list.__hash__ now raises the same TypeError
   (previously, these were the same as object.__hash__).
 
+- New-style objects now support deleting their __dict__.  This is for
+  all intents and purposes equivalent to assigning a brand new empty
+  dictionary, but saves space if the object is not used further.
+
 Core and builtins
 
 Extension modules
index 16591cf77f8eedac6d70a0db8cd495ec5c06ec88..14a7e86c81337279ed5b07fc75a69328f78baeeb 100644 (file)
@@ -812,13 +812,13 @@ subtype_setdict(PyObject *obj, PyObject *value, void *context)
                                "This object has no __dict__");
                return -1;
        }
-       if (value == NULL || !PyDict_Check(value)) {
+       if (value != NULL && !PyDict_Check(value)) {
                PyErr_SetString(PyExc_TypeError,
                                "__dict__ must be set to a dictionary");
                return -1;
        }
        dict = *dictptr;
-       Py_INCREF(value);
+       Py_XINCREF(value);
        *dictptr = value;
        Py_XDECREF(dict);
        return 0;