-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Add support for Path.absolute() #73874
Comments
Method absolute of Path objects lacked documentation, proposed PR adds relevant method to docs. |
As brought up on the PR, it turns out Path.absolute() is extremely under-tested. Perhaps we should deprecate Path.absolute() instead of document it and properly test it (and the testing will be necessary to move forward with the documentation)? Path.resolve() handles absolute paths already while also resolving '.' and '..': https://docs.python.org/3/library/pathlib.html#pathlib.Path.resolve. It also works with non-existent paths so unless there's some performance issue I'm not aware of for resolving '.' and '..', then I say we deprecate Path.absolute(). |
I've closed the PR on GitHub until we decide whether we just want to deprecate Path.absolute() in favour of Path.resolve(). |
I'm still thinking about this but I'm still leaning towards deprecating pathlib.absolute(). |
resolve() can't replace absolute(). They serve different purposes. Sometimes one wants an absolute path, but without resolving symbolic links. absolute() processes a path as a string, which will continue to be true if it's updated to call nt._getfullpathname (GetFullPathName) on Windows. OTOH, resolve() can outright fail on Windows. I can write up a list of examples (I can think of 5 or 6 unhandled error codes), but it's not directly relevant to this issue. |
I know it has it's uses (avoiding stat calls is one of them), but it is still undocumented, untested, and has two comments in it saying it needs work. Because of all that it might as well not exist since it doesn't meet our standards of quality. If someone wants to fix all those issues then we can properly document it as supported, but if no one is willing to then I don't think we should leave unsupported code lying around that people might discover through dir(). And it doesn't serve a _different_ purpose compared to resolve(), it serves a _subset_ of resolve()'s purpose since resolve() calls absolute() unconditionally. |
What's the rationale for not calling self._flavour.pathmod.abspath() to implement absolute()? For example: >>> p = pathlib.Path('C:/con')
>>> p._flavour.pathmod.abspath(p)
'\\\\.\\con'
>>> p._from_parts((p._flavour.pathmod.abspath(p),), init=False)
WindowsPath('//./con/') That's almost right except for an unrelated problem that pathlib shouldn't append a trailing slash for \\.\ local device paths. Doing so creates a different path, which may be invalid. \\.\con is a symbolic link to \Device\ConDrv\Console, and adding a trailing backslash after the "Console" filename is invalid. An example where the resulting path is valid but wrong is the volume device \\.\C:, which is a link to something like \Device\HarddiskVolume2. Appending a backslash refers to the root directory of the file system on the volume. |
"What's the rationale for not calling self._flavour.pathmod.abspath() to implement absolute()?" Beats me. :) It's just how Antoine wrote it and that's all I know. |
posixpath.abspath() collapses "<path>/<symlink>/.." to "<path>", this is not correct. Not sure about ntpath.abspath(). |
I feel this is reasonable limitation, and expected behavior for some cases. |
I have opened https://bugs.python.org/issue39090 to track updating the pathlib docs to have a section on getting the absolute path in various ways along with what the trade-offs are for each approach. |
New PR up here: #26153 The correspondence between
Differences:
|
Hi - please could a core dev review PR 26153? It adds documentation and tests for Path.absolute(). Thank you! |
Now that GH 26153 is merged, I think this bug can be resolved. |
In Windows, paths that are relative to the current directory on a drive aren't resolved. The following should be resolved by the current code: >>> os.chdir('C:/Temp')
>>> pathlib.Path('C:').absolute()
WindowsPath('C:') But _from_parts() has bugs with drive-relative paths. Assuming the bugs are fixed, when a path has a drive, Path.absolute() should resolve against abspath(self.drive) instead of getcwd(). |
@eryksun I'm not seeing what's wrong with your example. Would you mind pointing out what you expect the result to be? And are you saying on Windows you have to resolve the drive separately from the working directory and then concatenate them? |
"C:" or "C:spam\\eggs" are not absolute paths. They depend on the effective working directory on the drive. An absolute path should never depend on a working directory, which can change at random. WinAPI SetEnvironmentVariableW() allows applications to set environment variables with names that begin with "=". These names are effectively reserved for special use by the OS, at least as documented. In particular, names of the form "=X:", where "X" is a drive letter, are used to store the working directory on a drive. The C runtime _[w]chdir() function sets these per-drive environment variables, as does Python's os.chdir(). As environment variables, they can be inherited by child processes. When then Windows API resolves a file path to access a file, or in GetFullPathNameW(), a drive-relative path such as "X:" or "X:spam\\eggs" is resolved against either the current working directory (if it's on the drive) or the value of the "=X:" environment variable for the drive. If the latter isn't defined, it defaults to the root directory, e.g. "X:\\". If the current working directory is on the drive, the system updates the value of the "=X:" environment variable, if it exists.
No, if self.drive is defined, then abspath(self.drive) should be called instead of getcwd(). In Windows, ntpath.abspath() calls WinAPI GetFullPathNameW(), which resolves the working directory on the drive. |
@eryksun thanks for flagging, a couple thoughts: I'd imagine that bug is reproducible with I'm planning to /experimentally/ throw away pathlib's internal |
Yes, it's a separate issue that affects the _from_parts() call in absolute(). How about designing absolute() to create a new instance from an absolute path that's created by os.path? For example:
Of course use accessor functions. |
pathlib.Path.absolute()
. #26153Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: