[Python-checkins] python/dist/src/Modules arraymodule.c,2.81,2.82

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Mon, 06 Jan 2003 17:59:28 -0800


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

Modified Files:
	arraymodule.c 
Log Message:
SF patch #662433: Fill arraymodule's tp_iter and sq_contains slots



Index: arraymodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/arraymodule.c,v
retrieving revision 2.81
retrieving revision 2.82
diff -C2 -d -r2.81 -r2.82
*** arraymodule.c	6 Jan 2003 12:41:25 -0000	2.81
--- arraymodule.c	7 Jan 2003 01:58:51 -0000	2.82
***************
*** 845,848 ****
--- 845,861 ----
  Return index of first occurence of x in the array.");
  
+ static int
+ array_contains(arrayobject *self, PyObject *v)
+ {
+ 	int i, cmp;
+ 
+ 	for (i = 0, cmp = 0 ; cmp == 0 && i < self->ob_size; i++) {
+ 		PyObject *selfi = getarrayitem((PyObject *)self, i);
+ 		cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
+ 		Py_DECREF(selfi);
+ 	}
+ 	return cmp;
+ }
+ 
  static PyObject *
  array_remove(arrayobject *self, PyObject *v)
***************
*** 1656,1660 ****
  	(intobjargproc)array_ass_item,		/*sq_ass_item*/
  	(intintobjargproc)array_ass_slice,	/*sq_ass_slice*/
! 	NULL,					/*sq_contains*/
  	(binaryfunc)array_inplace_concat,	/*sq_inplace_concat*/
  	(intargfunc)array_inplace_repeat	/*sq_inplace_repeat*/
--- 1669,1673 ----
  	(intobjargproc)array_ass_item,		/*sq_ass_item*/
  	(intintobjargproc)array_ass_slice,	/*sq_ass_slice*/
! 	(objobjproc)array_contains,		/*sq_contains*/
  	(binaryfunc)array_inplace_concat,	/*sq_inplace_concat*/
  	(intargfunc)array_inplace_repeat	/*sq_inplace_repeat*/
***************
*** 1823,1826 ****
--- 1836,1841 ----
  ");
  
+ static PyObject *array_iter(arrayobject *ao);
+ 
  static PyTypeObject Arraytype = {
  	PyObject_HEAD_INIT(NULL)
***************
*** 1850,1854 ****
  	array_richcompare,			/* tp_richcompare */
  	0,					/* tp_weaklistoffset */
! 	0,					/* tp_iter */
  	0,					/* tp_iternext */
  	array_methods,				/* tp_methods */
--- 1865,1869 ----
  	array_richcompare,			/* tp_richcompare */
  	0,					/* tp_weaklistoffset */
! 	(getiterfunc)array_iter,		/* tp_iter */
  	0,					/* tp_iternext */
  	array_methods,				/* tp_methods */
***************
*** 1865,1868 ****
--- 1880,1987 ----
  	PyObject_Del,				/* tp_free */
  };
+ 
+ 
+ /*********************** Array Iterator **************************/
+ 
+ typedef struct {
+ 	PyObject_HEAD
+ 	long			index;
+ 	arrayobject		*ao;
+ 	PyObject		* (*getitem)(struct arrayobject *, int);
+ } arrayiterobject;
+ 
+ static PyTypeObject PyArrayIter_Type;
+ 
+ #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type)
+ 
+ static PyObject *
+ array_iter(arrayobject *ao)
+ {
+ 	arrayiterobject *it;
+ 
+ 	if (!array_Check(ao)) {
+ 		PyErr_BadInternalCall();
+ 		return NULL;
+ 	}
+ 
+ 	it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
+ 	if (it == NULL)
+ 		return NULL;
+ 
+ 	Py_INCREF(ao);
+ 	it->ao = ao;
+ 	it->index = 0;
+ 	it->getitem = ao->ob_descr->getitem;
+ 	PyObject_GC_Track(it);
+ 	return (PyObject *)it;
+ }
+ 
+ static PyObject *
+ arrayiter_getiter(PyObject *it)
+ {
+ 	Py_INCREF(it);
+ 	return it;
+ }
+ 
+ static PyObject *
+ arrayiter_next(arrayiterobject *it)
+ {
+ 	assert(PyArrayIter_Check(it));
+ 	if (it->index < it->ao->ob_size)
+ 		return (*it->getitem)(it->ao, it->index++);
+ 	return NULL;
+ }
+ 
+ static void
+ arrayiter_dealloc(arrayiterobject *it)
+ {
+ 	PyObject_GC_UnTrack(it);
+ 	Py_XDECREF(it->ao);
+ 	PyObject_GC_Del(it);
+ }
+ 
+ static int
+ arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
+ {
+ 	if (it->ao != NULL)
+ 		return visit((PyObject *)(it->ao), arg);
+ 	return 0;
+ }
+ 
+ static PyTypeObject PyArrayIter_Type = {
+ 	PyObject_HEAD_INIT(&PyType_Type)
+ 	0,                                      /* ob_size */
+ 	"arrayiterator",                        /* tp_name */
+ 	sizeof(arrayiterobject),                /* tp_basicsize */
+ 	0,                                      /* tp_itemsize */
+ 	/* methods */
+ 	(destructor)arrayiter_dealloc,		/* tp_dealloc */
+ 	0,                                      /* tp_print */
+ 	0,                                      /* tp_getattr */
+ 	0,                                      /* tp_setattr */
+ 	0,                                      /* tp_compare */
+ 	0,                                      /* tp_repr */
+ 	0,                                      /* tp_as_number */
+ 	0,                                      /* tp_as_sequence */
+ 	0,                                      /* tp_as_mapping */
+ 	0,                                      /* tp_hash */
+ 	0,                                      /* tp_call */
+ 	0,                                      /* tp_str */
+ 	PyObject_GenericGetAttr,                /* tp_getattro */
+ 	0,                                      /* tp_setattro */
+ 	0,                                      /* tp_as_buffer */
+ 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 	0,                                      /* tp_doc */
+ 	(traverseproc)arrayiter_traverse,	/* tp_traverse */
+ 	0,					/* tp_clear */
+ 	0,                                      /* tp_richcompare */
+ 	0,                                      /* tp_weaklistoffset */
+ 	(getiterfunc)arrayiter_getiter,		/* tp_iter */
+ 	(iternextfunc)arrayiter_next,		/* tp_iternext */
+ 	0,					/* tp_methods */
+ };
+ 
+ 
+ /*********************** Install Module **************************/
  
  /* No functions in array module. */