[pypy-svn] r43775 - in pypy/branch/kill-ctypes/pypy/module/posix: . test

fijal at codespeak.net fijal at codespeak.net
Mon May 28 12:53:24 CEST 2007


Author: fijal
Date: Mon May 28 12:53:18 2007
New Revision: 43775

Modified:
   pypy/branch/kill-ctypes/pypy/module/posix/__init__.py
   pypy/branch/kill-ctypes/pypy/module/posix/interp_posix.py
   pypy/branch/kill-ctypes/pypy/module/posix/test/test_posix2.py
Log:
Add os.utime


Modified: pypy/branch/kill-ctypes/pypy/module/posix/__init__.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/module/posix/__init__.py	(original)
+++ pypy/branch/kill-ctypes/pypy/module/posix/__init__.py	Mon May 28 12:53:18 2007
@@ -51,6 +51,7 @@
     '_exit'     : 'interp_posix._exit',
     #'getuid'    : 'interp_posix.getuid',
     #'geteuid'   : 'interp_posix.geteuid',
+    'utime'     : 'interp_posix.utime',
     }
     if hasattr(os, 'ftruncate'):
         interpleveldefs['ftruncate'] = 'interp_posix.ftruncate'

Modified: pypy/branch/kill-ctypes/pypy/module/posix/interp_posix.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/module/posix/interp_posix.py	(original)
+++ pypy/branch/kill-ctypes/pypy/module/posix/interp_posix.py	Mon May 28 12:53:18 2007
@@ -481,11 +481,31 @@
     return space.newtuple([space.wrap(ob) for ob in result])
 uname.unwrap_spec = [ObjSpace]
 
-#def utime(space, path, w_tuple):
-#    """ utime(path, (atime, mtime))
-#utime(path, None)
-#
-#Set the access and modified time of the file to the given values.  If the
-#second form is used, set the access and modified times to the current time.
-#    """
-#utime.unwrap_spec = [ObjSpace, str, W_Root]
+def utime(space, path, w_tuple):
+    """ utime(path, (atime, mtime))
+utime(path, None)
+
+Set the access and modified time of the file to the given values.  If the
+second form is used, set the access and modified times to the current time.
+    """
+    if space.is_w(w_tuple, space.w_None):
+        try:
+            ros.utime_null(path)
+            return
+        except OSError, e:
+            raise wrap_oserror(space, e)
+    try:
+        msg = "utime() arg 2 must be a tuple (atime, mtime) or None"
+        args_w = space.unpackiterable(w_tuple)
+        if len(args_w) != 2:
+            raise OperationError(space.w_TypeError, space.wrap(msg))
+        actime = space.int_w(args_w[0])
+        modtime = space.int_w(args_w[1])
+        ros.utime_tuple(path, (actime, modtime))
+    except OSError, e:
+        raise wrap_oserror(space, e)
+    except OperationError, e:
+        if not e.match(space, space.w_TypeError):
+            raise
+        raise OperationError(space.w_TypeError, space.wrap(msg))
+utime.unwrap_spec = [ObjSpace, str, W_Root]

Modified: pypy/branch/kill-ctypes/pypy/module/posix/test/test_posix2.py
==============================================================================
--- pypy/branch/kill-ctypes/pypy/module/posix/test/test_posix2.py	(original)
+++ pypy/branch/kill-ctypes/pypy/module/posix/test/test_posix2.py	Mon May 28 12:53:18 2007
@@ -171,6 +171,27 @@
             stream = os.popen('echo 1')
             assert stream.read() == '1\n'
 
+    def test_utime(self):
+        os = self.posix
+        import os.path
+        # XXX utimes & float support
+        path = os.path.join(self.pdir, "test_utime.txt")
+        fh = open(path, "w")
+        fh.write("x")
+        fh.close()
+        from time import time, sleep
+        t0 = time()
+        sleep(1)
+        os.utime(path, None)
+        assert os.stat(path).st_atime > t0
+        os.utime(path, (int(t0), int(t0)))
+        assert int(os.stat(path).st_atime) == int(t0)
+
+    def test_utime_raises(self):
+        os = self.posix
+        raises(TypeError, "os.utime('xxx', 3)")
+        raises(OSError, "os.utime('somefilewhichihopewouldneverappearhere', None)")
+
 class AppTestEnvironment(object):
     def setup_class(cls): 
         cls.space = space 



More information about the Pypy-commit mailing list