Skip to content

Commit 6f8f97f

Browse files
gh-154427: Check the access time in UtimeTests only if it is stored (GH-154428)
HAMMER2 on DragonFly BSD does not store the access time and os.stat() returns the modification time instead. Probe the file system, like it is already done for the timestamp resolution. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 458367e commit 6f8f97f

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

Lib/test/test_os/test_os.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,11 +1058,18 @@ def support_subsecond(self, filename):
10581058
or (st.st_mtime != st[8])
10591059
or (st.st_ctime != st[9]))
10601060

1061+
def support_atime(self, filename):
1062+
# Heuristic to check if the filesystem stores the access time.
1063+
# Use whole seconds, to not depend on the timestamp resolution.
1064+
os.utime(filename, (1, 2))
1065+
return os.stat(filename).st_atime == 1
1066+
10611067
def _test_utime(self, set_time, filename=None):
10621068
if not filename:
10631069
filename = self.fname
10641070

10651071
support_subsecond = self.support_subsecond(filename)
1072+
support_atime = self.support_atime(filename)
10661073
if support_subsecond:
10671074
# Timestamp with a resolution of 1 microsecond (10^-6).
10681075
#
@@ -1089,18 +1096,22 @@ def _test_utime(self, set_time, filename=None):
10891096
# digits worth of sub-second precision.
10901097
# Some day it would be good to fix this upstream.
10911098
delta=1e-5
1092-
self.assertAlmostEqual(st.st_atime, atime_ns * 1e-9, delta=1e-5)
1099+
if support_atime:
1100+
self.assertAlmostEqual(st.st_atime, atime_ns * 1e-9, delta=1e-5)
1101+
self.assertAlmostEqual(st.st_atime_ns, atime_ns, delta=1e9 * 1e-5)
10931102
self.assertAlmostEqual(st.st_mtime, mtime_ns * 1e-9, delta=1e-5)
1094-
self.assertAlmostEqual(st.st_atime_ns, atime_ns, delta=1e9 * 1e-5)
10951103
self.assertAlmostEqual(st.st_mtime_ns, mtime_ns, delta=1e9 * 1e-5)
10961104
else:
10971105
if support_subsecond:
1098-
self.assertAlmostEqual(st.st_atime, atime_ns * 1e-9, delta=1e-6)
1106+
if support_atime:
1107+
self.assertAlmostEqual(st.st_atime, atime_ns * 1e-9, delta=1e-6)
10991108
self.assertAlmostEqual(st.st_mtime, mtime_ns * 1e-9, delta=1e-6)
11001109
else:
1101-
self.assertEqual(st.st_atime, atime_ns * 1e-9)
1110+
if support_atime:
1111+
self.assertEqual(st.st_atime, atime_ns * 1e-9)
11021112
self.assertEqual(st.st_mtime, mtime_ns * 1e-9)
1103-
self.assertEqual(st.st_atime_ns, atime_ns)
1113+
if support_atime:
1114+
self.assertEqual(st.st_atime_ns, atime_ns)
11041115
self.assertEqual(st.st_mtime_ns, mtime_ns)
11051116

11061117
def test_utime(self):

0 commit comments

Comments
 (0)