-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Some ntpath/posixpath functions unusable on foreign OS #119671
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
I'm not sure if/how to solve this. It could be considered a docs bug - perhaps we need to adjust the Alternatively we could remove the docs mention of the Lines 35 to 84 in 6ec3712
|
Also, it's worth noting that there's no problem with |
Could we maybe move the safe functions to |
Sure, but we're interested in whether functions are safe overall, not under specific conditions. |
It might be a little annoying having to import it from the other module though (and a type checker might complain about it): from ntpath import relpath # Do not import `ntpath`, use `os.path` or `ntpath.pure`.
from ntpath.pure import * Maybe we could define |
Could we discuss adding a submodule on Discourse? I was asked to not create new idea threads until 2025. |
TBH I consider this a pretty low-priority issue and not worth raising on discourse. It's been this way for a long time. If/when other core devs encounter this issue, they might search the bug tracker, arrive here, and give their tuppence. |
I don't think this is worth adding submodules. Fallback implementations or We've got little to no chance of moving any existing functions into new modules, so no compatibility issues are resolved. But I think we can justify replacing unexplained OSError/AttributeErrors with NotImplementedError (or a |
Note that even this conservative check doesn't work for diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index fccca4e066b..dd431c4dbf7 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -516,8 +516,13 @@ def relpath(path, start=None):
start = os.fspath(start)
try:
- start_tail = abspath(start).lstrip(sep)
- path_tail = abspath(path).lstrip(sep)
+ start_abs = abspath(start)
+ path_abs = abspath(path)
+ if not isabs(start_abs) or not isabs(path_abs):
+ raise ValueError("paths are not absolute")
+
+ start_tail = start_abs.lstrip(sep)
+ path_tail = path_abs.lstrip(sep)
start_list = start_tail.split(sep) if start_tail else []
path_list = path_tail.split(sep) if path_tail else []
# Work out how much of the filepath is shared by start and path. At least the submodules could make this work without breaking anyone's code. And all safe functions can be refactored to them, making the code easier to maintain. Other approaches actually seem more complicated to me. |
The
os.path
docs intro includes:But we don't explain that some of the functions in
ntpath
andposixpath
aren't suitable for usage on a "foreign" OS, e.g. that usingntpath.realpath()
from POSIX is a bad idea.These functions are safe because they do purely lexical work:
basename
commonpath
commonprefix
dirname
isabs
join
normcase
normpath
split
splitdrive
splitroot
splitext
These functions are probably unsafe because they rely on details of the host OS (e.g. via the
os
module):abspath
exists
lexists
expanduser
expandvars
getatime
getmtime
getctime
getsize
isfile
isdir
isjunction
islink
ismount
isdevdrive
realpath
relpath
samefile
sameopenfile
samestat
supports_unicode_filenames
Linked PRs
The text was updated successfully, but these errors were encountered: