diff options
author | Guido van Rossum <guido@python.org> | 2001-12-03 16:32:18 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-12-03 16:32:18 (GMT) |
commit | dbb53d99189075522eecc87723292c85aee15388 (patch) | |
tree | 1131490f1290ba22acf8505c9d1e7aa25308336b /Objects/dictobject.c | |
parent | cb33165ca2f8c1493b511bb7b38b44ede4211280 (diff) | |
download | cpython-dbb53d99189075522eecc87723292c85aee15388.zip cpython-dbb53d99189075522eecc87723292c85aee15388.tar.gz cpython-dbb53d99189075522eecc87723292c85aee15388.tar.bz2 |
Fix of SF bug #475877 (Mutable subtype instances are hashable).
Rather than tweaking the inheritance of type object slots (which turns
out to be too messy to try), this fix adds a __hash__ to the list and
dict types (the only mutable types I'm aware of) that explicitly
raises an error. This has the advantage that list.__hash__([]) also
raises an error (previously, this would invoke object.__hash__([]),
returning the argument's address); ditto for dict.__hash__.
The disadvantage for this fix is that 3rd party mutable types aren't
automatically fixed. This should be added to the rules for creating
subclassable extension types: if you don't want your object to be
hashable, add a tp_hash function that raises an exception.
Also, it's possible that I've forgotten about other mutable types for
which this should be done.
Diffstat (limited to 'Objects/dictobject.c')
-rw-r--r-- | Objects/dictobject.c | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 019f112..3b922a9 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -1797,6 +1797,13 @@ dict_init(PyObject *self, PyObject *args, PyObject *kwds) return result; } +static long +dict_nohash(PyObject *self) +{ + PyErr_SetString(PyExc_TypeError, "dict objects are unhashable"); + return -1; +} + static PyObject * dict_iter(dictobject *dict) { @@ -1827,7 +1834,7 @@ PyTypeObject PyDict_Type = { 0, /* tp_as_number */ &dict_as_sequence, /* tp_as_sequence */ &dict_as_mapping, /* tp_as_mapping */ - 0, /* tp_hash */ + dict_nohash, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ |