[Python-checkins] python/dist/src/Objects dictobject.c, 2.148, 2.149 listobject.c, 2.168, 2.169 methodobject.c, 2.47, 2.48 setobject.c, 1.14, 1.15 typeobject.c, 2.253, 2.254

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Sat Dec 13 06:26:14 EST 2003


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

Modified Files:
	dictobject.c listobject.c methodobject.c setobject.c 
	typeobject.c 
Log Message:
* Added a new method flag, METH_COEXIST.

* Used the flag to optimize set.__contains__(), dict.__contains__(),
  dict.__getitem__(), and list.__getitem__().



Index: dictobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/dictobject.c,v
retrieving revision 2.148
retrieving revision 2.149
diff -C2 -d -r2.148 -r2.149
*** dictobject.c	25 Nov 2003 21:12:14 -0000	2.148
--- dictobject.c	13 Dec 2003 11:26:11 -0000	2.149
***************
*** 499,502 ****
--- 499,527 ----
  }
  
+ static PyObject *
+ dict_getitem(PyObject *op, PyObject *key)
+ {
+ 	long hash;
+ 	dictobject *mp = (dictobject *)op;
+ 	PyObject *v;
+ 
+ 	if (!PyDict_Check(op)) {
+ 		return NULL;
+ 	}
+ 	if (!PyString_CheckExact(key) ||
+ 	    (hash = ((PyStringObject *) key)->ob_shash) == -1)
+ 	{
+ 		hash = PyObject_Hash(key);
+ 		if (hash == -1)
+ 			return NULL;
+ 	}
+ 	v = (mp->ma_lookup)(mp, key, hash) -> me_value;
+ 	if (v == NULL)
+ 		PyErr_SetObject(PyExc_KeyError, key);
+ 	else
+ 		Py_INCREF(v);
+ 	return v;
+ }
+ 
  /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
   * dictionary if it is merely replacing the value for an existing key.
***************
*** 1736,1739 ****
--- 1761,1769 ----
  "D.has_key(k) -> True if D has a key k, else False");
  
+ PyDoc_STRVAR(contains__doc__,
+ "D.__contains__(k) -> True if D has a key k, else False");
+ 
+ PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
+ 
  PyDoc_STRVAR(get__doc__,
  "D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.");
***************
*** 1782,1785 ****
--- 1812,1819 ----
  
  static PyMethodDef mapp_methods[] = {
+ 	{"__contains__",(PyCFunction)dict_has_key,      METH_O | METH_COEXIST,
+ 	 contains__doc__},
+ 	{"__getitem__", (PyCFunction)dict_getitem,	METH_O | METH_COEXIST,
+ 	 getitem__doc__},
  	{"has_key",	(PyCFunction)dict_has_key,      METH_O,
  	 has_key__doc__},

Index: listobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/listobject.c,v
retrieving revision 2.168
retrieving revision 2.169
diff -C2 -d -r2.168 -r2.169
*** listobject.c	10 Dec 2003 07:31:08 -0000	2.168
--- listobject.c	13 Dec 2003 11:26:11 -0000	2.169
***************
*** 2372,2375 ****
--- 2372,2377 ----
  static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
  
+ PyDoc_STRVAR(getitem_doc,
+ "x.__getitem__(y) <==> x[y]");
  PyDoc_STRVAR(reversed_doc,
  "L.__reversed__() -- return a reverse iterator over the list");
***************
*** 2397,2401 ****
--- 2399,2406 ----
  cmp(x, y) -> -1, 0, 1");
  
+ static PyObject *list_subscript(PyListObject*, PyObject*);
+ 
  static PyMethodDef list_methods[] = {
+ 	{"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
  	{"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
  	{"append",	(PyCFunction)listappend,  METH_O, append_doc},

Index: methodobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/methodobject.c,v
retrieving revision 2.47
retrieving revision 2.48
diff -C2 -d -r2.47 -r2.48
*** methodobject.c	18 Feb 2003 17:18:34 -0000	2.47
--- methodobject.c	13 Dec 2003 11:26:11 -0000	2.48
***************
*** 68,72 ****
  	int size;
  
! 	switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC)) {
  	case METH_VARARGS:
  		if (kw == NULL || PyDict_Size(kw) == 0)
--- 68,72 ----
  	int size;
  
! 	switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) {
  	case METH_VARARGS:
  		if (kw == NULL || PyDict_Size(kw) == 0)

Index: setobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/setobject.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** setobject.c	25 Nov 2003 21:12:14 -0000	1.14
--- setobject.c	13 Dec 2003 11:26:11 -0000	1.15
***************
*** 157,160 ****
--- 157,182 ----
  
  static PyObject *
+ set_direct_contains(PySetObject *so, PyObject *key)
+ {
+ 	PyObject *tmp;
+ 	long result;
+ 
+ 	result = PyDict_Contains(so->data, key);
+ 	if (result == -1 && PyAnySet_Check(key)) {
+ 		PyErr_Clear();
+ 		tmp = frozenset_dict_wrapper(((PySetObject *)(key))->data);
+ 		if (tmp == NULL)
+ 			return NULL;
+ 		result = PyDict_Contains(so->data, tmp);
+ 		Py_DECREF(tmp);
+ 	}
+ 	if (result == -1)
+ 		return NULL;
+ 	return PyBool_FromLong(result);
+ }
+ 
+ PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
+ 
+ static PyObject *
  set_copy(PySetObject *so)
  {
***************
*** 969,972 ****
--- 991,996 ----
  	{"clear",	(PyCFunction)set_clear,		METH_NOARGS,
  	 clear_doc},
+ 	{"__contains__",	(PyCFunction)set_direct_contains,	METH_O | METH_COEXIST,
+ 	 contains_doc},
  	{"copy",	(PyCFunction)set_copy,		METH_NOARGS,
  	 copy_doc},
***************
*** 1095,1098 ****
--- 1119,1124 ----
  
  static PyMethodDef frozenset_methods[] = {
+ 	{"__contains__",	(PyCFunction)set_direct_contains,	METH_O | METH_COEXIST,
+ 	 contains_doc},
  	{"copy",	(PyCFunction)frozenset_copy,	METH_NOARGS,
  	 copy_doc},

Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.253
retrieving revision 2.254
diff -C2 -d -r2.253 -r2.254
*** typeobject.c	13 Nov 2003 22:50:00 -0000	2.253
--- typeobject.c	13 Dec 2003 11:26:12 -0000	2.254
***************
*** 2793,2798 ****
  	for (; meth->ml_name != NULL; meth++) {
  		PyObject *descr;
! 		if (PyDict_GetItemString(dict, meth->ml_name))
! 			continue;
  		if (meth->ml_flags & METH_CLASS) {
  			if (meth->ml_flags & METH_STATIC) {
--- 2793,2799 ----
  	for (; meth->ml_name != NULL; meth++) {
  		PyObject *descr;
! 		if (PyDict_GetItemString(dict, meth->ml_name) &&
! 			!(meth->ml_flags & METH_COEXIST))
! 				continue;
  		if (meth->ml_flags & METH_CLASS) {
  			if (meth->ml_flags & METH_STATIC) {





More information about the Python-checkins mailing list