[Python-checkins] CVS: python/dist/src/Modules arraymodule.c,2.59,2.60

Guido van Rossum gvanrossum@users.sourceforge.net
Wed, 17 Jan 2001 17:02:57 -0800


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

Modified Files:
	arraymodule.c 
Log Message:
Same treatment as listobject.c:

- In count(), remove(), index(): call RichCompare(Py_EQ).

- Get rid of array_compare(), in favor of new array_richcompare() (a
  near clone of list_compare()).

- Aligned items in array_methods initializer and comments for type
  struct initializer.

- Folded a few long lines.


Index: arraymodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/arraymodule.c,v
retrieving revision 2.59
retrieving revision 2.60
diff -C2 -r2.59 -r2.60
*** arraymodule.c	2000/11/10 19:04:19	2.59
--- arraymodule.c	2001/01/18 01:02:55	2.60
***************
*** 415,438 ****
  }
  
! static int
! array_compare(arrayobject *v, arrayobject *w)
  {
! 	int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
! 	int i;
! 	for (i = 0; i < len; i++) {
! 		PyObject *ai, *bi;
  		int cmp;
! 		ai = getarrayitem((PyObject *)v, i);
! 		bi = getarrayitem((PyObject *)w, i);
! 		if (ai && bi)
! 			cmp = PyObject_Compare(ai, bi);
  		else
! 			cmp = -1;
! 		Py_XDECREF(ai);
! 		Py_XDECREF(bi);
! 		if (cmp != 0)
! 			return cmp;
  	}
! 	return v->ob_size - w->ob_size;
  }
  
--- 415,502 ----
  }
  
! static PyObject *
! array_richcompare(PyObject *v, PyObject *w, int op)
  {
! 	arrayobject *va, *wa;
! 	PyObject *vi = NULL;
! 	PyObject *wi = NULL;
! 	int i, k;
! 	PyObject *res;
! 
! 	if (!is_arrayobject(v) || !is_arrayobject(w)) {
! 		Py_INCREF(Py_NotImplemented);
! 		return Py_NotImplemented;
! 	}
! 
! 	va = (arrayobject *)v;
! 	wa = (arrayobject *)w;
! 
! 	if (va->ob_size != wa->ob_size && (op == Py_EQ || op == Py_NE)) {
! 		/* Shortcut: if the lengths differ, the arrays differ */
! 		if (op == Py_EQ)
! 			res = Py_False;
! 		else
! 			res = Py_True;
! 		Py_INCREF(res);
! 		return res;
! 	}
! 
! 	/* Search for the first index where items are different */
! 	k = 1;
! 	for (i = 0; i < va->ob_size && i < wa->ob_size; i++) {
! 		vi = getarrayitem(v, i);
! 		wi = getarrayitem(w, i);
! 		if (vi == NULL || wi == NULL) {
! 			Py_XDECREF(vi);
! 			Py_XDECREF(wi);
! 			return NULL;
! 		}
! 		k = PyObject_RichCompareBool(vi, wi, Py_EQ);
! 		if (k == 0)
! 			break; /* Keeping vi and wi alive! */
! 		Py_DECREF(vi);
! 		Py_DECREF(wi);
! 		if (k < 0)
! 			return NULL;
! 	}
! 
! 	if (k) {
! 		/* No more items to compare -- compare sizes */
! 		int vs = va->ob_size;
! 		int ws = wa->ob_size;
  		int cmp;
! 		switch (op) {
! 		case Py_LT: cmp = vs <  ws; break;
! 		case Py_LE: cmp = ws <= ws; break;
! 		case Py_EQ: cmp = vs == ws; break;
! 		case Py_NE: cmp = vs != ws; break;
! 		case Py_GT: cmp = vs >  ws; break;
! 		case Py_GE: cmp = vs >= ws; break;
! 		default: return NULL; /* cannot happen */
! 		}
! 		if (cmp)
! 			res = Py_True;
  		else
! 			res = Py_False;
! 		Py_INCREF(res);
! 		return res;
  	}
! 
! 	/* We have an item that differs.  First, shortcuts for EQ/NE */
! 	if (op == Py_EQ) {
! 		Py_INCREF(Py_False);
! 		res = Py_False;
! 	}
! 	else if (op == Py_NE) {
! 		Py_INCREF(Py_True);
! 		res = Py_True;
! 	}
! 	else {
! 		/* Compare the final item again using the proper operator */
! 		res = PyObject_RichCompare(vi, wi, op);
! 	}
! 	Py_DECREF(vi);
! 	Py_DECREF(wi);
! 	return res;
  }
  
***************
*** 637,644 ****
  	for (i = 0; i < self->ob_size; i++) {
  		PyObject *selfi = getarrayitem((PyObject *)self, i);
! 		if (PyObject_Compare(selfi, v) == 0)
! 			count++;
  		Py_DECREF(selfi);
! 		if (PyErr_Occurred())
  			return NULL;
  	}
