-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
ntpath.realpath() mishandles filenames that resemble drives #102475
Comments
Any ideas on how to go about fixing this one? Keen to work on it but it seems (with regards to the linked issue) there are other things related to those to sort out first? |
Thanks for taking a look! Essentially: I suspect the main problematic case is here: Line 697 in ccb5af7
... but there are other usages of |
Thanks for the detailed explanation. I'll have a go at it. |
Define a custom if isinstance(path, bytes):
tail = b''
sep = b'\\'
else:
tail = ''
sep = '\\'
def join(path, tail):
if path[-1:] == sep or not tail:
return path + tail
return path + sep + tail Use this function for all of the four |
I said four Lines 695 to 696 in 4d1f033
The working directory on a non-existing drive should be the root path. For example, the following result is wrong: >>> ntpath.exists('Z:')
False
>>> ntpath.realpath('Z:spam')
'Z:spam' |
Eryk, is that function necessary in all four cases? I wonder if we can guarantee that |
You've done all the work for me! |
The issue also needs tests in "Lib/test_ntpath.py".
The result from |
Is os.path.join just wrong? Is there a proper definition of a "canonical" path for Windows? If not should the Python community create a definition? It will have to takes account of namespaces, drive letters, mount points, case sensitivity etc . I think that one rule should be that If I also think that os.path.realpath( 'c:/aa.b' ) should give a similar result to os.path.realpath( 'c:/a.b' ) p.s. see also some of the comments on issue #100162 |
No, because it depends on what the file system driver (and any filter drivers) will accept/handle. And those are extensible, so we can't define it independently of knowing exactly what's on any given system (which we don't).
We can't enforce this kind of rule unless the OS does. The best we can do is be a thin wrapper over what the OS does. The problems being raised here are probably because we also try to maintain compatibility with the old days when we assumed FAT and NTFS paths were the only ones that existed. Should still be fixed, but I don't think there's a good general policy for us to adopt besides moving towards using more OS functionality and implementing less of it ourselves. |
The
realpath()
docs say:Note the word "appended". In fact,
realpath()
usesos.path.join()
to join the path segments, and as we all know/love,os.path.join()
supports resetting the drive or root, thus discarding prior parts. As a result:The text was updated successfully, but these errors were encountered: