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
os.path.abspath()
& posixpath.realpath()
don't work for relative paths on the other platform
#118345
Comments
If you agree I'll make a pull request. At least for |
For comparison, note that Here's an implementation of try:
from nt import _getfullpathname
except ImportError:
# Non-Windows platforms
def abspath(path):
"""Return the absolute version of a path."""
path = os.fspath(path)
if not isabs(path):
if isinstance(path, bytes):
drive = splitroot(path)[0] or b'C:'
path = join(drive, b'\\', path)
else:
drive = splitroot(path)[0] or 'C:'
path = join(drive, '\\', path)
return normpath(path)
else:
def abspath(path):
"""Return the absolute version of a path."""
try:
return _getfullpathname(normpath(path))
except (OSError, ValueError):
path = os.fspath(path)
if not isabs(path):
if isinstance(path, bytes):
cwd = os.getcwdb()
else:
cwd = os.getcwd()
cwd_drive = splitroot(cwd)[0]
path_drive = splitroot(path)[0]
if path_drive and path_drive != cwd_drive:
try:
cwd = _getfullpathname(path_drive)
except (OSError, ValueError):
pass
path = join(cwd, path)
return normpath(path) |
I improved your snippet: now it's a bit faster and works correctly when it fails to determine the current working directory. |
>>> import posixpath
>>> posixpath.realpath('foo')
'C:\\Users\\wanne/foo'
>>> posixpath.isabs('C:\\Users\\wanne/foo')
False |
ntpath.abspath()
& posixpath.abspath()
don't work for relative paths on the other platformos.path.abspath()
& posixpath.realpath()
don't work for relative paths on the other platform
Great. If Actually, the latter |
Why do you think there's a bug here? In particular APIs that end up interacting with the filesystem, like |
We're literally testing that And you would expect that
The only extra code is this function (a wrapper that adds the strict argument to def realpath(path, *, strict=False):
"""Return an absolute path."""
if strict:
raise NotImplementedError('realpath: strict unavailable on this platform')
return abspath(path) When we have a C implementation for |
The implementation IMHO rightfully assumes that Other's don't work, and often cannot work in an expected way due to the difference in path separators on Windows and POSIX.
IMHO There are no "correct" semantics when calling Note that teaching |
That still doesn't explain why we're calling these functions with relative paths in the tests. (absolute paths are fine, though)
I agree, but that's NOT what I want to change here. I want
|
I copied the fix for drive relative paths to the other pull request. If this gets rejected, we don't lose it. |
This is exactly right. Perhaps we should work towards making |
That's too strict. def abspath(path):
"""Return the absolute version of a path."""
path = os.fspath(path)
if not isabs(path):
warn("passing relative paths is deprecated", DeprecationWarning, stacklevel=2)
drive, _, path = splitroot(path)
if isinstance(path, bytes):
path = join(drive or b'C:', b'\\' + path)
else:
path = join(drive or 'C:', '\\' + path)
return normpath(path) |
Maybe, although I'm not sure if adding warnings is worth the effort because the majority of uses of these APIs is through
|
It DOES work correctly for absolute paths ('/foo/bar' for Lines 409 to 418 in f5b7e39
Lines 604 to 618 in f5b7e39
|
Again, the fallback functions I want to change are ONLY used when on a foreign platform. It's a small effort to assume the cwd is That being said, if you don't want ANY extra code, we could not fix |
Relative URLs work differently to relative paths, so users doing this are asking for trouble.
This is not going to happen. |
Sighs, that one time I waited for some sign of approval before making a pull request. |
Hmm, this might not even have been possible when not using the c implementation of Python... |
On my system (Ubuntu 22.04 LTS, Python 3.10.12 [GCC 11.4.0]) on linux), I am a POSIX user, not an NT user, but I still have a similar situation. In my case, I have imported both |
Bug report
Bug description:
Windows
Expected an absolute path:
/foo
(or/Users/wanne/foo
, which is more work). For example (when #117855 lands):Unix
Expected an absolute path:
C:\\foo
(orC:\\Users\\wanne\\foo
, which is more work for drive relative paths). For example:CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS, Windows
Linked PRs
os.path.abspath()
&posixpath.abspath()
for relative paths on the other platform #118346The text was updated successfully, but these errors were encountered: