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

Make os.DirEntry exist #71225

Closed
brettcannon opened this issue May 16, 2016 · 12 comments
Closed

Make os.DirEntry exist #71225

brettcannon opened this issue May 16, 2016 · 12 comments
Assignees
Labels
stdlib Python modules in the Lib dir

Comments

@brettcannon
Copy link
Member

BPO 27038
Nosy @brettcannon, @vstinner, @benhoyt
Files
  • issue27038.patch: patch adds os.DirEntry and a test
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/brettcannon'
    closed_at = <Date 2016-06-24.21:15:15.669>
    created_at = <Date 2016-05-16.15:43:34.988>
    labels = ['library']
    title = 'Make os.DirEntry exist'
    updated_at = <Date 2016-08-16.10:30:21.733>
    user = 'https://github.com/brettcannon'

    bugs.python.org fields:

    activity = <Date 2016-08-16.10:30:21.733>
    actor = 'vstinner'
    assignee = 'brett.cannon'
    closed = True
    closed_date = <Date 2016-06-24.21:15:15.669>
    closer = 'brett.cannon'
    components = ['Library (Lib)']
    creation = <Date 2016-05-16.15:43:34.988>
    creator = 'brett.cannon'
    dependencies = []
    files = ['43130']
    hgrepos = []
    issue_num = 27038
    keywords = ['patch']
    message_count = 12.0
    messages = ['265700', '269213', '269214', '272569', '272570', '272571', '272578', '272789', '272804', '272808', '272810', '272849']
    nosy_count = 5.0
    nosy_names = ['brett.cannon', 'vstinner', 'benhoyt', 'python-dev', 'moloney']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue27038'
    versions = ['Python 3.6']

    @brettcannon
    Copy link
    Member Author

    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).

    @brettcannon brettcannon added the stdlib Python modules in the Lib dir label May 16, 2016
    @brettcannon brettcannon self-assigned this Jun 10, 2016
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 24, 2016

    New changeset b841972ed0bd by Brett Cannon in branch 'default':
    Issue bpo-27038: Expose DirEntry as os.DirEntry.
    https://hg.python.org/cpython/rev/b841972ed0bd

    @brettcannon
    Copy link
    Member Author

    Thanks for the patch, Jelle!

    @moloney
    Copy link
    Mannequin

    moloney mannequin commented Aug 13, 2016

    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)?

    @brettcannon
    Copy link
    Member Author

    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?

    @moloney
    Copy link
    Mannequin

    moloney mannequin commented Aug 13, 2016

    It makes it much easier to write functions that can work with either a DirEntry object or a plain path.

    @vstinner
    Copy link
    Member

    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.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 15, 2016

    New changeset a95d98086621 by Ned Deily in branch 'default':
    Issue bpo-27736: Prevent segfault after interpreter re-initialization due
    https://hg.python.org/cpython/rev/a95d98086621

    @moloney
    Copy link
    Mannequin

    moloney mannequin commented Aug 15, 2016

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

    @brettcannon
    Copy link
    Member Author

    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 .

    @moloney
    Copy link
    Mannequin

    moloney mannequin commented Aug 15, 2016

    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

    @vstinner
    Copy link
    Member

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants