[pypy-svn] r46280 - in pypy/branch/pypy-more-rtti-inprogress: rpython/module translator/c/test

arigo at codespeak.net arigo at codespeak.net
Mon Sep 3 17:02:37 CEST 2007


Author: arigo
Date: Mon Sep  3 17:02:35 2007
New Revision: 46280

Modified:
   pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os.py
   pypy/branch/pypy-more-rtti-inprogress/translator/c/test/test_extfunc.py
Log:
os.utime():
* Windows support
* sub-second resolution support (not for Windows)


Modified: pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os.py	(original)
+++ pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os.py	Mon Sep  3 17:02:35 2007
@@ -19,6 +19,12 @@
 from pypy.rpython.tool import rffi_platform as platform
 posix = __import__(os.name)
 
+if sys.platform.startswith('win'):
+    underscore_on_windows = '_'
+else:
+    underscore_on_windows = ''
+
+
 class CConfig:
     """
     Definitions for platform integration.
@@ -32,6 +38,7 @@
     """
     _includes_ = []
     if not sys.platform.startswith('win'):
+        # XXX many of these includes are not portable at all
         _includes_ += ['dirent.h', 'sys/stat.h',
                        'sys/times.h', 'utime.h', 'sys/types.h', 'unistd.h',
                        'signal.h', 'sys/wait.h']
@@ -43,15 +50,17 @@
                            ('tms_stime', rffi.INT),
                            ('tms_cutime', rffi.INT),
                            ('tms_cstime', rffi.INT)])
+    else:
+        _includes_ += ['sys/utime.h']
 
     SEEK_SET = platform.DefinedConstantInteger('SEEK_SET')
     SEEK_CUR = platform.DefinedConstantInteger('SEEK_CUR')
     SEEK_END = platform.DefinedConstantInteger('SEEK_END')
 
-if sys.platform.startswith('win'):
-    underscore_on_windows = '_'
-else:
-    underscore_on_windows = ''
+    HAVE_UTIMES = platform.Has('utimes')
+    UTIMBUF     = platform.Struct('struct '+underscore_on_windows+'utimbuf',
+                                  [('actime', rffi.INT),
+                                   ('modtime', rffi.INT)])
 
 
 class RegisterOs(BaseLazyRegistering):
@@ -133,29 +142,49 @@
 
     @registering(os.utime)
     def register_os_utime(self):
-        TIME_T = rffi.INT    # XXX do the right thing
-        UTIMEBUFP = rffi.CStructPtr('utimbuf', ('actime', TIME_T),
-                                    ('modtime', TIME_T))
+        UTIMBUFP = lltype.Ptr(self.UTIMBUF)
+        os_utime = self.llexternal('utime', [rffi.CCHARP, UTIMBUFP], rffi.INT)
 
-        # XXX sys/types.h is not portable at all
-        os_utime = self.llexternal('utime', [rffi.CCHARP, UTIMEBUFP], rffi.INT)
+        if self.HAVE_UTIMES:
+            class CConfig:
+                TIMEVAL = platform.Struct('struct timeval', [('tv_sec', rffi.LONG),
+                                                             ('tv_usec', rffi.LONG)])
+            config = platform.configure(CConfig)
+            TIMEVAL = config['TIMEVAL']
+            TIMEVAL2P = lltype.Ptr(lltype.FixedSizeArray(TIMEVAL, 2))
+            os_utimes = self.llexternal('utimes', [rffi.CCHARP, TIMEVAL2P], rffi.INT)
+
+            def os_utime_platform(path, actime, modtime):
+                import math
+                l_times = lltype.malloc(TIMEVAL2P.TO, flavor='raw')
+                fracpart, intpart = math.modf(actime)
+                l_times[0].c_tv_sec = int(intpart)
+                l_times[0].c_tv_usec = int(fracpart * 1E6)
+                fracpart, intpart = math.modf(modtime)
+                l_times[1].c_tv_sec = int(intpart)
+                l_times[1].c_tv_usec = int(fracpart * 1E6)
+                error = os_utimes(path, l_times)
+                lltype.free(l_times, flavor='raw')
+                return error
+        else:
+            # we only have utime(), which does not allow sub-second resolution
+            def os_utime_platform(path, actime, modtime):
+                l_utimbuf = lltype.malloc(UTIMBUFP.TO, flavor='raw')
+                l_utimbuf.c_actime  = int(actime)
+                l_utimbuf.c_modtime = int(modtime)
+                error = os_utime(path, l_utimbuf)
+                lltype.free(l_utimbuf, flavor='raw')
+                return error
 
         def os_utime_llimpl(path, tp):
-            # XXX right now they're all ints, might change in future
-            # XXX does not use utimes, even when available
             # NB. this function is specialized; we get one version where
             # tp is known to be None, and one version where it is known
             # to be a tuple of 2 floats.
             if tp is None:
-                l_utimebuf = lltype.nullptr(UTIMEBUFP.TO)
+                error = os_utime(path, lltype.nullptr(UTIMBUFP.TO))
             else:
-                l_utimebuf = lltype.malloc(UTIMEBUFP.TO, flavor='raw')
                 actime, modtime = tp
-                l_utimebuf.c_actime  = int(actime)
-                l_utimebuf.c_modtime = int(modtime)
-            error = os_utime(path, l_utimebuf)
-            if tp is not None:
-                lltype.free(l_utimebuf, flavor='raw')
+                error = os_utime_platform(path, actime, modtime)
             if error == -1:
                 raise OSError(rffi.get_errno(), "os_utime failed")
         os_utime_llimpl._annspecialcase_ = 'specialize:argtype(1)'

Modified: pypy/branch/pypy-more-rtti-inprogress/translator/c/test/test_extfunc.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/translator/c/test/test_extfunc.py	(original)
+++ pypy/branch/pypy-more-rtti-inprogress/translator/c/test/test_extfunc.py	Mon Sep  3 17:02:35 2007
@@ -902,7 +902,6 @@
         assert open(filename).read() == "2"
 
 def test_utime():
-    # XXX utimes & float support
     path = str(udir.ensure("test_utime.txt"))
     from time import time, sleep
     t0 = time()



More information about the Pypy-commit mailing list