[Python-checkins] python/dist/src/Objects fileobject.c,2.141.6.7,2.141.6.8
tim_one@users.sourceforge.net
tim_one@users.sourceforge.net
Wed, 30 Apr 2003 12:25:03 -0700
Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1:/tmp/cvs-serv4120/Objects
Modified Files:
Tag: release22-maint
fileobject.c
Log Message:
file_truncate(): Backported 2.3 code so that file.truncate(n) works on
Windows when n is too big to fit in a 32-bit int. This was a hole in
2.2's large file support on Windows, and turns out it's a bad hole at
least for ZODB.
Index: fileobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v
retrieving revision 2.141.6.7
retrieving revision 2.141.6.8
diff -C2 -d -r2.141.6.7 -r2.141.6.8
*** fileobject.c 4 Mar 2003 00:50:24 -0000 2.141.6.7
--- fileobject.c 30 Apr 2003 19:24:59 -0000 2.141.6.8
***************
*** 9,16 ****
#endif /* DONT_HAVE_SYS_TYPES_H */
! #ifdef MS_WIN32
#define fileno _fileno
! /* can (almost fully) duplicate with _chsize, see file_truncate */
#define HAVE_FTRUNCATE
#endif
--- 9,18 ----
#endif /* DONT_HAVE_SYS_TYPES_H */
! #ifdef MS_WINDOWS
#define fileno _fileno
! /* can simulate truncate with Win32 API functions; see file_truncate */
#define HAVE_FTRUNCATE
+ #define WIN32_LEAN_AND_MEAN
+ #include <windows.h>
#endif
***************
*** 389,392 ****
--- 391,397 ----
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)
***************
*** 399,403 ****
if (PyErr_Occurred())
return NULL;
! } else {
/* Default to current position*/
Py_BEGIN_ALLOW_THREADS
--- 404,409 ----
if (PyErr_Occurred())
return NULL;
! }
! else {
/* Default to current position*/
Py_BEGIN_ALLOW_THREADS
***************
*** 405,433 ****
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
--- 411,481 ----
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_WINDOWS
! /* 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
***************
*** 437,441 ****
Py_END_ALLOW_THREADS
if (ret != 0) goto onioerror;
! #endif /* !MS_WIN32 */
Py_INCREF(Py_None);
--- 485,489 ----
Py_END_ALLOW_THREADS
if (ret != 0) goto onioerror;
! #endif /* !MS_WINDOWS */
Py_INCREF(Py_None);