Currently, when given a file descriptor, os.listdir will duplicate it so that the original file descriptor is not closed. In many cases, a file descriptor is not needed anymore after directory is listed, so this is not necessary. I propose adding a keyword argument closefd to os.listdir which, if set to True, will make os.listdir take ownership of the passed file descriptor and close it at the end.
Not a problem, just two unnecessary syscalls. Also, I think that many of those who pass a file descriptor to os.listdir don't need it afterwards, because after you fstat() a file descriptor (to discover that it points to a directory) and read the directory contents, there are few things you can use it for.
This proposal doesn't feel to me like it matches the general APIs we have in the standard library. I would think the python style for this would be to use a context manager when opening the fd. So, I'm -1 on this proposal.
I agree with Serhiy and David. The responsibility for closing the file is with whoever opened it, and adding an option in a function for it to also close the fd after doing what it was designed to do makes the API unnecessarily over-complicated.
History
Date
User
Action
Args
2022-04-11 14:58:31
admin
set
github: 71273
2021-06-24 10:35:09
iritkatriel
set
status: open -> closednosy: + iritkatrielmessages: + resolution: rejectedstage: resolved