Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.1k
GH-125413: Addpathlib.Path.dir_entry
attribute#125419
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Add a `Path.dir_entry` attribute. In any path object generated by`Path.iterdir()`, it stores an `os.DirEntry` object corresponding to thepath; in other cases it is `None`.This can be used to retrieve the file type and attributes of directorychildren without necessarily incurring further system calls.Under the hood, we use `dir_entry` in our implementations of`PathBase.glob()`, `PathBase.walk()` and `PathBase.copy()`, the last ofwhich also provides the implementation of `Path.copy()`, resulting in amodest speedup when copying local directory trees.
Copying is a little faster:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I'll review tests when I'm not sleepy.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Code that accessesdir_entry
is explicitly saying "potentially stale values are OK", so what if we defined it as being lazily populated rather than as it beingNone
if not set externally before being accessed?
This would have the added benefit that the required-for-technical-reasons slot onPurePathBase
would be called_dir_entry
, and we could define the public read-only property onPathBase
like:
@propertydefdir_entry(self):ifself._dir_entryisnotNone:returnself._dir_entryself.dir_entry=dir_entry=os.DirEntry.from_path(self)returndir_entry
Itwould need a new helper inos.DirEntry
that accepted anos.PathLike
parameter and creating a populated directory entry instance for it, but that seems like a potentially useful feature anyway.
When you're done making the requested changes, leave the comment: |
I played around with that idea, and I haven't completely ruled it out, but it's a bit of a rabbit hole. On naming and re-using Then we need to define when Then we need to figure out how this interacts with the rest of the None of this is insurmountable, mind :) |
Perhaps I'm overthinking this, and all we really need is a |
picnixz commentedOct 28, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Once you've decided on whether to continue on this work or not, please ping me again (sorry, I missed this one) |
Uh oh!
There was an error while loading.Please reload this page.
Add a
Path.dir_entry
attribute. In any path object generated byPath.iterdir()
, it stores anos.DirEntry
object corresponding to the path; in other cases it isNone
.This can be used to retrieve the file type and attributes of directory children without necessarily incurring further system calls.
Under the hood, we use
dir_entry
in our implementations ofPathBase.glob()
,PathBase.walk()
andPathBase.copy()
, the last of which also provides the implementation ofPath.copy()
, resulting in a modest speedup when copying local directory trees.os.DirEntry
objects from pathlib #125413📚 Documentation preview 📚:https://cpython-previews--125419.org.readthedocs.build/