Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-125413: Add private metadata methods topathlib.Path.info#129897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
barneygale merged 16 commits intopython:mainfrombarneygale:gh-125413-merge-copyreader
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
16 commits
Select commitHold shift + click to select a range
4dd7c21
GH-125413: Add private metadata methods to `pathlib.Path.info`
barneygaleFeb 9, 2025
30faab7
Fix atime/mtime copying
barneygaleFeb 9, 2025
22e065d
Simplify patch
barneygaleFeb 9, 2025
cd4a1b5
Improve naming
barneygaleFeb 9, 2025
07f89a4
Return `None` if results aren't available.
barneygaleFeb 9, 2025
2026886
Merge `_create_metadata()` and `_create_symlink_metadata()`
barneygaleFeb 9, 2025
ba4d393
Simplify patch a bit
barneygaleFeb 9, 2025
6cde4fb
Further reduce diff
barneygaleFeb 9, 2025
0871a39
Stop suppressing stat() errors from new methods
barneygaleFeb 9, 2025
89e49bf
Stop using `samefile()`, add `_file_id()` and `_device_id()`
barneygaleFeb 11, 2025
86b671f
Attempt to fix access time test
barneygaleFeb 12, 2025
f6b2249
Delete `_device_id()`
barneygaleFeb 13, 2025
47616de
Merge branch 'main' into gh-125413-merge-copyreader
barneygaleFeb 13, 2025
bc9032b
Merge branch 'main' into gh-125413-merge-copyreader
barneygaleFeb 16, 2025
6c03d51
Merge branch 'main' into gh-125413-merge-copyreader
encukouFeb 17, 2025
13db1fe
`_create_metadata` -> `_copy_metadata`
barneygaleFeb 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Stop usingsamefile(), add_file_id() and_device_id()
  • Loading branch information
@barneygale
barneygale committedFeb 11, 2025
commit89e49bf9a46100982e83f71c9d2b21a2837f3675
4 changes: 2 additions & 2 deletionsLib/pathlib/_local.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,7 +19,7 @@
except ImportError:
grp = None

from pathlib._os import LocalCopyWriter, PathInfo, DirEntryInfo
from pathlib._os import LocalCopyWriter, PathInfo, DirEntryInfo, ensure_different_files
from pathlib._abc import JoinablePath, ReadablePath, WritablePath


Expand DownExpand Up@@ -1069,7 +1069,7 @@ def move(self, target):
else:
if not hasattr(target, '_copy_writer'):
target = self.with_segments(target_str)
target._copy_writer._ensure_different_file(self)
ensure_different_files(self, target)
try:
os.replace(self, target_str)
return target
Expand Down
83 changes: 49 additions & 34 deletionsLib/pathlib/_os.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -216,7 +216,7 @@ def _create_metadata(self, source, follow_symlinks=True):
pass

def _create(self, source, follow_symlinks, dirs_exist_ok, preserve_metadata):
self._ensure_distinct_path(source)
ensure_distinct_paths(source, self._path)
if not follow_symlinks and source.is_symlink():
self._create_symlink(source, preserve_metadata)
elif source.is_dir():
Expand All@@ -243,7 +243,7 @@ def _create_dir(self, source, follow_symlinks, dirs_exist_ok, preserve_metadata)

def _create_file(self, source, preserve_metadata):
"""Copy the given file to our path."""
self._ensure_different_file(source)
ensure_different_files(source, self._path)
with magic_open(source, 'rb') as source_f:
try:
with magic_open(self._path, 'wb') as target_f:
Expand All@@ -263,30 +263,25 @@ def _create_symlink(self, source, preserve_metadata):
if preserve_metadata:
self._create_metadata(source, follow_symlinks=False)

def _ensure_different_file(self, source):
"""
Raise OSError(EINVAL) if both paths refer to the same file.
"""
pass

def_ensure_distinct_path(self, source):
"""
Raise OSError(EINVAL) if the other path is within this path.
"""
# Note: there is no straightforward, foolproof algorithm to determine
# if one directory is within another (a particularly perverse example
# would be a single network share mounted in one location via NFS, and
# in another location via CIFS), so we simply checks whether the
# other path is lexically equal to, or within, this path.
if source ==self._path:
err = OSError(EINVAL, "Source and target are the same path")
elif source inself._path.parents:
err = OSError(EINVAL, "Source path is a parent of target path")
else:
return
err.filename = str(source)
err.filename2 = str(self._path)
raise err
defensure_distinct_paths(source, target):
"""
Raise OSError(EINVAL) if the other path is within this path.
"""
# Note: there is no straightforward, foolproof algorithm to determine
# if one directory is within another (a particularly perverse example
# would be a single network share mounted in one location via NFS, and
# in another location via CIFS), so we simply checks whether the
# other path is lexically equal to, or within, this path.
if source ==target:
err = OSError(EINVAL, "Source and target are the same path")
elif source intarget.parents:
err = OSError(EINVAL, "Source path is a parent of target path")
else:
return
err.filename = str(source)
err.filename2 = str(target)
raise err


class LocalCopyWriter(CopyWriter):
Expand DownExpand Up@@ -376,19 +371,31 @@ def _create_symlink(self, source, preserve_metadata):
if preserve_metadata:
self._create_metadata(source, follow_symlinks=False)

def _ensure_different_file(self, source):
"""
Raise OSError(EINVAL) if both paths refer to the same file.
"""

def ensure_different_files(source, target):
"""
Raise OSError(EINVAL) if both paths refer to the same file.
"""
try:
source_file_id = source.info._file_id
target_file_id = target.info._file_id
source_device_id = source.info._device_id
target_device_id = target.info._device_id
except AttributeError:
if source != target:
return
else:
try:
if not self._path.samefile(source):
if source_file_id() != target_file_id():
return
if source_device_id() != target_device_id():
return
except (OSError, ValueError):
return
err = OSError(EINVAL, "Source and target are the same file")
err.filename = str(source)
err.filename2 = str(self._path)
raise err
err = OSError(EINVAL, "Source and target are the same file")
err.filename = str(source)
err.filename2 = str(target)
raise err


class _PathInfoBase:
Expand DownExpand Up@@ -439,6 +446,14 @@ def _posix_permissions(self, *, follow_symlinks=True):
"""Return the POSIX file permissions."""
return S_IMODE(self._stat(follow_symlinks=follow_symlinks).st_mode)

def _file_id(self, *, follow_symlinks=True):
"""Returns the identifier of the file (unique for a device ID)."""
return self._stat(follow_symlinks=follow_symlinks).st_ino

def _device_id(self, *, follow_symlinks=True):
"""Returns the identifier of the device on which the file resides."""
return self._stat(follow_symlinks=follow_symlinks).st_dev

def _access_time_ns(self, *, follow_symlinks=True):
"""Return the access time in nanoseconds."""
return self._stat(follow_symlinks=follow_symlinks).st_atime_ns
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp