[Python-checkins] cpython: Issue #13964: Write tests for new os.*utime*() functions

victor.stinner python-checkins at python.org
Wed Feb 8 03:00:42 CET 2012


http://hg.python.org/cpython/rev/d297f9b10c64
changeset:   74824:d297f9b10c64
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Wed Feb 08 03:01:11 2012 +0100
summary:
  Issue #13964: Write tests for new os.*utime*() functions

files:
  Lib/test/test_os.py |  37 +++++++++++++++++++++++++++++++++
  1 files changed, 37 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -300,6 +300,43 @@
         st2 = os.stat(support.TESTFN)
         self.assertAlmostEqual(st1.st_mtime, st2.st_mtime, delta=10)
 
+    def test_utime_subsecond(self):
+        asec, amsec = 1, 901
+        atime = asec + amsec * 1e-3
+        msec, mmsec = 5, 901
+        mtime = msec + mmsec * 1e-3
+        filename = self.fname
+        dirname = os.path.dirname(filename)
+        for func in ('utime', 'futimes', 'futimens', 'lutimes', 'utimensat'):
+            if not hasattr(os, func):
+                continue
+            os.utime(filename, (0, 0))
+            if func == 'utime':
+                os.utime(filename, (atime, mtime))
+            elif func == 'futimes':
+                with open(filename, "wb") as f:
+                    os.futimes(f.fileno(), (atime, mtime))
+                os.utime(filename, (atime, mtime))
+            elif func == 'futimens':
+                with open(filename, "wb") as f:
+                    os.futimens(f.fileno(),
+                               (asec, amsec * 1000000),
+                               (msec, mmsec * 1000000))
+            elif func == 'lutimes':
+                os.lutimes(filename, (atime, mtime))
+            else:
+                dirfd = os.open(dirname, os.O_RDONLY)
+                try:
+                    os.utimensat(dirfd, os.path.basename(filename),
+                                 (asec, amsec * 1000000),
+                                 (msec, mmsec * 1000000))
+                finally:
+                    os.close(dirfd)
+            st = os.stat(filename)
+            self.assertAlmostEqual(st.st_atime, atime, places=3)
+            self.assertAlmostEqual(st.st_mtime, mtime, places=3)
+
+
     # Restrict test to Win32, since there is no guarantee other
     # systems support centiseconds
     if sys.platform == 'win32':

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list