diff -r 3156dd82df2d Lib/test/test_os.py --- a/Lib/test/test_os.py Wed Jun 10 15:44:18 2015 -0700 +++ b/Lib/test/test_os.py Fri Jun 12 00:51:26 2015 +0200 @@ -418,19 +418,27 @@ class StatAttributeTests(unittest.TestCa self._test_utime(self.fname, getattr, utime, 10) self._test_utime(support.TESTFN, getattr, utime, 10) - - def _test_utime_ns(self, set_times_ns, test_dir=True): - def getattr_ns(o, attr): - return getattr(o, attr + "_ns") - ten_s = 10 * 1000 * 1000 * 1000 - self._test_utime(self.fname, getattr_ns, set_times_ns, ten_s) - if test_dir: - self._test_utime(support.TESTFN, getattr_ns, set_times_ns, ten_s) + def _test_utime_ns(self, utime): + # Use positive timestamps with a resolution of 1 microsecond smaller + # than 2^24 seconds to avoid rounding issues. + # + # The C internal function used by os.utime() may only support a + # resolution of 1 us, so use a timestamp of 1 us. Writing a portable + # test with a resolution of 1 ns requires more work: see the issue + # #15745. + atime_ns = 1002003000 # 1.002003 seconds + mtime_ns = 4005006000 # 4.005006 seconds + utime(self.fname, atime_ns, mtime_ns) + st = os.stat(self.fname) + self.assertEqual(st.st_atime_ns, atime_ns) + self.assertEqual(st.st_mtime_ns, mtime_ns) def test_utime_ns(self): - def utime_ns(file, times): - return os.utime(file, ns=times) - self._test_utime_ns(utime_ns) + def utime(filename, atime_ns, mtime_ns): + # test utimensat(timespec), utimes(timeval), utime(utimbuf) + # or utime(time_t) + os.utime(filename, ns=(atime_ns, mtime_ns)) + self._test_utime_ns(utime) requires_utime_dir_fd = unittest.skipUnless( os.utime in os.supports_dir_fd, @@ -443,17 +451,22 @@ class StatAttributeTests(unittest.TestCa "follow_symlinks support for utime required for this test.") @requires_utime_nofollow_symlinks - def test_lutimes_ns(self): - def lutimes_ns(file, times): - return os.utime(file, ns=times, follow_symlinks=False) - self._test_utime_ns(lutimes_ns) + def test_utime_nofollow_symlinks(self): + def utime(filename, atime_ns, mtime_ns): + # use follow_symlinks=False to test utimensat(timespec) + # or lutimes(timeval) + os.utime(filename, ns=(atime_ns, mtime_ns), + follow_symlinks=False) + self._test_utime_ns(utime) @requires_utime_fd def test_futimes_ns(self): - def futimes_ns(file, times): - with open(file, "wb") as f: - os.utime(f.fileno(), ns=times) - self._test_utime_ns(futimes_ns, test_dir=False) + def utime(filename, atime_ns, mtime_ns): + with open(filename, 'wb') as fp: + # use a file descriptor to test futimens(timespec) + # or futimes(timeval) + os.utime(fp.fileno(), ns=(atime_ns, mtime_ns)) + self._test_utime_ns(utime) def _utime_invalid_arguments(self, name, arg): with self.assertRaises(ValueError):