Skip to content

Commit 764b8ae

Browse files
committed
Wrap os.DirEntry in _DirEntryStatus
1 parent 28bcf00 commit 764b8ae

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

Doc/library/pathlib.rst

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,11 +1195,8 @@ Querying file type and status
11951195
directory
11961196

11971197
If the path was generated from :meth:`Path.iterdir` then this attribute is
1198-
an :class:`os.DirEntry` instance. These objects are initialized with some
1199-
information about the file type; see the :func:`os.scandir` docs for
1200-
details. In other cases, this attribute is an instance of an internal
1201-
pathlib class which initially knows nothing about the file status. In
1202-
either case, merely accessing :attr:`Path.status` does not perform any
1198+
initialized with information about the file type gleaned from scanning the
1199+
parent. Merely accessing :attr:`Path.status` does not perform any
12031200
filesystem queries.
12041201

12051202
To fetch up-to-date information, it's best to call :meth:`Path.is_dir`,

Doc/whatsnew/3.14.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,8 @@ pathlib
543543
implementing the :class:`pathlib.types.Status` protocol (also new). The
544544
object supports querying the file type and internally caching
545545
:func:`~os.stat` results. Path objects generated by
546-
:meth:`~pathlib.Path.iterdir` store :class:`os.DirEntry` objects, which are
547-
initialized with file type information gleaned from scanning the parent
548-
directory.
546+
:meth:`~pathlib.Path.iterdir` are initialized with file type information
547+
gleaned from scanning the parent directory.
549548

550549
(Contributed by Barney Gale in :gh:`125413`.)
551550

Lib/pathlib/_local.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,35 @@ def is_symlink(self):
108108
return S_ISLNK(self._get_mode(follow_symlinks=False))
109109

110110

111+
class _DirEntryStatus:
112+
__slots__ = ('_entry', '_repr')
113+
114+
def __init__(self, path, entry):
115+
self._entry = entry
116+
self._repr = f"<{type(path).__name__}.status>"
117+
118+
def __repr__(self):
119+
return self._repr
120+
121+
def is_dir(self, *, follow_symlinks=True):
122+
"""
123+
Whether this path is a directory.
124+
"""
125+
return self._entry.is_dir(follow_symlinks=follow_symlinks)
126+
127+
def is_file(self, *, follow_symlinks=True):
128+
"""
129+
Whether this path is a regular file.
130+
"""
131+
return self._entry.is_file(follow_symlinks=follow_symlinks)
132+
133+
def is_symlink(self):
134+
"""
135+
Whether this path is a symbolic link.
136+
"""
137+
return self._entry.is_symlink()
138+
139+
111140
class PurePath(PurePathBase):
112141
"""Base class for manipulating paths without I/O.
113142
@@ -762,7 +791,7 @@ def _filter_trailing_slash(self, paths):
762791
def _from_dir_entry(self, dir_entry, path_str):
763792
path = self.with_segments(path_str)
764793
path._str = path_str
765-
path._status = dir_entry
794+
path._status = _DirEntryStatus(path, dir_entry)
766795
return path
767796

768797
def iterdir(self):

Misc/NEWS.d/next/Library/2024-12-10-19-39-35.gh-issue-125413.wOb4yr.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ Add :attr:`pathlib.Path.status` attribute, which stores an object
22
implementing the :class:`pathlib.types.Status` protocol (also new). The
33
object supports querying the file type and internally caching
44
:func:`~os.stat` results. Path objects generated by
5-
:meth:`~pathlib.Path.iterdir` store :class:`os.DirEntry` objects, which are
6-
initialized with file type information gleaned from scanning the parent
7-
directory.
5+
:meth:`~pathlib.Path.iterdir` are initialized with file type information
6+
gleaned from scanning the parent directory.

0 commit comments

Comments
 (0)