[Python-checkins] python/dist/src/Modules cStringIO.c,2.46,2.47

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Mon Mar 8 13:17:33 EST 2004


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

Modified Files:
	cStringIO.c 
Log Message:
SF patch #907403:  Improvements to cStringIO.writelines()

The writelines() method now accepts any iterable argument and writes
the lines one at a time rather than using ''.join(lines) followed by
a single write.  Results in considerable memory savings and makes
the method suitable for use with generator expressions.



Index: cStringIO.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/cStringIO.c,v
retrieving revision 2.46
retrieving revision 2.47
diff -C2 -d -r2.46 -r2.47
*** cStringIO.c	27 Feb 2004 10:30:49 -0000	2.46
--- cStringIO.c	8 Mar 2004 18:17:31 -0000	2.47
***************
*** 417,454 ****
  }
  
- 
  PyDoc_STRVAR(O_writelines__doc__,
! "writelines(sequence_of_strings): write each string");
  static PyObject *
  O_writelines(Oobject *self, PyObject *args) {
!         PyObject *tmp = 0;
! 	static PyObject *joiner = NULL;
! 
! 	if (!joiner) {
! 		PyObject *empty_string = PyString_FromString("");
! 		if (empty_string == NULL)
  			return NULL;
! 		joiner = PyObject_GetAttrString(empty_string, "join");
! 		Py_DECREF(empty_string);
! 		if (joiner == NULL)
  			return NULL;
  	}
! 
!         if (PyObject_Size(args) < 0) return NULL;
! 
! 	args = PyTuple_Pack(1, args);
! 	if (args == NULL)
! 		return NULL;
! 	tmp = PyObject_Call(joiner, args, NULL);
! 	Py_DECREF(args);
!         UNLESS (tmp) return NULL;
! 
!         args = PyTuple_Pack(1, tmp);
!         Py_DECREF(tmp);
!         UNLESS (args) return NULL;
! 
!         tmp = O_write(self, args);
!         Py_DECREF(args);
!         return tmp;
  }
  
--- 417,449 ----
  }
  
  PyDoc_STRVAR(O_writelines__doc__,
! "writelines(sequence_of_strings) -> None.  Write the strings to the file.\n"
! "\n"
! "Note that newlines are not added.  The sequence can be any iterable object\n"
! "producing strings. This is equivalent to calling write() for each string.");
  static PyObject *
  O_writelines(Oobject *self, PyObject *args) {
! 	PyObject *it, *s;
! 	
! 	it = PyObject_GetIter(args);
! 	if (it == NULL)
! 		return NULL;
! 	while ((s = PyIter_Next(it)) != NULL) {
! 		int n;
! 		char *c;
! 		if (PyString_AsStringAndSize(s, &c, &n) == -1) {
! 			Py_DECREF(it);
! 			Py_DECREF(s);
  			return NULL;
! 		}
! 		if (O_cwrite((PyObject *)self, c, n) == -1) {
! 			Py_DECREF(it);
! 			Py_DECREF(s);
  			return NULL;
+ 		}
+ 		Py_DECREF(s);
  	}
! 	Py_DECREF(it);
! 	Py_RETURN_NONE;
  }
  




More information about the Python-checkins mailing list