Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for not following symlinks in pathlib.Path.is_dir() #105793

Closed
barneygale opened this issue Jun 14, 2023 · 2 comments
Closed

Support for not following symlinks in pathlib.Path.is_dir() #105793

barneygale opened this issue Jun 14, 2023 · 2 comments
Labels
3.13 new features, bugs and security fixes topic-pathlib type-feature A feature request or enhancement

Comments

@barneygale
Copy link
Contributor

barneygale commented Jun 14, 2023

Feature or enhancement

Add a follow_symlinks argument to pathlib.Path.is_dir(), defaulting to True

Pitch

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):
        return os.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():

   def _scandir(self):
       return contextlib.nullcontext(list(self.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.

Previous discussion

General discussion of AbstractPath: https://discuss.python.org/t/make-pathlib-extensible/3428

We added a follow_symlinks argument to Path.exists() in #89769 / #29655.

Linked PRs

@barneygale barneygale added type-feature A feature request or enhancement topic-pathlib 3.13 new features, bugs and security fixes labels Jun 14, 2023
barneygale added a commit to barneygale/cpython that referenced this issue Jun 14, 2023
Brings `pathlib.Path.is_dir()` in line with `os.DirEntry.is_dir()`, which
will be important for implementing generic path walking and globbing.
@eryksun
Copy link
Contributor

eryksun commented Jun 14, 2023

Path.is_file() may as well also support follow_symlinks, as is implemented for DirEntry.is_file().

barneygale added a commit to barneygale/cpython that referenced this issue Jun 15, 2023
barneygale added a commit to barneygale/cpython that referenced this issue Jun 19, 2023
barneygale added a commit to barneygale/cpython that referenced this issue Jun 24, 2023
barneygale added a commit that referenced this issue Jun 26, 2023
…d `is_file()` (GH-105794)

Brings `pathlib.Path.is_dir()` and `in line with `os.DirEntry.is_dir()`, which
will be important for implementing generic path walking and globbing.
Likewise `is_file()`.
@barneygale
Copy link
Contributor Author

Implemented in:

Thank you Steve for the review and Eryk for the is_file() suggestion :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 new features, bugs and security fixes topic-pathlib type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants