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

arigo at codespeak.net arigo at codespeak.net
Sun Apr 19 12:42:59 CEST 2009


Author: arigo
Date: Sun Apr 19 12:42:59 2009
New Revision: 64350

Modified:
   pypy/trunk/pypy/rlib/rposix.py
   pypy/trunk/pypy/rlib/streamio.py
Log:
Move the Windows-only rlib.rposix.ftruncate() to
rlib.streamio.ftruncate_win32(), which is its single
use point.  Fix it to not raise IOError, but StreamError.


Modified: pypy/trunk/pypy/rlib/rposix.py
==============================================================================
--- pypy/trunk/pypy/rlib/rposix.py	(original)
+++ pypy/trunk/pypy/rlib/rposix.py	Sun Apr 19 12:42:59 2009
@@ -1,4 +1,4 @@
-import os, sys
+import os
 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,24 +42,3 @@
             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	Sun Apr 19 12:42:59 2009
@@ -157,6 +157,28 @@
 StreamErrors = (OSError, StreamError)     # errors that can generally be raised
 
 
+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_win32(fd, size):
+        curpos = os.lseek(fd, 0, 1)
+        try:
+            # 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 StreamError("Invalid file handle")
+            if not SetEndOfFile(handle):
+                raise StreamError("Could not truncate file")
+        finally:
+            os.lseek(fd, curpos, 0)
+
+
 class Stream(object):
 
     """Base class for streams.  Provides a default implementation of
@@ -253,8 +275,7 @@
 
     if sys.platform == "win32":
         def truncate(self, size):
-            from pypy.rlib.rposix import ftruncate
-            ftruncate(self.fd, size)
+            ftruncate_win32(self.fd, size)
     else:
         def truncate(self, size):
             os.ftruncate(self.fd, size)



More information about the Pypy-commit mailing list