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

Michael Hudson mwh@users.sourceforge.net
2002年3月16日 10:19:36 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv28149/Objects
Modified Files:
 Tag: release22-maint
	fileobject.c 
Log Message:
Backport Tim's work on getting file.truncate working better on Win32.
"cvs diff | patch" managed to stick the NEWS item in the 2.2 final
section! I wonder which silly man wrote patch <wink>.
Index: fileobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v
retrieving revision 2.141.6.1
retrieving revision 2.141.6.2
diff -C2 -d -r2.141.6.1 -r2.141.6.2
*** fileobject.c	12 Jan 2002 11:13:24 -0000	2.141.6.1
--- fileobject.c	16 Mar 2002 18:19:33 -0000	2.141.6.2
***************
*** 11,16 ****
 #ifdef MS_WIN32
 #define fileno _fileno
! /* can (almost fully) duplicate with _chsize, see file_truncate */
 #define HAVE_FTRUNCATE
 #endif
 
--- 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
 
***************
*** 376,379 ****
--- 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)
***************
*** 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
--- 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

AltStyle によって変換されたページ (->オリジナル) /