[issue21343] os.path.relpath returns inconsistent types

Matt Bachmann report at bugs.python.org
Sun May 25 16:55:53 CEST 2014


Matt Bachmann added the comment:

There is a difference! '.' is a bytes string and u'.' is a unicode one! 

I found this problem because I work on a project that supports both python2 and python3.

In python3 I pass in unicode I get back unicode. In python2.7 I pass in unicode and I get back a bytes string. We need to ensure that all data in the system is unicode. 

Under 2.7 I get unicode sometimes and bytes other times so I need to do this ugly check 

    root_rel_path = os.path.relpath(self._cwd, self._root)
    if isinstance(root_rel_path, six.binary_type):
        root_rel_path = root_rel_path.decode()

in order to ensure that my string is once again of the correct type.

----------

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


More information about the Python-bugs-list mailing list