This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Windows: os.path.isabs(os.path.abspath(" ")) == False
Type: Stage: resolved
Components: Windows Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Tim.Graham, eryksun, lazka, miss-islington, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords: patch

Created on 2017-07-26 16:47 by lazka, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 8544 merged pansen, 2018-07-29 09:58
PR 8549 merged miss-islington, 2018-07-29 12:47
PR 8550 merged steve.dower, 2018-07-29 13:05
PR 10082 merged Tim.Graham, 2018-10-25 13:35
PR 10096 merged steve.dower, 2018-10-25 15:33
PR 10097 merged steve.dower, 2018-10-25 15:36
Messages (11)
msg299253 - (view) Author: Christoph Reiter (lazka) * Date: 2017-07-26 16:47
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
msg299266 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-07-26 21:47
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.
msg322633 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2018-07-29 12:47
New changeset d2e902e4fb304f27e4a72356efbc1fc26be3935d by Steve Dower (Franz Wöllert) in branch 'master':
bpo-31047: Fix ntpath.abspath for invalid paths (GH-8544)
https://github.com/python/cpython/commit/d2e902e4fb304f27e4a72356efbc1fc26be3935d
msg322636 - (view) Author: miss-islington (miss-islington) Date: 2018-07-29 15:42
New changeset 5753b13cb949b939b2b29cec5e2d646f9a30db44 by Miss Islington (bot) in branch '3.7':
bpo-31047: Fix ntpath.abspath for invalid paths (GH-8544)
https://github.com/python/cpython/commit/5753b13cb949b939b2b29cec5e2d646f9a30db44
msg323229 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2018-08-07 00:08
New changeset b0bf51b32240369ccb736dc32ff82bb96f375402 by Steve Dower in branch '3.6':
bpo-31047: Fix ntpath.abspath for invalid paths (GH-8544)
https://github.com/python/cpython/commit/b0bf51b32240369ccb736dc32ff82bb96f375402
msg328322 - (view) Author: Tim Graham (Tim.Graham) * Date: 2018-10-23 15:32
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\)
msg328328 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2018-10-23 17:43
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.
msg328448 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2018-10-25 15:26
New changeset d03b7757811ae51277f8ed399a9a0fd78dfd3425 by Steve Dower (Tim Graham) in branch 'master':
bpo-31047: Fix ntpath.abspath to trim ending separator (GH-10082)
https://github.com/python/cpython/commit/d03b7757811ae51277f8ed399a9a0fd78dfd3425
msg328463 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2018-10-25 17:46
New changeset a7ffb663953bc84452af1e5f4089359d54e226b5 by Steve Dower in branch '3.7':
[3.7] bpo-31047: Fix ntpath.abspath to trim ending separator (GH-10082)
https://github.com/python/cpython/commit/a7ffb663953bc84452af1e5f4089359d54e226b5
msg328464 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2018-10-25 17:46
New changeset 4aa1fda7069642c21c1ee570c4ba44442a657e5e by Steve Dower in branch '3.6':
bpo-31047: Fix ntpath.abspath to trim ending separator (GH-10082)
https://github.com/python/cpython/commit/4aa1fda7069642c21c1ee570c4ba44442a657e5e
msg328465 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2018-10-25 17:47
Thanks, Tim!
History
Date User Action Args
2022-04-11 14:58:49adminsetgithub: 75230
2018-10-25 17:47:07steve.dowersetstatus: open -> closed
resolution: fixed
messages: + msg328465

stage: patch review -> resolved
2018-10-25 17:46:38steve.dowersetmessages: + msg328464
2018-10-25 17:46:28steve.dowersetmessages: + msg328463
2018-10-25 15:36:58steve.dowersetpull_requests: + pull_request9430
2018-10-25 15:33:35steve.dowersetpull_requests: + pull_request9429
2018-10-25 15:26:43steve.dowersetmessages: + msg328448
2018-10-25 13:35:51Tim.Grahamsetstage: needs patch -> patch review
pull_requests: + pull_request9423
2018-10-23 17:43:20steve.dowersetstatus: closed -> open
messages: + msg328328

assignee: steve.dower ->
resolution: fixed -> (no value)
stage: resolved -> needs patch
2018-10-23 15:32:35Tim.Grahamsetnosy: + Tim.Graham
messages: + msg328322
2018-08-07 00:41:07steve.dowersetstatus: open -> closed
assignee: steve.dower
resolution: fixed
stage: patch review -> resolved
2018-08-07 00:08:43steve.dowersetmessages: + msg323229
2018-07-29 15:42:20miss-islingtonsetnosy: + miss-islington
messages: + msg322636
2018-07-29 13:05:08steve.dowersetpull_requests: + pull_request8066
2018-07-29 12:47:25miss-islingtonsetpull_requests: + pull_request8065
2018-07-29 12:47:12steve.dowersetmessages: + msg322633
2018-07-29 09:58:20pansensetkeywords: + patch
stage: patch review
pull_requests: + pull_request8061
2017-07-26 21:48:00eryksunsetnosy: + eryksun
messages: + msg299266
2017-07-26 16:47:29lazkacreate