Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12820c0

Browse files
committed
Revert utime(..., None) strategy (it has too poor resolution under Windows) and restore the previous test workaround
(issue #19715)
1 parent c3055be commit 12820c0

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

Lib/pathlib.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import posixpath
77
import re
88
import sys
9+
import time
910
import weakref
1011
try:
1112
import threading
@@ -1075,8 +1076,9 @@ def touch(self, mode=0o666, exist_ok=True):
10751076
# First try to bump modification time
10761077
# Implementation note: GNU touch uses the UTIME_NOW option of
10771078
# the utimensat() / futimens() functions.
1079+
t = time.time()
10781080
try:
1079-
self._accessor.utime(self, None)
1081+
self._accessor.utime(self, (t, t))
10801082
except OSError:
10811083
# Avoid exception chaining
10821084
pass

Lib/test/test_pathlib.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,8 +1391,11 @@ def test_touch_common(self):
13911391
# The file mtime should be refreshed by calling touch() again
13921392
p.touch()
13931393
st = p.stat()
1394-
self.assertGreaterEqual(st.st_mtime_ns, old_mtime_ns)
1395-
self.assertGreaterEqual(st.st_mtime, old_mtime)
1394+
# Issue #19715: there can be an inconsistency under Windows between
1395+
# the timestamp rounding when creating a file, and the timestamp
1396+
# rounding done when calling utime(). `delta` makes up for this.
1397+
delta = 1e-6 if os.name == 'nt' else 0
1398+
self.assertGreaterEqual(st.st_mtime, old_mtime - delta)
13961399
# Now with exist_ok=False
13971400
p = P / 'newfileB'
13981401
self.assertFalse(p.exists())

0 commit comments

Comments
 (0)