✕
Skip to content

os.path.relpath returns inconsistent types #65542

Closed

Description

@bachmann1234
BPO 21343
Nosy @serhiy-storchaka, @Bachmann1234
Files
  • reldir.patch: Patch to described issue
  • 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 = None
    closed_at = <Date 2014-05-28.15:57:53.780>
    created_at = <Date 2014-04-24.06:40:22.270>
    labels = ['invalid', 'type-bug', 'library']
    title = 'os.path.relpath returns inconsistent types'
    updated_at = <Date 2014-05-28.15:57:53.778>
    user = 'https://github.com/bachmann1234'

    bugs.python.org fields:

    activity = <Date 2014-05-28.15:57:53.778>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-05-28.15:57:53.780>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2014-04-24.06:40:22.270>
    creator = 'Matt.Bachmann'
    dependencies = []
    files = ['35019']
    hgrepos = []
    issue_num = 21343
    keywords = ['patch']
    message_count = 9.0
    messages = ['217119', '218516', '218528', '219074', '219092', '219094', '219097', '219126', '219284']
    nosy_count = 2.0
    nosy_names = ['serhiy.storchaka', 'Matt.Bachmann']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue21343'
    versions = ['Python 2.7']

    Activity

    1. bachmann1234 commented on Apr 24, 2014

      bachmann1234mannequin
      MannequinAuthor

      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'
    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Apr 24, 2014
    3. serhiy-storchaka commented on May 14, 2014

      @serhiy-storchaka
      Member

      I think this is not a bug and shouldn't be fixed in 2.7.

    4. bachmann1234 commented on May 14, 2014

      bachmann1234mannequin
      MannequinAuthor

      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.

    5. serhiy-storchaka commented on May 25, 2014

      @serhiy-storchaka
      Member

      Why you should check the type? There is no difference between '.' and u'.'.

    6. bachmann1234 commented on May 25, 2014

      bachmann1234mannequin
      MannequinAuthor

      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.

    7. bachmann1234 commented on May 25, 2014

      bachmann1234mannequin
      MannequinAuthor

      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.

    8. serhiy-storchaka commented on May 25, 2014

      @serhiy-storchaka
      Member

      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?

    9. bachmann1234 commented on May 26, 2014

      bachmann1234mannequin
      MannequinAuthor

      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.

    10. serhiy-storchaka commented on May 28, 2014

      @serhiy-storchaka
      Member

      OK. I say the original issue is not a bug.

    11. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions