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
Clarify the behavior of listdir(fd) in both code and documentation #59381
Comments
When you pass in a file descriptor as the first argument to listdir, its output is a list of strings, not a list of bytes. This should be mentioned in the documentation. It's also worth making a pass over the other functions accepting an fd. As it happens, the code that implements this behavior is obtuse. So I propose also modifying it to be more intelligible. |
Maybe the correct fix for this is to change the interface? os.listdir(path, *, dir_fd=None) Then we'll pick up the return type (str/bytes) from the path variable. It would make bpo-15177 even easier too, should we go down that route. |
+1 for changing the API for any cases where the filesystem path is also used to determine the return type - given that we stuffed this up for the rmtree implementation, I expect end users would be prone to making exactly the same mistake. Retaining the ability to control the return type explicitly even when pass a descriptor would be a *lot* cleaner than other possibilities (which would all involve a bunch of manual encoding and decoding with os.fsencode() and os.fsdecode()). |
Nick, how do you feel specifically about listdir(path, *, dir_fd)? I'm mentally exhausted from the past couple of days and have temporarily lost the ability to infer. |
Nevermind, I'm an idiot. fdopendir doesn't support dir_fd as a separate path, so listdir can't support it either. There's an unwritten property of the os module on a POSIX system: all the functions are essentially-atomic. This is useful and should not be broken lightly. I'll make a patch clarifying the behavior of listdir. |
Don't forget to point people to os.fsencode() if they actually wanted bytes, or os.fsdecode() if they already have bytes and want to convert them to text. |
My first cut at a patch. Made the logic in posix_listdir easy to follow, fixed up the docstring and docs. Posted in the correct issue this time. (And Nick: I thought of that independently :D ) |
Removed "do we have os.listdir" checks from the unit tests. Yes, Virginia, we always have os.listdir in Python 3.3. |
Regenerated patch to make Reitveld happy. |
GRAHH HULK SMASH |
New changeset 8172d8e907a4 by Larry Hastings in branch 'default': |
Good news, everyone! |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: