New submission from Christian Heimes:

The patch fixes for real what "Patch # 1323 by Amaury Forgeot d'Arc"
claims to have fixed. I reverted his patch to io.py and implemented the
fix in the Windows specific part of truncate in _fileio.c. It fixes two
tests for raw io on Windows.

----------
components: Library (Lib)
files: py3k_win_io.patch
messages: 56769
nosy: tiran
severity: normal
status: open
title: Fix truncate on Windows, this time for real
type: behavior
versions: Python 3.0
Added file: http://bugs.python.org/file8613/py3k_win_io.patch

__________________________________
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1330>
__________________________________
Index: Lib/io.py
===================================================================
--- Lib/io.py	(revision 58660)
+++ Lib/io.py	(working copy)
@@ -597,25 +597,10 @@
         return self.raw.tell()
 
     def truncate(self, pos=None):
-        # On Windows, the truncate operation changes the current position
-        # to the end of the file, which may leave us with desynchronized
-        # buffers.
-        # Since we promise that truncate() won't change the current position,
-        # the easiest thing is to capture current pos now and seek back to
-        # it at the end.
-
-        initialpos = self.tell()
         if pos is None:
-            pos = initialpos
+            pos = self.tell()
+        return self.raw.truncate(pos)
 
-        # Flush the stream.  We're mixing buffered I/O with lower-level I/O,
-        # and a flush may be necessary to synch both views of the current
-        # file state.
-        self.flush()
-        newpos = self.raw.truncate(pos)
-        self.seek(initialpos)
-        return newpos
-
     ### Flush and close ###
 
     def flush(self):
Index: Modules/_fileio.c
===================================================================
--- Modules/_fileio.c	(revision 58660)
+++ Modules/_fileio.c	(working copy)
@@ -628,14 +628,22 @@
 	   so don't even try using it. */
 	{
 		HANDLE hFile;
-		PyObject *pos2;
+		PyObject *pos2, *oldposobj;
 
+		/* store the current position */
+		oldposobj = portable_lseek(self->fd, NULL, 1);
+		if (oldposobj == NULL) {
+			Py_DECREF(posobj);
+			return NULL;
+		}
+
 		/* Have to move current pos to desired endpoint on Windows. */
 		errno = 0;
 		pos2 = portable_lseek(fd, posobj, SEEK_SET);
 		if (pos2 == NULL)
 		{
 			Py_DECREF(posobj);
+			Py_DECREF(oldposobj);
 			return NULL;
 		}
 		Py_DECREF(pos2);
@@ -651,6 +659,16 @@
 				errno = EACCES;
 		}
 		Py_END_ALLOW_THREADS
+		
+		/* Move to the previous position in the file */
+		pos2 = portable_lseek(fd, oldposobj, SEEK_SET);
+		if (pos2 == NULL) {
+			Py_DECREF(posobj);
+			Py_DECREF(oldposobj);
+			return NULL;
+		}
+		Py_DECREF(pos2);
+		Py_DECREF(oldposobj);
 	}
 #else
 	Py_BEGIN_ALLOW_THREADS
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to