summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-12-05 19:46:42 (GMT)
committerGuido van Rossum <guido@python.org>2001-12-05 19:46:42 (GMT)
commitd331cb550221cbf0a5863968ed272fb2afc90202 (patch)
tree55cd126e9bd86860463f2dd440f9dff890e25db5
parent698da02d3b0ce3364dc4e9bd61a47d73ca4e74a7 (diff)
downloadcpython-d331cb550221cbf0a5863968ed272fb2afc90202.zip
cpython-d331cb550221cbf0a5863968ed272fb2afc90202.tar.gz
cpython-d331cb550221cbf0a5863968ed272fb2afc90202.tar.bz2
At the PythonLabs meeting someone mentioned it would make Jim really
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.
-rw-r--r--Lib/test/test_descr.py7
-rw-r--r--Misc/NEWS4
-rw-r--r--Objects/typeobject.c4
3 files changed, 7 insertions, 8 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index 48d1138..b73025b 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -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, {})
diff --git a/Misc/NEWS b/Misc/NEWS
index 565c675..58119ea 100644
--- 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
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 16591cf..14a7e86 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -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;