[issue3187] os.listdir can return byte strings

STINNER Victor report at bugs.python.org
Thu Aug 21 12:59:29 CEST 2008


STINNER Victor <haypo at users.sourceforge.net> added the comment:

I wrote a Filename class. I tries different methods:
 * no parent class "class Filename: ..." -> I don't know how to make 
bytes(filename) works!? But it's the best option to avoid strange bugs 
(mix bytes/str, remember Python 2.x...)
 * str parent class "class Filename(str): ..." -> doesn't work because 
os functions uses the fake unicode filename before testing the bytes 
(real) filename
 * bytes parent class "class Filename(bytes): ..." -> that's the 
current implementation

The idea is to encode str -> bytes (and not bytes -> str because we 
want to avoid problems with such conversions). So I reimplemented most 
bytes methods: __addr__, __raddr__, __contains__, startswith, endswith 
and index. index method has no start/end arguments since the behaviour 
would be different than a real unicode string :-/

I added an example of fixed os.listdir(): create Filename() object if 
we get bytes. Should we always create Filename objects? I don't think 
so.

Added file: http://bugs.python.org/file11189/filename.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3187>
_______________________________________


More information about the Python-bugs-list mailing list