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: os.path.realpath returns invalid path for junction pointing to letter-less volume
Type: Stage:
Components: Windows Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, grv87, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2021-10-24 09:38 by grv87, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg404923 - (view) Author: Basil Peace (grv87) Date: 2021-10-24 09:38
If a path contains a junction pointing to a dir on a letter-less drive then `os.path.realpath` returns `Volume{<uuid>}\dir`, without `\\?\` prefix. 

This path, of course, doesn't work correctly. Actually, it looks relative.

Original issue: https://github.com/pypa/pip/issues/10597
msg404925 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-10-24 11:15
This is from checking whether the \\?\ prefix can be stripped. The _getfinalpathname() call that it makes fails with the initial winerror (ERROR_PATH_NOT_FOUND), since nt._getfinalpathname() still lacks support for volume GUID paths. In this case, it assumes the path doesn't exist and removes the prefix. 

This check should be skipped for all prefixed paths if they aren't drives or UNC shares. For example, if colon_sep (i.e. ":\\") is defined:

        # The path returned by _getfinalpathname will always start with \\?\ -
        # strip off that prefix unless it was already provided on the original
        # path.
        if not had_prefix and path.startswith(prefix):
            # For UNC paths, the prefix will be \\?\UNC\
            if path.startswith(unc_prefix):
                spath = new_unc_prefix + path[len(unc_prefix):]
            # For drive paths, the root is of the form \\?\X:\
            elif path.startswith(colon_sep, len(prefix) + 1):
                spath = path[len(prefix):]
            # For all others, the prefix must be retained.
            else:
                spath = None
            if spath is not None:
                # Ensure that the non-prefixed path resolves to the same path
                try:
                    if _getfinalpathname(spath) == path:
                        path = spath
                except OSError as ex:
                    # If the path does not exist and originally did not exist, then
                    # strip the prefix anyway.
                    if ex.winerror == initial_winerror:
                        path = spath
        return path
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89760
2021-10-24 11:15:13eryksunsetnosy: + eryksun
messages: + msg404925
2021-10-24 09:38:45grv87create