[Python-checkins] CVS: python/dist/src/Objects dictobject.c,2.117,2.118 listobject.c,2.102,2.103

Guido van Rossum gvanrossum@users.sourceforge.net
Mon, 03 Dec 2001 08:32:20 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv25230/Objects

Modified Files:
	dictobject.c listobject.c 
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: dictobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/dictobject.c,v
retrieving revision 2.117
retrieving revision 2.118
diff -C2 -d -r2.117 -r2.118
*** dictobject.c	2001/10/29 22:25:44	2.117
--- dictobject.c	2001/12/03 16:32:18	2.118
***************
*** 1798,1801 ****
--- 1798,1808 ----
  }
  
+ static long
+ dict_nohash(PyObject *self)
+ {
+ 	PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
+ 	return -1;
+ }
+ 
  static PyObject *
  dict_iter(dictobject *dict)
***************
*** 1828,1832 ****
  	&dict_as_sequence,			/* tp_as_sequence */
  	&dict_as_mapping,			/* tp_as_mapping */
! 	0,					/* tp_hash */
  	0,					/* tp_call */
  	0,					/* tp_str */
--- 1835,1839 ----
  	&dict_as_sequence,			/* tp_as_sequence */
  	&dict_as_mapping,			/* tp_as_mapping */
! 	dict_nohash,				/* tp_hash */
  	0,					/* tp_call */
  	0,					/* tp_str */

Index: listobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/listobject.c,v
retrieving revision 2.102
retrieving revision 2.103
diff -C2 -d -r2.102 -r2.103
*** listobject.c	2001/10/05 20:51:38	2.102
--- listobject.c	2001/12/03 16:32:18	2.103
***************
*** 1618,1621 ****
--- 1618,1628 ----
  }
  
+ static long
+ list_nohash(PyObject *self)
+ {
+ 	PyErr_SetString(PyExc_TypeError, "list objects are unhashable");
+ 	return -1;
+ }
+ 
  static char append_doc[] =
  "L.append(object) -- append object to end";
***************
*** 1682,1686 ****
  	&list_as_sequence,			/* tp_as_sequence */
  	0,					/* tp_as_mapping */
! 	0,					/* tp_hash */
  	0,					/* tp_call */
  	0,					/* tp_str */
--- 1689,1693 ----
  	&list_as_sequence,			/* tp_as_sequence */
  	0,					/* tp_as_mapping */
! 	list_nohash,				/* tp_hash */
  	0,					/* tp_call */
  	0,					/* tp_str */
***************
*** 1772,1776 ****
  	&immutable_list_as_sequence,		/* tp_as_sequence */
  	0,					/* tp_as_mapping */
! 	0,					/* tp_hash */
  	0,					/* tp_call */
  	0,					/* tp_str */
--- 1779,1783 ----
  	&immutable_list_as_sequence,		/* tp_as_sequence */
  	0,					/* tp_as_mapping */
! 	list_nohash,				/* tp_hash */
  	0,					/* tp_call */
  	0,					/* tp_str */