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

Michael Hudson mwh@users.sourceforge.net
Sun, 17 Mar 2002 07:55:53 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv20548/Objects

Modified Files:
      Tag: release22-maint
	fileobject.c 
Log Message:
Take Tim's work on file.truncate out of 2.2.1 again.



Index: fileobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v
retrieving revision 2.141.6.2
retrieving revision 2.141.6.3
diff -C2 -d -r2.141.6.2 -r2.141.6.3
*** fileobject.c	16 Mar 2002 18:19:33 -0000	2.141.6.2
--- fileobject.c	17 Mar 2002 15:55:50 -0000	2.141.6.3
***************
*** 11,18 ****
  #ifdef MS_WIN32
  #define fileno _fileno
! /* can simulate truncate with Win32 API functions; see file_truncate */
  #define HAVE_FTRUNCATE
- #define WINDOWS_LEAN_AND_MEAN
- #include <windows.h>
  #endif
  
--- 11,16 ----
  #ifdef MS_WIN32
  #define fileno _fileno
! /* can (almost fully) duplicate with _chsize, see file_truncate */
  #define HAVE_FTRUNCATE
  #endif
  
***************
*** 378,384 ****
  	if (!PyArg_ParseTuple(args, "|O:truncate", &newsizeobj))
  		return NULL;
- 
- 	/* Set newsize to current postion if newsizeobj NULL, else to the
- 	   specified value. */
  	if (newsizeobj != NULL) {
  #if !defined(HAVE_LARGEFILE_SUPPORT)
--- 376,379 ----
***************
*** 391,468 ****
  		if (PyErr_Occurred())
  			return NULL;
! 	}
! 	else {
! 		/* Default to current position. */
  		Py_BEGIN_ALLOW_THREADS
  		errno = 0;
  		newsize = _portable_ftell(f->f_fp);
  		Py_END_ALLOW_THREADS
! 		if (newsize == -1)
! 			goto onioerror;
  	}
- 
- 	/* Flush the file. */
  	Py_BEGIN_ALLOW_THREADS
  	errno = 0;
  	ret = fflush(f->f_fp);
  	Py_END_ALLOW_THREADS
! 	if (ret != 0)
! 		goto onioerror;
  
  #ifdef MS_WIN32
! 	/* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
! 	   so don't even try using it. */
! 	{
! 		Py_off_t current;	/* current file position */
! 		HANDLE hFile;
! 		int error;
! 
! 		/* current <- current file postion. */
! 		if (newsizeobj == NULL)
! 			current = newsize;
! 		else {
! 			Py_BEGIN_ALLOW_THREADS
! 			errno = 0;
! 			current = _portable_ftell(f->f_fp);
! 			Py_END_ALLOW_THREADS
! 			if (current == -1)
! 				goto onioerror;
! 		}
! 
! 		/* Move to newsize. */
! 		if (current != newsize) {
! 			Py_BEGIN_ALLOW_THREADS
! 			errno = 0;
! 			error = _portable_fseek(f->f_fp, newsize, SEEK_SET)
! 				!= 0;
! 			Py_END_ALLOW_THREADS
! 			if (error)
! 				goto onioerror;
! 		}
! 
! 		/* Truncate.  Note that this may grow the file! */
  		Py_BEGIN_ALLOW_THREADS
  		errno = 0;
! 		hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp));
! 		error = hFile == (HANDLE)-1;
! 		if (!error) {
! 			error = SetEndOfFile(hFile) == 0;
! 			if (error)
! 				errno = EACCES;
! 		}
  		Py_END_ALLOW_THREADS
! 		if (error)
! 			goto onioerror;
! 
! 		/* Restore original file position. */
! 		if (current != newsize) {
! 			Py_BEGIN_ALLOW_THREADS
! 			errno = 0;
! 			error = _portable_fseek(f->f_fp, current, SEEK_SET)
! 				!= 0;
! 			Py_END_ALLOW_THREADS
! 			if (error)
! 				goto onioerror;
! 		}
  	}
  #else
--- 386,420 ----
  		if (PyErr_Occurred())
  			return NULL;
! 	} else {
! 		/* Default to current position*/
  		Py_BEGIN_ALLOW_THREADS
  		errno = 0;
  		newsize = _portable_ftell(f->f_fp);
  		Py_END_ALLOW_THREADS
! 		if (newsize == -1) {
! 		        PyErr_SetFromErrno(PyExc_IOError);
! 			clearerr(f->f_fp);
! 			return NULL;
! 		}
  	}
  	Py_BEGIN_ALLOW_THREADS
  	errno = 0;
  	ret = fflush(f->f_fp);
  	Py_END_ALLOW_THREADS
! 	if (ret != 0) goto onioerror;
  
  #ifdef MS_WIN32
! 	/* can use _chsize; if, however, the newsize overflows 32-bits then
! 	   _chsize is *not* adequate; in this case, an OverflowError is raised */
! 	if (newsize > LONG_MAX) {
! 		PyErr_SetString(PyExc_OverflowError,
! 			"the new size is too long for _chsize (it is limited to 32-bit values)");
! 		return NULL;
! 	} else {
  		Py_BEGIN_ALLOW_THREADS
  		errno = 0;
! 		ret = _chsize(fileno(f->f_fp), (long)newsize);
  		Py_END_ALLOW_THREADS
! 		if (ret != 0) goto onioerror;
  	}
  #else