Pathlib's walk() and glob() implementations are built upon os.scandir(), which yields os.DirEntry objects. The interface for os.DirEntry is a rough subset of pathlib.Path, including the name attribute and is_dir() method.
Pathlib only ever calls os.scandir() via a private pathlib.Path._scandir() method, currently defined as follows:
def_scandir(self):
returnos.scandir(self)
In future I'd like to add a pathlib.AbstractPath class with abstract stat(), iterdir() and open() methods.
The default implementation of AbstractPath._scandir() would use iterdir():
Note how it returns AbstractPath objects, and not DirEntry objects. This exploits the similarities of the Path and DirEntry APIs.
... but there's a problem!
The os.DirEntry.is_dir() method accepts a keyword-only follow_symlinks argument. Our globbing implementation requires us to set this argument. But the Path.is_dir() method does not accept this argument!
If we add a keyword-only follow_symlinks argument to Path.is_dir(), we make it compatible with os.DirEntry.is_dir(), which in turn allows us to build a glob() implementation upon user-defined stat() and iterdir() methods in a future AbstractPath class.
Activity
pythonGH-105793: Add follow_symlinks argument to pathlib.Path.is_dir()
eryksun commented on Jun 14, 2023
Path.is_file() may as well also support follow_symlinks, as is implemented for DirEntry.is_file().
Merge branch 'main' into pythongh-105793-is-dir-follow-symlinks
Merge branch 'main' into pythongh-105793-is-dir-follow-symlinks
Merge branch 'main' into pythongh-105793-is-dir-follow-symlinks
GH-105793: Add follow_symlinks argument to `pathlib.Path.is_dir()` an…
barneygale commented on Jun 26, 2023
Implemented in:
Thank you Steve for the review and Eryk for the is_file() suggestion :-)