Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b4e43e

Browse files
committed
_portable_fseek():
Subtlety on Windows: if we change test_largefile.py to use a file > 4GB, it still fails. A debug session suggests this is because fseek(fp, 0, 2) refuses to seek to the end of the file when the file is > 4GB, because it uses the SetFilePointer() in 32-bit mode. But it only fails when we seek relative to the end of the file, because in the other seek modes only calls to fgetpos() and fsetpos() are made, which use Get/SetFilePointer() in 64-bit mode. Solution: #ifdef MS_WInDOWS, replace the call to fseek(fp, ...) with a call to _lseeki64(fileno(fp), ...). Make sure to call fflush(fp) first. (XXX Could also replace the entire branch with a call to _lseeki64(). Would that be more efficient? Certainly less generated code.) (XXX This needs more testing. I can't actually test that it works for files >4GB on my Win98 machine, because the filesystem here won't let me create files >=4GB at all. Tim should test this on his Win2K machine.)
1 parent c4c062f commit 8b4e43e

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

Objects/fileobject.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,14 @@ _portable_fseek(FILE *fp, Py_off_t offset, int whence)
239239
fpos_t pos;
240240
switch (whence) {
241241
case SEEK_END:
242+
#ifdef MS_WINDOWS
243+
fflush(fp);
244+
if (_lseeki64(fileno(fp), 0, 2) == -1)
245+
return -1;
246+
#else
242247
if (fseek(fp, 0, SEEK_END) != 0)
243248
return -1;
249+
#endif
244250
/* fall through */
245251
case SEEK_CUR:
246252
if (fgetpos(fp, &pos) != 0)

0 commit comments

Comments
 (0)