Repository navigation
os.listdir can return byte strings #47437
Copy link
Copy link
Closed
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
| BPO | 3187 |
|---|---|
| Nosy | @gvanrossum, @loewis, @birkenfeld, @amauryfa, @pitrou, @vstinner, @benjaminp, @djc |
| Files |
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/birkenfeld'
closed_at = <Date 2008-10-07.07:12:42.880>
created_at = <Date 2008-06-24.10:28:25.633>
labels = ['type-bug', 'docs']
title = 'os.listdir can return byte strings'
updated_at = <Date 2008-10-07.07:12:42.879>
user = 'https://bugs.python.org/HWJ'bugs.python.org fields:
activity = <Date 2008-10-07.07:12:42.879>
actor = 'loewis'
assignee = 'georg.brandl'
closed = True
closed_date = <Date 2008-10-07.07:12:42.880>
closer = 'loewis'
components = ['Documentation']
creation = <Date 2008-06-24.10:28:25.633>
creator = 'HWJ'
dependencies = []
files = ['11212', '11213', '11215', '11216', '11549', '11550', '11581', '11630', '11632', '11652', '11655', '11658', '11663', '11680', '11685', '11693', '11721']
hgrepos = []
issue_num = 3187
keywords = ['patch']
message_count = 79.0
messages = ['68674', '68679', '68684', '68685', '68686', '68688', '68689', '70943', '70953', '71525', '71612', '71615', '71624', '71629', '71647', '71648', '71655', '71680', '71699', '71700', '71705', '71748', '71749', '71751', '71752', '71756', '71757', '71769', '71991', '72495', '73362', '73534', '73535', '73540', '73678', '73680', '73688', '73909', '73910', '73911', '73925', '73926', '73992', '73999', '74000', '74006', '74007', '74008', '74027', '74032', '74059', '74080', '74083', '74101', '74173', '74186', '74192', '74222', '74236', '74237', '74240', '74241', '74242', '74246', '74255', '74256', '74257', '74266', '74267', '74268', '74270', '74271', '74275', '74276', '74277', '74409', '74412', '74414', '74426']
nosy_count = 13.0
nosy_names = ['gvanrossum', 'loewis', 'georg.brandl', 'amaury.forgeotdarc', 'pitrou', 'vstinner', 'draghuram', 'benjamin.peterson', 'djc', 'HWJ', 'dlitz', 'zegreek', 'bboissin']
pr_nums = []
priority = 'critical'
resolution = 'accepted'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue3187'
versions = ['Python 3.0']Metadata
Metadata
Assignees
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
HWJ commentedon Jun 24, 2008
=========================
file WalkBug.py:
#!/usr/local/bin/python3.0
amauryfa commentedon Jun 24, 2008
Could you tell us what this 1665th line should be?
Maybe the 1665th directory has something special (a filename with spaces
or non-ascii chars...)
Can you try with an older version of python?
benjaminp commentedon Jun 24, 2008
It's failing because he's giving a string to bytes.startswith when it
requires a byte string or such.
amauryfa commentedon Jun 24, 2008
"he's giving a string"... the user simply called os.walk, which accepts
strings AFAIK.
We should discover what produced this bytestring. Does listdir() returns
a mixed list of strings and bytes?
benjaminp commentedon Jun 24, 2008
It seems the conversion to unicode strings (PyUnicode vs PyBytes) was
not complete in os.listdir. See the attached patch.
amauryfa commentedon Jun 24, 2008
The original problem seems to come from some Unix platform, but this
patch only handles two cases:
And in both cases, the default (utf-8) conversion seems wrong. Something
like cp1252 (the ANSI code page for Western Windows) would be more sensible.
In the posix part of the function, there is the comment (2003-03-04):
/* fall back to the original byte string, as
discussed in patch bpo-683592 */
btw, I find the penultimate message of this other thread very pleasant,
in the py3k context... I suppose the conclusions would not be the same
today.
HWJ commentedon Jun 24, 2008
Yes, the next directory contains a filename with an iso-latin1 but non-
ascii character
The patch (applied to SVN GMT 13:30) does NOT help.
[-]os.walk - strange bug[/-][+]os.listdir can return byte strings[/+]pitrou commentedon Aug 9, 2008
Hmm, I suppose that while the filename is latin1-encoded,
Py_FileSystemDefaultEncoding is "utf-8" and therefore os.listdir fails
decoding the filename and falls back on returning a byte string.
It was acceptable in Python 2.x but is a very annoying problem in py3k
now that unicode and bytes objects can't be mixed together anymore. I'm
bumping this to critical, although there is probably no clean solution.
85 remaining items
gvanrossum commentedon Oct 3, 2008
FWIW, I don't see a need to change macpath.py -- it's only used for
MacOS 9 and the occasional legacy app. OSX uses posixpath.py.
amauryfa commentedon Oct 3, 2008
Committed r66779: test_ntpath now passes with the -bb option.
It seems that the Windows buildbots do not set -bb.
gvanrossum commentedon Oct 3, 2008
Thanks Amaury!
On to Georg for doc tweaks. Summary:
mixture of str and bytes instances)
Stuff that didn't change but that you might want to mention:
return bytes if the arg is bytes
Martin already documented sys.setfilesystemencoding().
amauryfa commentedon Oct 3, 2008
I have a patch for macpath.py nonetheless.
Tested on Windows (of course ;-) but all functions are pure text
manipulation, except realpath(). It was much easier than ntpath.py.
I also added tests for three functions which were not exercised at all.
benjaminp commentedon Oct 3, 2008
Amaury, you're patch looks good.
amauryfa commentedon Oct 3, 2008
Committed macpath.py in r66781.
vstinner commentedon Oct 6, 2008
Would it possible to close this issue since os.listdir() is fixed and
many other related functions (posix, posixpath, ntpath, macpath, etc.)
are also fixed? I propose to open new issues for new bugs since this
issue becomes a little big long :)
Eg. see new issues bpo-4035 and bpo-4036!
loewis commentedon Oct 6, 2008
IIUC, these fixes are still not complete: they lack documentation
changes. Of course, it would have been better if the original patches
already contained the necessary documentation and test suite changes.
See msg74271 for what Guido considers the lacking documentation;
you may find that other aspects also need documentation.
As for test cases: it seems that those got waived, in the hurry.
vstinner commentedon Oct 6, 2008
Le Tuesday 07 October 2008 01:13:22 Martin v. Löwis, vous avez écrit :
Most (or all) patches include new tests about bytes. Here is a patch for
os.rst documentation about listdir(), getcwdb() and readlink().
I wrote a long document about bytes for filenames but not only. I'm still
waiting for some contributors or reviewers:
http://wiki.python.org/moin/Python3UnicodeDecodeError
Can you be more precise? Which tests have to be improved/rewritten?
loewis commentedon Oct 7, 2008
Thanks! Committed as r66829.
I've added additional documentation in r66830, which should complete
Guido's list of things to be documented. So the issue can be closed
now.
We should discuss that on python-dev, of course - the question is
whether additional documentation patches are needed in response to
this specific change.
I was probably looking at the wrong patches (such as getcwd_bytes.patch,
merge_os_getcwd_getcwdu.patch, etc); I now see that the final patch did
have tests. I recommend that patches that get superseded by other
patches are removed from the issue. The won't be deleted; it's still
possible to navigate to them through the History at the bottom of the
issue.