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: 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

Closed
barneygale wants to merge3 commits intopython:mainfrombarneygale:gh-125413

Conversation

barneygale
Copy link
Contributor

@barneygalebarneygale commentedOct 13, 2024
edited by github-actionsbot
Loading

Add aPath.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 usedir_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.


📚 Documentation preview 📚:https://cpython-previews--125419.org.readthedocs.build/

nineteendo reacted with thumbs up emoji
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.
@barneygale
Copy link
ContributorAuthor

Copying is a little faster:

$ ./python -m timeit -s "from pathlib import Path" "Path('Doc').copy('Doc2', dirs_exist_ok=True, preserve_metadata=True)"5 loops, best of 5: 70.7 msec per loop  # before5 loops, best of 5: 68.7 msec per loop  # after

Copy link
Member

@picnixzpicnixz left a 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.

barneygaleand others added2 commitsOctober 18, 2024 03:15
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Copy link
Contributor

@ncoghlanncoghlan left a 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.

@bedevere-app
Copy link

When you're done making the requested changes, leave the comment:I have made the requested changes; please review again.

@barneygale
Copy link
ContributorAuthor

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-usingDirEntry: I don't thinkos.DirEntry.from_path() makes sense. The purpose ofDirEntry is that it stores information from callingos.scandir() on theparent directory. I think we'd need a new class withname,is_dir() andis_symlink() attributes. We'd lazily generate an instance of this class fromPath.last_status (or.status, or soemthing), assuming there's not already aDirEntry stored. The new class could be calledpathlib.PathStatus or something along those lines.

Then we need to define whenos.stat() is called and when exceptions are raised. ADirEntry object is initially populated withsome information from theos.scandir() call, so we might want ourPathStatus object to perform astat() on creation. But should itos.stat() oros.lstat()? And doesn't that imply that ourPath attribute should be a method rather than a property, given itmay perform serious work? MaybePath.cached_status()?

Then we need to figure out how this interacts with the rest of thePath methods. ShouldPath.stat() andPath.lstat() automatically update the status object? Should it replace an existingDirEntry object with aPathStatus object? ShouldPath.is_dir() callself.stat(); return self.cached_status().is_dir()?

None of this is insurmountable, mind :)

@barneygale
Copy link
ContributorAuthor

Perhaps I'm overthinking this, and all we really need is aPath.scandir() method

@barneygalebarneygale marked this pull request as draftOctober 25, 2024 20:41
@picnixz
Copy link
Member

picnixz commentedOct 28, 2024
edited
Loading

Once you've decided on whether to continue on this work or not, please ping me again (sorry, I missed this one)

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@picnixzpicnixzpicnixz left review comments

@rruuaanngrruuaanngrruuaanng left review comments

@ncoghlanncoghlanncoghlan requested changes

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@barneygale@picnixz@ncoghlan@rruuaanng

[8]ページ先頭

©2009-2025 Movatter.jp