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

scandir.dirfd() method #83280

Closed
giampaolo opened this issue Dec 19, 2019 · 4 comments
Closed

scandir.dirfd() method #83280

giampaolo opened this issue Dec 19, 2019 · 4 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir

Comments

@giampaolo
Copy link
Contributor

BPO 39099
Nosy @giampaolo, @benjaminp, @eryksun
PRs
  • bpo-39099: add scandir.dirfd() #17664
  • 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 = None
    closed_at = <Date 2019-12-19.19:08:15.551>
    created_at = <Date 2019-12-19.18:29:01.546>
    labels = ['library', '3.9']
    title = 'scandir.dirfd() method'
    updated_at = <Date 2019-12-20.01:11:30.167>
    user = 'https://github.com/giampaolo'

    bugs.python.org fields:

    activity = <Date 2019-12-20.01:11:30.167>
    actor = 'eryksun'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-12-19.19:08:15.551>
    closer = 'giampaolo.rodola'
    components = ['Library (Lib)']
    creation = <Date 2019-12-19.18:29:01.546>
    creator = 'giampaolo.rodola'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 39099
    keywords = ['patch']
    message_count = 4.0
    messages = ['358686', '358687', '358688', '358692']
    nosy_count = 3.0
    nosy_names = ['giampaolo.rodola', 'benjamin.peterson', 'eryksun']
    pr_nums = ['17664']
    priority = 'normal'
    resolution = None
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue39099'
    versions = ['Python 3.9']

    @giampaolo
    Copy link
    Contributor Author

    PR in attachment adds a new dirfd() method to the scandir() object (POSIX only). This can be be passed to os.* functions supporting the "dir_fd" parameter, and avoid opening a new fd as in:

    >>> dirfd = os.open("basename", os.O_RDONLY, dir_fd=topfd)
    

    At the moment I am not sure if it's possible to also support Windows.

    @giampaolo giampaolo added 3.9 only security fixes stdlib Python modules in the Lib dir labels Dec 19, 2019
    @benjaminp
    Copy link
    Contributor

    Why not just os.open the directory yourself and pass it to os.scandir?

    @giampaolo
    Copy link
    Contributor Author

    Good point, I didn't consider that. I suppose you're right. =)
    Closing.

    @eryksun
    Copy link
    Contributor

    eryksun commented Dec 20, 2019

    I am not sure if it's possible to also support Windows.

    For reference, FindFirstFileW doesn't support handle-relative names, and neither does CreateFileW. At a lower level in Windows NT, NtCreateFile has always supported handle-relative names, but CPython doesn't use the NT API.

    fd support (not dirfd) could be added to listdir and scandir in Windows. A directory can be listed via GetFileInformationByHandleEx: FileFullDirectoryInfo. This doesn't query the short name, so it may perform better than FindFirstFileW. However, opening a directory with os.open can't be supported. VC++ still hasn't documented the _O_OBTAIN_DIR (0x2000) flag.

    @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
    3.9 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants