[Python-checkins] python/dist/src/Modules arraymodule.c,2.94,2.95

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Sun Mar 14 00:44:01 EST 2004


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17368/Modules

Modified Files:
	arraymodule.c 
Log Message:
SF feature request #686323:  Minor array module enhancements

array.extend() now accepts iterable arguments implements as a series
of appends.  Besides being a user convenience and matching the behavior
for lists, this the saves memory and cycles that would be used to
create a temporary array object.



Index: arraymodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/arraymodule.c,v
retrieving revision 2.94
retrieving revision 2.95
diff -C2 -d -r2.94 -r2.95
*** arraymodule.c	14 Mar 2004 04:37:50 -0000	2.94
--- arraymodule.c	14 Mar 2004 05:43:59 -0000	2.95
***************
*** 776,789 ****
  
  static int
! array_do_extend(arrayobject *self, PyObject *bb)
  {
! 	int size;
  
! 	if (!array_Check(bb)) {
! 		PyErr_Format(PyExc_TypeError,
! 			"can only extend array with array (not \"%.200s\")",
! 			bb->ob_type->tp_name);
  		return -1;
  	}
  #define b ((arrayobject *)bb)
  	if (self->ob_descr != b->ob_descr) {
--- 776,808 ----
  
  static int
! array_iter_extend(arrayobject *self, PyObject *bb)
  {
! 	PyObject *it, *v;
  
! 	it = PyObject_GetIter(bb);
! 	if (it == NULL)
  		return -1;
+ 
+ 	while ((v = PyIter_Next(it)) != NULL) {
+ 		if (ins1(self, (int) self->ob_size, v) != 0) {
+ 			Py_DECREF(v);
+ 			Py_DECREF(it);
+ 			return -1;
+ 		}
+ 		Py_DECREF(v);
  	}
+ 	Py_DECREF(it);
+ 	if (PyErr_Occurred())
+ 		return -1;
+ 	return 0;
+ }
+ 
+ static int
+ array_do_extend(arrayobject *self, PyObject *bb)
+ {
+ 	int size;
+ 
+ 	if (!array_Check(bb))
+ 		return array_iter_extend(self, bb);
  #define b ((arrayobject *)bb)
  	if (self->ob_descr != b->ob_descr) {
***************
*** 811,814 ****
--- 830,839 ----
  array_inplace_concat(arrayobject *self, PyObject *bb)
  {
+ 	if (!array_Check(bb)) {
+ 		PyErr_Format(PyExc_TypeError,
+ 			"can only extend array with array (not \"%.200s\")",
+ 			bb->ob_type->tp_name);
+ 		return NULL;
+ 	}
  	if (array_do_extend(self, bb) == -1)
  		return NULL;
***************
*** 991,997 ****
  
  PyDoc_STRVAR(extend_doc,
! "extend(array)\n\
  \n\
!  Append array items to the end of the array.");
  
  static PyObject *
--- 1016,1022 ----
  
  PyDoc_STRVAR(extend_doc,
! "extend(array or iterable)\n\
  \n\
!  Append items to the end of the array.");
  
  static PyObject *
***************
*** 1882,1886 ****
  byteswap() -- byteswap all the items of the array\n\
  count() -- return number of occurences of an object\n\
! extend() -- extend array by appending array elements\n\
  fromfile() -- read items from a file object\n\
  fromlist() -- append items from the list\n\
--- 1907,1911 ----
  byteswap() -- byteswap all the items of the array\n\
  count() -- return number of occurences of an object\n\
! extend() -- extend array by appending multiple elements from an iterable\n\
  fromfile() -- read items from a file object\n\
  fromlist() -- append items from the list\n\




More information about the Python-checkins mailing list