[issue3187] os.listdir can return byte strings

Guido van Rossum report at bugs.python.org
Fri Oct 3 18:34:58 CEST 2008


Guido van Rossum <guido at python.org> added the comment:

Reducing priority to critical, it's just docs and tweaks from here.

 You should also support bytearray() in ntpath:
>    isinstance(path, (bytes, bytearray))

No, you shouldn't.  I changed my mind on this several times and in the
end figured it's good enough to just support bytes and str instances.

Amaury: I've reviewed your patch and ran test_ntpath.py on a Linux box.
 I get this traceback:

======================================================================
ERROR: test_relpath (__main__.TestNtpath)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "Lib/test/test_ntpath.py", line 188, in test_relpath
    tester('ntpath.relpath("a")', 'a')
  File "Lib/test/test_ntpath.py", line 22, in tester
    gotResult = eval(fn)
  File "<string>", line 1, in <module>
  File "/usr/local/google/home/guido/python/py3k/Lib/ntpath.py", line
530, in relpath
    start_list = abspath(start).split(sep)
  File "/usr/local/google/home/guido/python/py3k/Lib/ntpath.py", line
499, in abspath
    path = join(os.getcwd(), path)
  File "/usr/local/google/home/guido/python/py3k/Lib/ntpath.py", line
137, in join
    if b[:1] in seps:
TypeError: 'in <string>' requires string as left operand, not bytes
----------------------------------------------------------------------

The fix is to change the fallback abspath to this code:

    def abspath(path):
        """Return the absolute version of a path."""
        if not isabs(path):
            if isinstance(path, bytes):
                cwd = os.getcwdb()
            else:
                cwd = os.getcwd()
            path = join(cwd, path)
        return normpath(path)

Once you fix that please check it in!

----------
priority: release blocker -> critical

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


More information about the Python-bugs-list mailing list