[Python-checkins] CVS: python/dist/src/Objects dictobject.c,2.76,2.77

Guido van Rossum gvanrossum@users.sourceforge.net
Fri, 20 Apr 2001 09:50:42 -0700


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

Modified Files:
	dictobject.c 
Log Message:
Implement, test and document "key in dict" and "key not in dict".

I know some people don't like this -- if it's really controversial,
I'll take it out again.  (If it's only Alex Martelli who doesn't like
it, that doesn't count as "real controversial" though. :-)

That's why this is a separate checkin from the iterators stuff I'm
about to check in next.



Index: dictobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/dictobject.c,v
retrieving revision 2.76
retrieving revision 2.77
diff -C2 -r2.76 -r2.77
*** dictobject.c	2001/04/16 00:02:32	2.76
--- dictobject.c	2001/04/20 16:50:40	2.77
***************
*** 1293,1296 ****
--- 1293,1330 ----
  }
  
+ static int
+ dict_contains(dictobject *mp, PyObject *key)
+ {
+ 	long hash;
+ 
+ #ifdef CACHE_HASH
+ 	if (!PyString_Check(key) ||
+ 	    (hash = ((PyStringObject *) key)->ob_shash) == -1)
+ #endif
+ 	{
+ 		hash = PyObject_Hash(key);
+ 		if (hash == -1)
+ 			return -1;
+ 	}
+ 	return (mp->ma_size != 0
+ 		&& (mp->ma_lookup)(mp, key, hash)->me_value != NULL);
+ }
+ 
+ staticforward PyObject *dictiter_new(dictobject *);
+ 
+ /* Hack to implement "key in dict" */
+ static PySequenceMethods dict_as_sequence = {
+ 	0,					/* sq_length */
+ 	0,					/* sq_concat */
+ 	0,					/* sq_repeat */
+ 	0,					/* sq_item */
+ 	0,					/* sq_slice */
+ 	0,					/* sq_ass_item */
+ 	0,					/* sq_ass_slice */
+ 	(objobjproc)dict_contains,		/* sq_contains */
+ 	0,					/* sq_inplace_concat */
+ 	0,					/* sq_inplace_repeat */
+ };
+ 
  PyTypeObject PyDict_Type = {
  	PyObject_HEAD_INIT(&PyType_Type)
***************
*** 1306,1310 ****
  	(reprfunc)dict_repr,			/* tp_repr */
  	0,					/* tp_as_number */
! 	0,					/* tp_as_sequence */
  	&dict_as_mapping,			/* tp_as_mapping */
  	0,					/* tp_hash */
--- 1340,1344 ----
  	(reprfunc)dict_repr,			/* tp_repr */
  	0,					/* tp_as_number */
! 	&dict_as_sequence,			/* tp_as_sequence */
  	&dict_as_mapping,			/* tp_as_mapping */
  	0,					/* tp_hash */