[pypy-svn] r61438 - pypy/trunk/pypy/rlib

afa at codespeak.net afa at codespeak.net
Thu Jan 29 00:48:17 CET 2009


Author: afa
Date: Thu Jan 29 00:48:15 2009
New Revision: 61438

Modified:
   pypy/trunk/pypy/rlib/rposix.py
   pypy/trunk/pypy/rlib/streamio.py
Log:
Implement streamio.truncate for windows.
this fixes at least test.test_file.


Modified: pypy/trunk/pypy/rlib/rposix.py
==============================================================================
--- pypy/trunk/pypy/rlib/rposix.py	(original)
+++ pypy/trunk/pypy/rlib/rposix.py	Thu Jan 29 00:48:15 2009
@@ -1,4 +1,4 @@
-import os
+import os, sys
 from pypy.rpython.lltypesystem.rffi import CConstant, CExternVariable, INT
 from pypy.rpython.lltypesystem import lltype, ll2ctypes, rffi
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
@@ -42,3 +42,24 @@
             os.close(fd)
         except OSError:
             pass
+
+# An implementation of ftruncate for Windows
+if sys.platform == 'win32':
+    from pypy.rlib import rwin32
+
+    eci = ExternalCompilationInfo()
+    _get_osfhandle = rffi.llexternal('_get_osfhandle', [rffi.INT], rffi.LONG,
+                                     compilation_info=eci)
+    SetEndOfFile = rffi.llexternal('SetEndOfFile', [rffi.LONG], rwin32.BOOL,
+                                   compilation_info=eci)
+
+    def ftruncate(fd, size):
+        # move to the position to be truncated
+        os.lseek(fd, size, 0)
+        # Truncate.  Note that this may grow the file!
+        handle = _get_osfhandle(fd)
+        if handle == -1:
+            raise IOError(get_errno(), "Invalid file handle")
+        if not SetEndOfFile(handle):
+            raise IOError("Could not truncate file")
+        return size

Modified: pypy/trunk/pypy/rlib/streamio.py
==============================================================================
--- pypy/trunk/pypy/rlib/streamio.py	(original)
+++ pypy/trunk/pypy/rlib/streamio.py	Thu Jan 29 00:48:15 2009
@@ -253,7 +253,8 @@
 
     if sys.platform == "win32":
         def truncate(self, size):
-            raise NotImplementedError
+            from pypy.rlib.rposix import ftruncate
+            ftruncate(self.fd, size)
     else:
         def truncate(self, size):
             os.ftruncate(self.fd, size)



More information about the Pypy-commit mailing list