[Python-3000-checkins] r58673 - in python/branches/py3k: Lib/io.py Modules/_fileio.c

guido.van.rossum python-3000-checkins at python.org
Fri Oct 26 19:19:34 CEST 2007


Author: guido.van.rossum
Date: Fri Oct 26 19:19:33 2007
New Revision: 58673

Modified:
   python/branches/py3k/Lib/io.py
   python/branches/py3k/Modules/_fileio.c
Log:
Patch 1330 by Christian Heimes (with some TLC applied by myself).
Move most of the messiness with truncate() on Windows into _fileio.c.
Still keep the flush() call in io.py though.


Modified: python/branches/py3k/Lib/io.py
==============================================================================
--- python/branches/py3k/Lib/io.py	(original)
+++ python/branches/py3k/Lib/io.py	Fri Oct 26 19:19:33 2007
@@ -597,24 +597,14 @@
         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
-
         # 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
+
+        if pos is None:
+            pos = self.tell()
+        return self.raw.truncate(pos)
 
     ### Flush and close ###
 

Modified: python/branches/py3k/Modules/_fileio.c
==============================================================================
--- python/branches/py3k/Modules/_fileio.c	(original)
+++ python/branches/py3k/Modules/_fileio.c	Fri Oct 26 19:19:33 2007
@@ -628,14 +628,21 @@
 	   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)
-		{
+		if (pos2 == NULL) {
 			Py_DECREF(posobj);
+			Py_DECREF(oldposobj);
 			return NULL;
 		}
 		Py_DECREF(pos2);
@@ -651,6 +658,18 @@
 				errno = EACCES;
 		}
 		Py_END_ALLOW_THREADS
+
+		if (ret == 0) {
+			/* 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


More information about the Python-3000-checkins mailing list