[Python-checkins] python/dist/src/Objects abstract.c, 2.123, 2.124 listobject.c, 2.191, 2.192

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Thu Mar 11 04:13:14 EST 2004


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

Modified Files:
	abstract.c listobject.c 
Log Message:
Eliminate a big block of duplicate code in PySequence_List() by 
exposing _PyList_Extend().



Index: abstract.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/abstract.c,v
retrieving revision 2.123
retrieving revision 2.124
diff -C2 -d -r2.123 -r2.124
*** abstract.c	11 Jan 2004 23:26:51 -0000	2.123
--- abstract.c	11 Mar 2004 09:13:12 -0000	2.124
***************
*** 1428,1494 ****
  PySequence_List(PyObject *v)
  {
- 	PyObject *it;      /* iter(v) */
  	PyObject *result;  /* result list */
! 	int n;		   /* guess for result list size */
! 	int i;
  
  	if (v == NULL)
  		return null_error();
  
! 	/* Special-case list(a_list), for speed. */
! 	if (PyList_Check(v))
! 		return PyList_GetSlice(v, 0, PyList_GET_SIZE(v));
! 
! 	/* Get iterator.  There may be some low-level efficiency to be gained
! 	 * by caching the tp_iternext slot instead of using PyIter_Next()
! 	 * later, but premature optimization is the root etc.
! 	 */
! 	it = PyObject_GetIter(v);
! 	if (it == NULL)
  		return NULL;
  
! 	/* Guess a result list size. */
! 	n = PyObject_Size(v);
! 	if (n < 0) {
! 		PyErr_Clear();
! 		n = 8;	/* arbitrary */
! 	}
! 	result = PyList_New(n);
! 	if (result == NULL) {
! 		Py_DECREF(it);
  		return NULL;
  	}
- 
- 	/* Run iterator to exhaustion. */
- 	for (i = 0; ; i++) {
- 		PyObject *item = PyIter_Next(it);
- 		if (item == NULL) {
- 			if (PyErr_Occurred()) {
- 				Py_DECREF(result);
- 				result = NULL;
- 			}
- 			break;
- 		}
- 		if (i < n)
- 			PyList_SET_ITEM(result, i, item); /* steals ref */
- 		else {
- 			int status = PyList_Append(result, item);
- 			Py_DECREF(item);  /* append creates a new ref */
- 			if (status < 0) {
- 				Py_DECREF(result);
- 				result = NULL;
- 				break;
- 			}
- 		}
- 	}
- 
- 	/* Cut back result list if initial guess was too large. */
- 	if (i < n && result != NULL) {
- 		if (PyList_SetSlice(result, i, n, (PyObject *)NULL) != 0) {
- 			Py_DECREF(result);
- 			result = NULL;
- 		}
- 	}
- 	Py_DECREF(it);
  	return result;
  }
--- 1428,1446 ----
  PySequence_List(PyObject *v)
  {
  	PyObject *result;  /* result list */
! 	PyObject *rv;      /* return value from PyList_Extend */
  
  	if (v == NULL)
  		return null_error();
  
! 	result = PyList_New(0);
! 	if (result == NULL)
  		return NULL;
  
! 	rv = _PyList_Extend((PyListObject *)result, v);
! 	if (rv == NULL) {
! 		Py_DECREF(result);
  		return NULL;
  	}
  	return result;
  }

Index: listobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/listobject.c,v
retrieving revision 2.191
retrieving revision 2.192
diff -C2 -d -r2.191 -r2.192
*** listobject.c	11 Mar 2004 07:34:19 -0000	2.191
--- listobject.c	11 Mar 2004 09:13:12 -0000	2.192
***************
*** 777,780 ****
--- 777,786 ----
  }
  
+ PyObject *
+ _PyList_Extend(PyListObject *self, PyObject *b)
+ {
+ 	return listextend(self, b);
+ }
+ 
  static PyObject *
  list_inplace_concat(PyListObject *self, PyObject *other)




More information about the Python-checkins mailing list