classification
Title: scandir.dirfd() method
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, eryksun, giampaolo.rodola
Priority: normal Keywords: patch

Created on 2019-12-19 18:29 by giampaolo.rodola, last changed 2019-12-20 01:11 by eryksun. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 17664 closed giampaolo.rodola, 2019-12-19 18:29
Messages (4)
msg358686 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2019-12-19 18:29
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.
msg358687 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2019-12-19 18:47
Why not just os.open the directory yourself and pass it to os.scandir?
msg358688 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2019-12-19 19:08
Good point, I didn't consider that. I suppose you're right. =)
Closing.
msg358692 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-12-20 01:11
> 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.
History
Date User Action Args
2019-12-20 01:11:30eryksunsetnosy: + eryksun
messages: + msg358692
2019-12-19 19:08:15giampaolo.rodolasetstatus: open -> closed

messages: + msg358688
stage: patch review -> resolved
2019-12-19 18:47:40benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg358687
2019-12-19 18:29:56giampaolo.rodolasetkeywords: + patch
stage: patch review
pull_requests: + pull_request17131
2019-12-19 18:29:01giampaolo.rodolacreate