Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: os.path.isabs(os.path.abspath(" ")) == False #75230

Closed
lazka mannequin opened this issue Jul 26, 2017 · 11 comments
Closed

Windows: os.path.isabs(os.path.abspath(" ")) == False #75230

lazka mannequin opened this issue Jul 26, 2017 · 11 comments

Comments

@lazka
Copy link
Mannequin

lazka mannequin commented Jul 26, 2017

BPO 31047
Nosy @pfmoore, @tjguk, @zware, @eryksun, @zooba, @timgraham, @lazka, @miss-islington
PRs
  • bpo-31047: Fix ntpath.abspath for invalid paths #8544
  • [3.7] bpo-31047: Fix ntpath.abspath for invalid paths (GH-8544) #8549
  • [3.6] bpo-31047: Fix ntpath.abspath for invalid paths (GH-8544) #8550
  • bpo-31047: Fix ntpath.abspath to trim ending separator #10082
  • [3.7] bpo-31047: Fix ntpath.abspath to trim ending separator (GH-10082) #10096
  • [3.6] bpo-31047: Fix ntpath.abspath to trim ending separator (GH-10082) #10097
  • 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 2018-10-25.17:47:07.708>
    created_at = <Date 2017-07-26.16:47:29.883>
    labels = ['OS-windows']
    title = 'Windows: os.path.isabs(os.path.abspath(" ")) == False'
    updated_at = <Date 2018-10-25.17:47:07.707>
    user = 'https://github.com/lazka'

    bugs.python.org fields:

    activity = <Date 2018-10-25.17:47:07.707>
    actor = 'steve.dower'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-10-25.17:47:07.708>
    closer = 'steve.dower'
    components = ['Windows']
    creation = <Date 2017-07-26.16:47:29.883>
    creator = 'lazka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 31047
    keywords = ['patch']
    message_count = 11.0
    messages = ['299253', '299266', '322633', '322636', '323229', '328322', '328328', '328448', '328463', '328464', '328465']
    nosy_count = 8.0
    nosy_names = ['paul.moore', 'tim.golden', 'zach.ware', 'eryksun', 'steve.dower', 'Tim.Graham', 'lazka', 'miss-islington']
    pr_nums = ['8544', '8549', '8550', '10082', '10096', '10097']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue31047'
    versions = ['Python 3.6']

    @lazka
    Copy link
    Mannequin Author

    lazka mannequin commented Jul 26, 2017

    On Windows os.path.abspath(" ") == " "

    While that's not a valid Windows path, similar invalid paths like "" or "?" etc all produce an absolute path.

    Tested on 2.7 and 3.6

    @lazka lazka mannequin added the OS-windows label Jul 26, 2017
    @eryksun
    Copy link
    Contributor

    eryksun commented Jul 26, 2017

    The generic abspath implementation could be moved into the genericpath module, where it will be common to both posixpath and ntpath:

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

    Then replace it in ntpath if nt._getfullpathname is defined, but with a fallback to the generic implementation if OSError is raised (e.g. for " "):

    try:
        from nt import _getfullpathname
    except ImportError:
        pass
    else:
        def abspath(path):
            """Return an absolute path."""
            try:
                return _getfullpathname(path)
            except OSError:
                return genericpath.abspath(path)
    

    This _getfullpathname version also skips the redundant fspath and normpath calls.

    @zooba
    Copy link
    Member

    zooba commented Jul 29, 2018

    New changeset d2e902e by Steve Dower (Franz Wöllert) in branch 'master':
    bpo-31047: Fix ntpath.abspath for invalid paths (GH-8544)
    d2e902e

    @miss-islington
    Copy link
    Contributor

    New changeset 5753b13 by Miss Islington (bot) in branch '3.7':
    bpo-31047: Fix ntpath.abspath for invalid paths (GH-8544)
    5753b13

    @zooba
    Copy link
    Member

    zooba commented Aug 7, 2018

    New changeset b0bf51b by Steve Dower in branch '3.6':
    bpo-31047: Fix ntpath.abspath for invalid paths (GH-8544)
    b0bf51b

    @zooba zooba closed this as completed Aug 7, 2018
    @zooba zooba self-assigned this Aug 7, 2018
    @timgraham
    Copy link
    Mannequin

    timgraham mannequin commented Oct 23, 2018

    I think this caused a behavior change:

    Before (Python 3.6.6):
    >>> from os.path import abspath
    >>> abspath('/abc/')
    'C:\\abc'
    
    After (Python 3.6.7):
    >>> abspath('/abc/')
    'C:\\abc\\'

    This causes a test failure in Django's safe_join() function: https://github.com/django/django/blob/10d82c85aa5f8bd6adff0db49798dd368455cdcf/django/utils/_os.py#L24-L47

    https://github.com/django/django/blob/10d82c85aa5f8bd6adff0db49798dd368455cdcf/tests/utils_tests/test_os_utils.py#L10

    Traceback (most recent call last):
      File "C:\Jenkins\workspace\django-windows\database\sqlite3\label\windows\python\Python36\tests\utils_tests\test_os_utils.py", line 10, in test_base_path_ends_with_sep
        drive, path = os.path.splitdrive(safe_join("/abc/", "abc"))
      File "C:\Jenkins\workspace\django-windows\database\sqlite3\label\windows\python\Python36\django\utils\_os.py", line 46, in safe_join
        'component ({})'.format(final_path, base_path))
    django.core.exceptions.SuspiciousFileOperation: The joined path (C:\abc\abc) is located outside of the base path component (C:\abc\)

    @zooba
    Copy link
    Member

    zooba commented Oct 23, 2018

    Agreed, it no longer matches os.normpath()'s declared behavior by not trimming the end separator.

    It's obviously too late for the current release (I'd hope Django is one of the projects running tests against RC's, but I guess not :( ), but I think we can fix it for the next updates on all versions.

    Patches welcome.

    @zooba zooba reopened this Oct 23, 2018
    @zooba zooba removed their assignment Oct 23, 2018
    @zooba
    Copy link
    Member

    zooba commented Oct 25, 2018

    New changeset d03b775 by Steve Dower (Tim Graham) in branch 'master':
    bpo-31047: Fix ntpath.abspath to trim ending separator (GH-10082)
    d03b775

    @zooba
    Copy link
    Member

    zooba commented Oct 25, 2018

    New changeset a7ffb66 by Steve Dower in branch '3.7':
    [3.7] bpo-31047: Fix ntpath.abspath to trim ending separator (GH-10082)
    a7ffb66

    @zooba
    Copy link
    Member

    zooba commented Oct 25, 2018

    New changeset 4aa1fda by Steve Dower in branch '3.6':
    bpo-31047: Fix ntpath.abspath to trim ending separator (GH-10082)
    4aa1fda

    @zooba
    Copy link
    Member

    zooba commented Oct 25, 2018

    Thanks, Tim!

    @zooba zooba closed this as completed Oct 25, 2018
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants