[Python-checkins] CVS: python/dist/src/Lib/test test_descr.py,1.106,1.107
Guido van Rossum
gvanrossum@users.sourceforge.net
Mon, 03 Dec 2001 08:32:20 -0800
Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv25230/Lib/test
Modified Files:
test_descr.py
Log Message:
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.
Index: test_descr.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_descr.py,v
retrieving revision 1.106
retrieving revision 1.107
diff -C2 -d -r1.106 -r1.107
*** test_descr.py 2001/12/03 15:38:28 1.106
--- test_descr.py 2001/12/03 16:32:17 1.107
***************
*** 2572,2575 ****
--- 2572,2598 ----
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()
***************
*** 2624,2627 ****
--- 2647,2651 ----
kwdargs()
delhook()
+ hashinherit()
if verbose: print "All OK"