--- 701,709 ----
  	for (i = 0; i < self->ob_size; i++) {
  		PyObject *selfi = getarrayitem((PyObject *)self, i);
! 		int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
  		Py_DECREF(selfi);
! 		if (cmp > 0)
! 			count++;
! 		else if (cmp < 0)
  			return NULL;
  	}
***************
*** 661,670 ****
  	for (i = 0; i < self->ob_size; i++) {
  		PyObject *selfi = getarrayitem((PyObject *)self, i);
! 		if (PyObject_Compare(selfi, v) == 0) {
! 			Py_DECREF(selfi);
  			return PyInt_FromLong((long)i);
  		}
! 		Py_DECREF(selfi);
! 		if (PyErr_Occurred())
  			return NULL;
  	}
--- 726,735 ----
  	for (i = 0; i < self->ob_size; i++) {
  		PyObject *selfi = getarrayitem((PyObject *)self, i);
! 		int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
! 		Py_DECREF(selfi);
! 		if (cmp > 0) {
  			return PyInt_FromLong((long)i);
  		}
! 		else if (cmp < 0)
  			return NULL;
  	}
***************
*** 688,693 ****
  	for (i = 0; i < self->ob_size; i++) {
  		PyObject *selfi = getarrayitem((PyObject *)self,i);
! 		if (PyObject_Compare(selfi, v) == 0) {
! 			Py_DECREF(selfi);
  			if (array_ass_slice(self, i, i+1,
  					   (PyObject *)NULL) != 0)
--- 753,759 ----
  	for (i = 0; i < self->ob_size; i++) {
  		PyObject *selfi = getarrayitem((PyObject *)self,i);
! 		int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
! 		Py_DECREF(selfi);
! 		if (cmp > 0) {
  			if (array_ass_slice(self, i, i+1,
  					   (PyObject *)NULL) != 0)
***************
*** 696,701 ****
  			return Py_None;
  		}
! 		Py_DECREF(selfi);
! 		if (PyErr_Occurred())
  			return NULL;
  	}
--- 762,766 ----
  			return Py_None;
  		}
! 		else if (cmp < 0)
  			return NULL;
  	}
***************
*** 751,756 ****
  	if (!is_arrayobject(bb)) {
  		PyErr_Format(PyExc_TypeError,
! 			     "can only extend array with array (not \"%.200s\")",
! 			     bb->ob_type->tp_name);
  		return NULL;
  	}
--- 816,821 ----
  	if (!is_arrayobject(bb)) {
  		PyErr_Format(PyExc_TypeError,
! 			"can only extend array with array (not \"%.200s\")",
! 			bb->ob_type->tp_name);
  		return NULL;
  	}
***************
*** 1138,1160 ****
  
  PyMethodDef array_methods[] = {
! 	{"append",	(PyCFunction)array_append, METH_VARARGS, append_doc},
! 	{"buffer_info", (PyCFunction)array_buffer_info, METH_VARARGS, buffer_info_doc},
! 	{"byteswap",	(PyCFunction)array_byteswap, METH_VARARGS, byteswap_doc},
! 	{"count",	(PyCFunction)array_count, METH_VARARGS, count_doc},
! 	{"extend",      (PyCFunction)array_extend, METH_VARARGS, extend_doc},
! 	{"fromfile",	(PyCFunction)array_fromfile, METH_VARARGS, fromfile_doc},
! 	{"fromlist",	(PyCFunction)array_fromlist, METH_VARARGS, fromlist_doc},
! 	{"fromstring",	(PyCFunction)array_fromstring, METH_VARARGS, fromstring_doc},
! 	{"index",	(PyCFunction)array_index, METH_VARARGS, index_doc},
! 	{"insert",	(PyCFunction)array_insert, METH_VARARGS, insert_doc},
! 	{"pop",		(PyCFunction)array_pop, METH_VARARGS, pop_doc},
! 	{"read",	(PyCFunction)array_fromfile, METH_VARARGS, fromfile_doc},
! 	{"remove",	(PyCFunction)array_remove, METH_VARARGS, remove_doc},
! 	{"reverse",	(PyCFunction)array_reverse, METH_VARARGS, reverse_doc},
! /*	{"sort",	(PyCFunction)array_sort, METH_VARARGS, sort_doc},*/
! 	{"tofile",	(PyCFunction)array_tofile, METH_VARARGS, tofile_doc},
! 	{"tolist",	(PyCFunction)array_tolist, METH_VARARGS, tolist_doc},
! 	{"tostring",	(PyCFunction)array_tostring, METH_VARARGS, tostring_doc},
! 	{"write",	(PyCFunction)array_tofile, METH_VARARGS, tofile_doc},
  	{NULL,		NULL}		/* sentinel */
  };
