Skip to content

Commit 30faab7

Browse files
committed
Fix atime/mtime copying
1 parent 4dd7c21 commit 30faab7

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

Lib/pathlib/_os.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,14 @@ def _create_metadata(self, source):
323323
"""Copy metadata from the given path to our path."""
324324
target = self._path
325325
info = source.info
326-
copy_times_ns = hasattr(info, '_get_times')
326+
copy_times_ns = hasattr(info, '_get_atime_ns') and hasattr(info, '_get_mtime_ns')
327327
copy_xattrs = hasattr(info, '_get_xattrs') and hasattr(os, 'setxattr')
328328
copy_mode = hasattr(info, '_get_mode')
329329
copy_flags = hasattr(info, '_get_flags') and hasattr(os, 'chflags')
330330

331331
if copy_times_ns:
332-
atime_ns, mtime_ns = info._get_times_ns()
332+
atime_ns = info._get_atime_ns()
333+
mtime_ns = info._get_mtime_ns()
333334
if atime_ns and mtime_ns:
334335
os.utime(target, ns=(atime_ns, mtime_ns))
335336
if copy_xattrs:
@@ -357,16 +358,21 @@ def _create_symlink_metadata(self, source):
357358
"""Copy metadata from the given symlink to our symlink."""
358359
target = self._path
359360
info = source.info
360-
copy_times_ns = (hasattr(info, '_get_times') and
361+
copy_times_ns = (hasattr(info, '_get_atime_ns') and
362+
hasattr(info, '_get_mtime_ns') and
361363
os.utime in os.supports_follow_symlinks)
362-
copy_xattrs = (hasattr(info, '_get_xattrs') and hasattr(os, 'setxattr') and
364+
copy_xattrs = (hasattr(info, '_get_xattrs') and
365+
hasattr(os, 'setxattr') and
363366
os.setxattr in os.supports_fd)
364-
copy_mode = hasattr(info, '_get_mode') and hasattr(os, 'lchmod')
365-
copy_flags = (hasattr(info, '_get_flags') and hasattr(os, 'chflags') and
367+
copy_mode = (hasattr(info, '_get_mode') and
368+
hasattr(os, 'lchmod'))
369+
copy_flags = (hasattr(info, '_get_flags') and
370+
hasattr(os, 'chflags') and
366371
os.chflags in os.supports_follow_symlinks)
367372

368373
if copy_times_ns:
369-
atime_ns, mtime_ns = info._get_times_ns(follow_symlinks=False)
374+
atime_ns = info._get_atime_ns(follow_symlinks=False)
375+
mtime_ns = info._get_mtime_ns(follow_symlinks=False)
370376
if atime_ns and mtime_ns:
371377
os.utime(target, ns=(atime_ns, mtime_ns), follow_symlinks=False)
372378
if copy_xattrs:
@@ -449,13 +455,19 @@ def _get_mode(self, *, follow_symlinks=True):
449455
return 0
450456
return st.st_mode
451457

452-
def _get_times_ns(self, *, follow_symlinks=True):
453-
"""Return the access and modify times in nanoseconds. If stat() fails,
454-
both values are set to zero."""
458+
def _get_atime_ns(self, *, follow_symlinks=True):
459+
"""Return the access time in nanoseconds, or zero if stat() fails."""
455460
st = self._stat(follow_symlinks=follow_symlinks)
456461
if st is None:
457-
return 0, 0
458-
return st.st_atime_ns, st.st_mtime_ns
462+
return 0
463+
return st.st_atime_ns
464+
465+
def _get_mtime_ns(self, *, follow_symlinks=True):
466+
"""Return the modify time in nanoseconds, or zero if stat() fails."""
467+
st = self._stat(follow_symlinks=follow_symlinks)
468+
if st is None:
469+
return 0
470+
return st.st_mtime_ns
459471

460472
if hasattr(os.stat_result, 'st_flags'):
461473
def _get_flags(self, *, follow_symlinks=True):

0 commit comments

Comments
 (0)