-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Result of os.path.realpath("") is incorrect if cwd is on letter-less drive (\\?\-prefixed) #94696
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
Comments
|
This is not a supported use case. The Windows API doesn't support setting the working directory to be a device path (i.e. prefixed by "\\.\" or "\\?\"), even though WinAPI
Here are a couple of examples that show undefined behavior. These examples use ctypes to ensure that the demonstrated behavior is not due to Python.
>>> kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
>>> buf = (ctypes.c_wchar * 260)()
>>> kernel32.SetCurrentDirectoryW('\\\\?\\Volume{f3a6addf-0000-0000-0000-500600000000}\\')
1
>>> kernel32.GetCurrentDirectoryW(260, buf)
48
>>> buf.value
'\\\\?\\Volume{f3a6addf-0000-0000-0000-500600000000}'
>>> # The above path refers to the volume device, not the filesystem root directory.
>>> # To get the correct behavior, use a device name that ends with a colon
>>> # (e.g. "Z:" or "spam:").
>>> kernel32.DefineDosDeviceW(0, 'spam:', r'\\?\Volume{f3a6addf-0000-0000-0000-500600000000}')
1
>>> kernel32.SetCurrentDirectoryW('\\\\?\\spam:\\')
1
>>> kernel32.GetCurrentDirectoryW(260, buf)
10
>>> buf.value
'\\\\?\\spam:\\'
>>> kernel32.SetCurrentDirectoryW('\\\\?\\C:\\')
1
>>> kernel32.GetCurrentDirectoryW(260, buf)
7
>>> buf.value
'\\\\?\\C:\\'
>>> kernel32.SetCurrentDirectoryW('\\')
0
>>> ctypes.get_last_error()
123
>>> kernel32.SetCurrentDirectoryW('\\\\?\\C:\\')
1
>>> kernel32.SetCurrentDirectoryW(r'\localhost\C$')
1
>>> kernel32.GetCurrentDirectoryW(260, buf)
14
>>> buf.value
'\\\\localhost\\C$'A script is just asking for problems if it sets a device path as the working directory. There's no practical benefit. In particular, using a "\\?\" extended device path does not allow the working directory to be a long path. The static buffer that's used for the current directory is hard limited to Note that a volume device is not required to have a DOS drive-letter name to be used in a DOS path. It can be accessed seamlessly from a mounted directory in a DOS path (e.g. "C:\Mount\SpamVolume") as long as the mount point is registered as the canonical DOS name (e.g. via "mountvol.exe" or
|
This means that pip should be significantly rewritten. Note that I use hundreds of apps and tools, and Python, pip and Modelio are the only ones with issues. Should I report this as the final answer to pip? |
IIRC, in legacy installation method pip starts new Python process (i.e. |


Bug report
The dot is invalid in
\\?\-prefixed path.Dots (
.and..) are normalized by Windows API (at least on NTFS). But\\?\paths bypass normalization and so may not contain dots. See, for example, here.All later WinAPI calls with this path fail.
As a simple test,
dir \\?\Volume{<uuid>}\dir\.doesn't work whereasdir \\?\Volume{<uuid>}\dirworks.P.S.
os.path.realpath(".")gives the same invalid resultYour environment
CPython 3.10.0
Windows 10
Context
The problem arises from pip.
sys.pathcontains empty string (meaning current dir I presume).And legacy installation method resolves
sys.pathrelatively tocwd.Investigation
Here:
cpython/Lib/ntpath.py
Line 704 in 4bed0db
os.path.joinstill adds slash even if the second argument is empty. And if the path is ended with slash, dot is added somewhere below.I fixed it to:
The text was updated successfully, but these errors were encountered: