[Python-checkins] CVS: python/dist/src/Objects fileobject.c,2.73.2.2,2.73.2.3

Guido van Rossum python-dev@python.org
Sat, 26 Aug 2000 03:49:01 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory slayer.i.sourceforge.net:/tmp/cvs-serv18792

Modified Files:
      Tag: cnri-16-start
	fileobject.c 
Log Message:
Apply Marc-Andre Lemburg's 2.0b1 fix for writelines() here -- lists of
non-strings weren't always caught in time to prevent dumping core.


Index: fileobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v
retrieving revision 2.73.2.2
retrieving revision 2.73.2.3
diff -C2 -r2.73.2.2 -r2.73.2.3
*** fileobject.c	2000/08/10 21:19:39	2.73.2.2
--- fileobject.c	2000/08/26 10:48:58	2.73.2.3
***************
*** 900,905 ****
  				if (line == NULL) {
  					if (PyErr_ExceptionMatches(
! 						PyExc_IndexError))
! 					{
  						PyErr_Clear();
  						break;
--- 900,904 ----
  				if (line == NULL) {
  					if (PyErr_ExceptionMatches(
! 						PyExc_IndexError)) {
  						PyErr_Clear();
  						break;
***************
*** 909,918 ****
  					goto error;
  				}
- 				if (!PyString_Check(line)) {
- 					Py_DECREF(line);
- 					PyErr_SetString(PyExc_TypeError,
- 				 "writelines() requires sequences of strings");
- 					goto error;
- 				}
  				PyList_SetItem(list, j, line);
  			}
--- 908,911 ----
***************
*** 921,929 ****
  			break;
  
  		Py_BEGIN_ALLOW_THREADS
  		f->f_softspace = 0;
  		errno = 0;
  		for (i = 0; i < j; i++) {
! 			line = PyList_GET_ITEM(list, i);
  			len = PyString_GET_SIZE(line);
  			nwritten = fwrite(PyString_AS_STRING(line),
--- 914,954 ----
  			break;
  
+ 		/* Check that all entries are indeed strings. If not,
+ 		   apply the same rules as for file.write() and
+ 		   convert the results to strings. This is slow, but
+ 		   seems to be the only way since all conversion APIs
+ 		   could potentially execute Python code. */
+ 		for (i = 0; i < j; i++) {
+ 			PyObject *v = PyList_GET_ITEM(list, i);
+ 			if (!PyString_Check(v)) {
+ 			    	const char *buffer;
+ 			    	int len;
+ 				if (((f->f_binary && 
+ 				      PyObject_AsReadBuffer(v,
+ 					      (const void**)&buffer,
+ 							    &len)) ||
+ 				     PyObject_AsCharBuffer(v,
+ 							   &buffer,
+ 							   &len))) {
+ 					PyErr_SetString(PyExc_TypeError,
+ 				"writelines() requires sequences of strings");
+ 					goto error;
+ 				}
+ 				line = PyString_FromStringAndSize(buffer,
+ 								  len);
+ 				if (line == NULL)
+ 					goto error;
+ 				Py_DECREF(v);
+ 				PyList_SET_ITEM(list, i, line);
+ 			}
+ 		}
+ 
+ 		/* Since we are releasing the global lock, the
+ 		   following code may *not* execute Python code. */
  		Py_BEGIN_ALLOW_THREADS
  		f->f_softspace = 0;
  		errno = 0;
  		for (i = 0; i < j; i++) {
! 		    	line = PyList_GET_ITEM(list, i);
  			len = PyString_GET_SIZE(line);
  			nwritten = fwrite(PyString_AS_STRING(line),