You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed an issue passing in unicode to os.path.relpath.
Specifically that in some cases when passing in unicode I would get back unicode and others I would get back a string. Below I demonstrate the issue. I also attached a patch.
Is this an issue or am I misunderstanding something. Is the patch reasonable? Totally willing to improve and i'll admit I cannot test the ntpath version.
Python 2.7.6 (default, Apr 9 2014, 11:48:52)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.relpath(u'.', u'.')
'.'
>>> os.path.relpath(u'.', u'../')
u'bachmann'
Why you should check the type? There is no difference between '.' and u'.'.
bachmann1234 commented on May 25, 2014
bachmann1234mannequin
MannequinAuthor
More actions
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
in order to ensure that my string is once again of the correct type.
bachmann1234 commented on May 25, 2014
bachmann1234mannequin
MannequinAuthor
More actions
Perhaps this is the bug I should be filing but here is why this comes up for me.
I get different output from this function if I pass in two types.
On my machine:
os.path.relpath(u'test_srcl.txt', u'.') returns u'test_src.txt'
os.path.relpath(u'test_srcl.txt', '.') returns u'../../Users/bachmann/Code/diff-cover/diff_cover/tests/fixtures/test_src.txt'
I make a couple calls to this function, if the first call gives me back a byte string and I pass it to the second call I get the incorrect result. So I need to decode.
If the function always gave back the same type as I gave it I would not have this issue.
In Python 2 str is coerced to unicode, so most functions should return the same (or compatible) result for str and unicode argument if it contains only 7-bit ASCII characters. Of course there are several obvious exceptions, such as type() or repr(). And presumably there are several bugs.
Apparently the actual bug in your case is that os.path.relpath(u'test_srcl.txt', u'.') and os.path.relpath(u'test_srcl.txt', '.') return totally different results.
What are os.getcwd(), os.getcwdu(), ntpath.abspath(ntpath.normpath(p)) for p in [u'test_srcl.txt', 'test_srcl.txt', u'.', '.'] in your case?
bachmann1234 commented on May 26, 2014
bachmann1234mannequin
MannequinAuthor
More actions
Looking into the project im working on I discovered why relpath was acting strangely.
It is because the project mocks get_cwd but not get_cwdu. Your request helped me track that down :-)
So that is not an issue. However, the issue described in the original ticket definitely happens in a clean python shell.
I still think it is bad that the method sometimes returns str and sometimes returns unicode, but I see your point that ultimately the byte strings that do come out of here coerce into unicode cleanly.
Activity
bachmann1234 commentedon Apr 24, 2014
I noticed an issue passing in unicode to os.path.relpath.
Specifically that in some cases when passing in unicode I would get back unicode and others I would get back a string. Below I demonstrate the issue. I also attached a patch.
Is this an issue or am I misunderstanding something. Is the patch reasonable? Totally willing to improve and i'll admit I cannot test the ntpath version.
serhiy-storchaka commentedon May 14, 2014
I think this is not a bug and shouldn't be fixed in 2.7.
bachmann1234 commentedon May 14, 2014
Can you help me understand why not?
If I give it two unicode strings it sometimes gives me back a unicode and sometimes gives me back a string.
In python3 this does what I expect.
In python27 I now have to check the type I get back because I cannot be sure what type I will be getting back.
serhiy-storchaka commentedon May 25, 2014
Why you should check the type? There is no difference between '.' and u'.'.
bachmann1234 commentedon May 25, 2014
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
in order to ensure that my string is once again of the correct type.
bachmann1234 commentedon May 25, 2014
Perhaps this is the bug I should be filing but here is why this comes up for me.
I get different output from this function if I pass in two types.
On my machine:
os.path.relpath(u'test_srcl.txt', u'.') returns u'test_src.txt'
os.path.relpath(u'test_srcl.txt', '.') returns u'../../Users/bachmann/Code/diff-cover/diff_cover/tests/fixtures/test_src.txt'
I make a couple calls to this function, if the first call gives me back a byte string and I pass it to the second call I get the incorrect result. So I need to decode.
If the function always gave back the same type as I gave it I would not have this issue.
serhiy-storchaka commentedon May 25, 2014
In Python 2 str is coerced to unicode, so most functions should return the same (or compatible) result for str and unicode argument if it contains only 7-bit ASCII characters. Of course there are several obvious exceptions, such as type() or repr(). And presumably there are several bugs.
Apparently the actual bug in your case is that os.path.relpath(u'test_srcl.txt', u'.') and os.path.relpath(u'test_srcl.txt', '.') return totally different results.
What are os.getcwd(), os.getcwdu(), ntpath.abspath(ntpath.normpath(p)) for p in [u'test_srcl.txt', 'test_srcl.txt', u'.', '.'] in your case?
bachmann1234 commentedon May 26, 2014
Looking into the project im working on I discovered why relpath was acting strangely.
It is because the project mocks get_cwd but not get_cwdu. Your request helped me track that down :-)
So that is not an issue. However, the issue described in the original ticket definitely happens in a clean python shell.
I still think it is bad that the method sometimes returns str and sometimes returns unicode, but I see your point that ultimately the byte strings that do come out of here coerce into unicode cleanly.
Thanks for working though this with me.
serhiy-storchaka commentedon May 28, 2014
OK. I say the original issue is not a bug.