Repository navigation
Add support for Path.absolute() #73874
Description
Activity
Method absolute of Path objects lacked documentation, proposed PR adds relevant method to docs.
- added3.7 (EOL)end of lifeend of lifedocsDocumentation in the Doc dirDocumentation in the Doc dir
on Mar 1, 2017 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.
- changed the title
[-]Document Path.absolute[/-][+]Add support for Path.absolute[/+]on Mar 11, 2017 - changed the title
[-]Add support for Path.absolute[/-][+]Add support for Path.absolute()[/+]on Mar 11, 2017 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.
8 remaining items
- addedtype-featureA feature request or enhancementA feature request or enhancement
on Mar 13, 2021 New PR up here: #26153
The correspondence between
pathlibandos.pathis as follows:Path.resolve()is roughlyos.path.realpath()Path.absolute()is roughlyos.path.abspath()
Differences:
resolve()always raises RuntimeError on symlink loops, whereasrealpath()either raises OSError or nothing depending on strict.absolute()doesn't normalize the path, whereasabspath()does. Normalizing without resolving symlinks can change the meaning of the path, so pathlib does the better thing here.
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?
I'm not seeing what's wrong with your example.
"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.
on Windows you have to resolve the drive separately from the
working directory and then concatenate them?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
Path('C:\\Temp', 'C:')already, right? If that's the case, should it logged as a separate issue?I'm planning to /experimentally/ throw away pathlib's internal
path parsing logic and defer toposixpath/ntpathinstead. I suspect this bug and others will be fixed by that change, but I need to see what the performance impact will be.I'd imagine that bug is reproducible with
Path('C:\\Temp', 'C:')
already, right? If that's the case, should it logged as a
separate issue?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:
join(abspath(self.drive) if self.drive else getcwd(), self)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: