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

M.-A. Lemburg python-dev@python.org
Fri, 25 Aug 2000 15:39:53 -0700


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

Modified Files:
	fileobject.c 
Log Message:
Fix to bug [ Bug #111860 ] file.writelines() crashes.
file.writelines() now tries to emulate the behaviour of file.write()
as closely as possible. Due to the problems with releasing the
interpreter lock the solution isn't exactly optimal, but still better
than not supporting the file.write() semantics at all.

Index: fileobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v
retrieving revision 2.83
retrieving revision 2.84
diff -C2 -r2.83 -r2.84
*** fileobject.c	2000/08/22 21:51:22	2.83
--- fileobject.c	2000/08/25 22:39:50	2.84
***************
*** 981,986 ****
  				if (line == NULL) {
  					if (PyErr_ExceptionMatches(
! 						PyExc_IndexError))
! 					{
  						PyErr_Clear();
  						break;
--- 981,985 ----
  				if (line == NULL) {
  					if (PyErr_ExceptionMatches(
! 						PyExc_IndexError)) {
  						PyErr_Clear();
  						break;
***************
*** 990,999 ****
  					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);
  			}
--- 989,992 ----
***************
*** 1002,1010 ****
  			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),
--- 995,1035 ----
  			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, v);
+ 			}
+ 		}
+ 
+ 		/* 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),