This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Make os.DirEntry exist
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: benhoyt, brett.cannon, moloney, python-dev, vstinner
Priority: normal Keywords: patch

Created on 2016-05-16 15:43 by brett.cannon, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue27038.patch JelleZijlstra, 2016-06-03 00:08 patch adds os.DirEntry and a test review
Messages (12)
msg265700 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-05-16 15:43
The docs claim that os.DirEntry exists (https://docs.python.org/3/library/os.html#os.DirEntry) but it actually does not (it is internal to the posix module). Should probably actually make os.DirEntry exist (but continue to not document the constructor).
msg269213 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-06-24 21:14
New changeset b841972ed0bd by Brett Cannon in branch 'default':
Issue #27038: Expose DirEntry as os.DirEntry.
https://hg.python.org/cpython/rev/b841972ed0bd
msg269214 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-06-24 21:15
Thanks for the patch, Jelle!
msg272569 - (view) Author: Brendan Moloney (moloney) Date: 2016-08-13 00:19
It would be nice if there was a supported way to create a DirEntry object from a path. If you don't want to document the constructor perhaps expose some helper function or class method (i.e. from_path)?
msg272570 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-08-13 00:35
Exposing a class method to construct one is no different than exposing the constructor, so it's still not desired to have. What is the specific need you have for creating a DirEntry instance on its own?
msg272571 - (view) Author: Brendan Moloney (moloney) Date: 2016-08-13 01:40
It makes it much easier to write functions that can work with either a DirEntry object or a plain path.
msg272578 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-08-13 06:29
Brendan Moloney added the comment:
> It would be nice if there was a supported way to create a DirEntry object
from a path. If you don't want to document the constructor perhaps expose
some helper function or class method (i.e. from_path)?

It's a deliberate choice to not expose the constructor. You should not
built such objzct yourself. It requires low-level data coming from
readdir() or FindFirstFile(). If you don't pass such data, you loose the
whole purpose of the optimization.

Maybe the DirEntry doc must be more explicit on that point?

Please use pathlib instead. pathlib objects don't cache os.stat() result,
but that's also deliberate! It was proposed to add an optional cache, but
the idea was not accepted.
msg272789 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-08-15 18:41
New changeset a95d98086621 by Ned Deily in branch 'default':
Issue #27736: Prevent segfault after interpreter re-initialization due
https://hg.python.org/cpython/rev/a95d98086621
msg272804 - (view) Author: Brendan Moloney (moloney) Date: 2016-08-15 22:11
The pathlib Path class is different enough from the DirEntry class that it doesn't really help my goal of allowing a function to work with either a DirEntry or a plain (str) path.

These functions are going to be working with DirEntry objects generated by scandir 99% of the time so that we can leverage the improved performance, but I want to allow users to pass in plain paths as well. If the stdlib doesn't allow me to create my own DirEntry object from a plain path I will probably end up creating some sort of FakeDirEntry class that mimics a DirEntry, and that seems a bit silly and not a great use of time and effort.

If the constructor currently "requires low-level data coming from
readdir() or FindFirstFile()" then why not a class method that just takes a path and calls stat on it? You could document the fact that it calls stat and that it doesn't have any of the performance benefits of using scandir. Consenting adults and all that...
msg272808 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-08-15 23:24
If all you want is to extract the path representation from an os.DirEntry instance and continue to work with strings/bytes then you can use os.fspath() which is new in Python 3.6 to get the representation: https://docs.python.org/3.6/library/os.html#os.fspath .
msg272810 - (view) Author: Brendan Moloney (moloney) Date: 2016-08-15 23:45
The functions were all written around DirEntry objects since that is what they will be processing 99% of the time and I want to do that as efficiently as possible. Converting a DirEntry object into a str representation doesn't help me with my use case at all. I want to be able to do it the other way around: str -> DirEntry
msg272849 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-08-16 10:30
Brendan Moloney: Please open a thread on the python-ideas mailing list to discuss your idea. Please don't start discussing on a *closed* issue: since an issue is closed, it's hidden from the main bug tracker and so only very few people see it.
History
Date User Action Args
2022-04-11 14:58:31adminsetgithub: 71225
2016-08-16 10:30:21vstinnersetmessages: + msg272849
2016-08-15 23:45:27moloneysetmessages: + msg272810
2016-08-15 23:24:30brett.cannonsetmessages: + msg272808
2016-08-15 22:11:33moloneysetmessages: + msg272804
2016-08-15 18:41:56python-devsetmessages: + msg272789
2016-08-13 06:29:54vstinnersetmessages: + msg272578
2016-08-13 01:40:45moloneysetmessages: + msg272571
2016-08-13 00:35:14brett.cannonsetmessages: + msg272570
2016-08-13 00:19:56moloneysetnosy: + moloney
messages: + msg272569
2016-06-24 21:15:15brett.cannonsetstatus: open -> closed
resolution: fixed
messages: + msg269214

stage: needs patch -> resolved
2016-06-24 21:14:53python-devsetnosy: + python-dev
messages: + msg269213
2016-06-10 21:38:59brett.cannonsetassignee: brett.cannon
2016-06-03 00:08:29JelleZijlstrasetfiles: + issue27038.patch
keywords: + patch
2016-05-23 14:37:29Jim.Jewettsetstage: needs patch
2016-05-16 15:49:26vstinnersetnosy: + vstinner
2016-05-16 15:49:19vstinnersetnosy: + benhoyt
2016-05-16 15:43:35brett.cannoncreate