--- 1203,1244 ----
  
  PyMethodDef array_methods[] = {
! 	{"append",	(PyCFunction)array_append,	METH_VARARGS,
! 	 append_doc},
! 	{"buffer_info", (PyCFunction)array_buffer_info, METH_VARARGS,
! 	 buffer_info_doc},
! 	{"byteswap",	(PyCFunction)array_byteswap,	METH_VARARGS,
! 	 byteswap_doc},
! 	{"count",	(PyCFunction)array_count,	METH_VARARGS,
! 	 count_doc},
! 	{"extend",      (PyCFunction)array_extend,	METH_VARARGS,
! 	 extend_doc},
! 	{"fromfile",	(PyCFunction)array_fromfile,	METH_VARARGS,
! 	 fromfile_doc},
! 	{"fromlist",	(PyCFunction)array_fromlist,	METH_VARARGS,
! 	 fromlist_doc},
! 	{"fromstring",	(PyCFunction)array_fromstring,	METH_VARARGS,
! 	 fromstring_doc},
! 	{"index",	(PyCFunction)array_index,	METH_VARARGS,
! 	 index_doc},
! 	{"insert",	(PyCFunction)array_insert,	METH_VARARGS,
! 	 insert_doc},
! 	{"pop",		(PyCFunction)array_pop,		METH_VARARGS,
! 	 pop_doc},
! 	{"read",	(PyCFunction)array_fromfile,	METH_VARARGS,
! 	 fromfile_doc},
! 	{"remove",	(PyCFunction)array_remove,	METH_VARARGS,
! 	 remove_doc},
! 	{"reverse",	(PyCFunction)array_reverse,	METH_VARARGS,
! 	 reverse_doc},
! /*	{"sort",	(PyCFunction)array_sort,	METH_VARARGS,
! 	sort_doc},*/
! 	{"tofile",	(PyCFunction)array_tofile,	METH_VARARGS,
! 	 tofile_doc},
! 	{"tolist",	(PyCFunction)array_tolist,	METH_VARARGS,
! 	 tolist_doc},
! 	{"tostring",	(PyCFunction)array_tostring,	METH_VARARGS,
! 	 tostring_doc},
! 	{"write",	(PyCFunction)array_tofile,	METH_VARARGS,
! 	 tofile_doc},
  	{NULL,		NULL}		/* sentinel */
  };
***************
*** 1346,1352 ****
  			}
  			if (initial != NULL && PyString_Check(initial)) {
! 				PyObject *t_initial = Py_BuildValue("(O)", initial);
  				PyObject *v =
! 					array_fromstring((arrayobject *)a, t_initial);
                                  Py_DECREF(t_initial);
  				if (v == NULL) {
--- 1430,1438 ----
  			}
  			if (initial != NULL && PyString_Check(initial)) {
! 				PyObject *t_initial = Py_BuildValue("(O)",
! 								    initial);
  				PyObject *v =
! 					array_fromstring((arrayobject *)a,
! 							 t_initial);
                                  Py_DECREF(t_initial);
  				if (v == NULL) {
***************
*** 1443,1463 ****
  	sizeof(arrayobject),
  	0,
! 	(destructor)array_dealloc,	/*tp_dealloc*/
! 	(printfunc)array_print,		/*tp_print*/
! 	(getattrfunc)array_getattr,	/*tp_getattr*/
! 	0,				/*tp_setattr*/
! 	(cmpfunc)array_compare,		/*tp_compare*/
! 	(reprfunc)array_repr,		/*tp_repr*/
! 	0,				/*tp_as_number*/
! 	&array_as_sequence,		/*tp_as_sequence*/
! 	0,				/*tp_as_mapping*/
! 	0, 				/*tp_hash*/
! 	0,				/*tp_call*/
! 	0,				/*tp_str*/
! 	0,				/*tp_getattro*/
! 	0,				/*tp_setattro*/
! 	&array_as_buffer,		/*tp_as_buffer*/
! 	0,				/*tp_xxx4*/
! 	arraytype_doc,			/*tp_doc*/
  };
  
--- 1529,1552 ----
  	sizeof(arrayobject),
  	0,
! 	(destructor)array_dealloc,		/* tp_dealloc */
! 	(printfunc)array_print,			/* tp_print */
! 	(getattrfunc)array_getattr,		/* tp_getattr */
! 	0,					/* tp_setattr */
! 	0,					/* tp_compare */
! 	(reprfunc)array_repr,			/* tp_repr */
! 	0,					/* tp_as _number*/
! 	&array_as_sequence,			/* tp_as _sequence*/
! 	0,					/* tp_as _mapping*/
! 	0, 					/* tp_hash */
! 	0,					/* tp_call */
! 	0,					/* tp_str */
! 	0,					/* tp_getattro */
! 	0,					/* tp_setattro */
! 	&array_as_buffer,			/* tp_as _buffer*/
! 	0,					/* tp_flags */
! 	arraytype_doc,				/* tp_doc */
!  	0,					/* tp_traverse */
! 	0,					/* tp_clear */
! 	array_richcompare,			/* tp_richcompare */
  };