[Python-checkins] python/dist/src/Modules itertoolsmodule.c, 1.22, 1.23

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Sat Oct 25 02:37:49 EDT 2003


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

Modified Files:
	itertoolsmodule.c 
Log Message:
Improvements to coding for itertools.tee():

* Add error checking code to PyList_Append() call.

* Replace PyObject_CallMethod(to->outbasket, "pop", NULL) with equivalent
  in-line code.  Inlining is important here because the search for the
  pop method will occur for every element returned by the iterator.

* Make tee's dealloc() a little smarter.  If the trailing iterator is
  being deallocated, then the queue data is no longer needed and can
  be freed.



Index: itertoolsmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/itertoolsmodule.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -C2 -d -r1.22 -r1.23
*** itertoolsmodule.c	24 Oct 2003 08:45:23 -0000	1.22
--- itertoolsmodule.c	25 Oct 2003 06:37:47 -0000	1.23
***************
*** 47,50 ****
--- 47,51 ----
  	teeobject *to = lz->tee;
  	PyObject *result, *tmp;
+ 	int n;
  
  	if (lz->num_seen == to->num_seen) { 
***************
*** 54,58 ****
  			return NULL;
  		if (to->save_mode)
! 			PyList_Append(to->inbasket, result);
  		to->num_seen++;
  		lz->num_seen++;
--- 55,62 ----
  			return NULL;
  		if (to->save_mode)
! 			if(PyList_Append(to->inbasket, result) == -1){
! 				Py_DECREF(result);
! 				return NULL;
! 			}
  		to->num_seen++;
  		lz->num_seen++;
***************
*** 67,75 ****
  		to->inbasket = tmp;
  		PyList_Reverse(to->outbasket);
- 		assert(PyList_GET_SIZE(to->outbasket) > 0);
  	}
  
  	lz->num_seen++;
! 	return PyObject_CallMethod(to->outbasket, "pop", NULL);
  }
  
--- 71,87 ----
  		to->inbasket = tmp;
  		PyList_Reverse(to->outbasket);
  	}
  
+ 	/* Pop result from to->outbasket */
+ 	n = PyList_GET_SIZE(to->outbasket);
+ 	assert(n>0);
+ 	result = PyList_GET_ITEM(to->outbasket, n-1);
+ 	Py_INCREF(result);
+ 	if (PyList_SetSlice(to->outbasket, n-1, n, NULL) == -1) {
+ 		Py_DECREF(result);
+ 		return NULL;
+ 	}
  	lz->num_seen++;
! 	return result;
  }
  
***************
*** 77,82 ****
--- 89,105 ----
  ii_dealloc(iiobject *ii)
  {
+ 	teeobject *to = ii->tee;
+ 	int n;
+ 
  	PyObject_GC_UnTrack(ii);
  	ii->tee->save_mode = 0;  /* Stop saving data */
+ 	if (ii->num_seen < to->num_seen) {
+ 		/* It is the trailing iterator being freed; this means 
+ 		   that the stored queue data is no longer needed */
+ 		n = PyList_GET_SIZE(to->inbasket);
+ 		PyList_SetSlice(to->inbasket, 0, n, NULL);
+ 		n = PyList_GET_SIZE(to->outbasket);
+ 		PyList_SetSlice(to->outbasket, 0, n, NULL);
+ 	}
  	Py_XDECREF(ii->tee);
  	PyObject_GC_Del(ii);





More information about the Python-checkins mailing list