Skip to content
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

pathlib.Path.absolute() mishandles drive-relative Windows paths #100809

Closed
barneygale opened this issue Jan 6, 2023 · 1 comment
Closed

pathlib.Path.absolute() mishandles drive-relative Windows paths #100809

barneygale opened this issue Jan 6, 2023 · 1 comment
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes topic-pathlib type-bug An unexpected behavior, bug, or error

Comments

@barneygale
Copy link
Contributor

barneygale commented Jan 6, 2023

Windows has one current directory per drive, and supports drive-relative paths like 'X:' and 'X:foo.txt'. This makes a conversion from relative to absolute paths more complicated than simply prepending a (single) current directory.

It's correctly handled in ntpath.abspath() by calling NT's GetFullPathNameW() function. But in pathlib we simply prepend os.getcwd(), leading to incorrect results:

>>> import os, nt, pathlib
>>> os.chdir('Z:/build')
>>> os.chdir('C:/')
>>> os.path.abspath('Z:')
'Z:\\build'
>>> nt._getfullpathname('Z:')
'Z:\\build'
>>> pathlib.Path('Z:').absolute()
WindowsPath('Z:')

This bug is present in all versions of CPython pathlib. We can't fix it by calling abspath() because it will also normalize the path, eliding '..' parts.

Linked PRs

@barneygale barneygale added type-bug An unexpected behavior, bug, or error topic-pathlib 3.11 only security fixes 3.10 only security fixes 3.12 bugs and security fixes labels Jan 6, 2023
barneygale added a commit to barneygale/cpython that referenced this issue Jan 6, 2023
….absolute()

If it's available, use `nt._getfullpathname()` to retrieve an absolute
path. This allows paths such as 'X:' to be made absolute even when
`os.getcwd()` returns a path on another drive. It follows the behaviour of
`os.path.abspath()`, except that no path normalisation is performed.
barneygale added a commit to barneygale/cpython that referenced this issue Feb 16, 2023
zooba pushed a commit that referenced this issue Feb 17, 2023
…ute() (GH-100812)

Resolving the drive independently uses the OS API, which ensures it starts from the current directory on that drive.
barneygale added a commit to barneygale/cpython that referenced this issue Feb 17, 2023
…ib.Path.absolute() (pythonGH-100812)

Resolving the drive independently uses the OS API, which ensures it starts from the current directory on that drive..
(cherry picked from commit 072011b)

Co-authored-by: Barney Gale <barney.gale@gmail.com>
@barneygale
Copy link
Contributor Author

Fixed in #100812 (3.12), not backported due to interactions with other recent pathlib work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes topic-pathlib type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant