summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-12-03 16:32:18 (GMT)
committerGuido van Rossum <guido@python.org>2001-12-03 16:32:18 (GMT)
commitdbb53d99189075522eecc87723292c85aee15388 (patch)
tree1131490f1290ba22acf8505c9d1e7aa25308336b /Lib
parentcb33165ca2f8c1493b511bb7b38b44ede4211280 (diff)
downloadcpython-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 'Lib')
-rw-r--r--Lib/test/test_descr.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index f3b81ac..d76013e 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -2571,6 +2571,29 @@ def delhook():
del c
vereq(log, [1])
+def hashinherit():
+ if verbose: print "Testing hash of mutable subclasses..."
+
+ class mydict(dict):
+ pass
+ d = mydict()
+ try:
+ hash(d)
+ except TypeError:
+ pass
+ else:
+ raise TestFailed, "hash() of dict subclass should fail"
+
+ class mylist(list):
+ pass
+ d = mylist()
+ try:
+ hash(d)
+ except TypeError:
+ pass
+ else:
+ raise TestFailed, "hash() of list subclass should fail"
+
def test_main():
class_docstrings()
lists()
@@ -2623,6 +2646,7 @@ def test_main():
str_of_str_subclass()
kwdargs()
delhook()
+ hashinherit()
if verbose: print "All OK"
if __name__ == "__main__":