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

Add os.path.splitroot() function #101000

Closed
barneygale opened this issue Jan 12, 2023 · 2 comments
Closed

Add os.path.splitroot() function #101000

barneygale opened this issue Jan 12, 2023 · 2 comments
Labels
3.12 bugs and security fixes stdlib Python modules in the Lib dir topic-pathlib type-feature A feature request or enhancement

Comments

@barneygale
Copy link
Contributor

barneygale commented Jan 12, 2023

Feature or enhancement

Add a function that splits a path into a (drive, root, tail) triad:

  1. The drive part has the same meaning as in splitdrive()
  2. The root part is one of: the empty string, a forward slash, a backward slash (Windows only), or two forward slashes (POSIX only)
  3. The tail part is everything following the root.

Similarly to splitdrive(), a splitroot() function would ensure that drive + root + tail is the same as the input path.

Pitch

The extra level of detail reflects an extra step in the Windows 'current path' hierarchy -- Windows has both a 'current drive', and a 'current directory' for one or more drives, which results in several kinds of non-absolute paths, e.g. 'foo/bar', '/foo/bar', 'X:foo/bar'

This three-part model is used successfully by pathlib, which exposes root as an attribute, and combines drive + root as an attribute called anchor. The anchor has useful properties, e.g. comparing two paths anchors can tell us whether a relative_to() operation is possible.

Pathlib has its own implementation of splitroot(), but its performance is hamstrung by its need for OS-agnosticism. By moving the implementation into ntpath and posixpath we can take advantage of OS-specific rules to improve pathlib performance.

Previous discussion

Linked PRs

@barneygale barneygale added the type-feature A feature request or enhancement label Jan 12, 2023
barneygale added a commit to barneygale/cpython that referenced this issue Jan 12, 2023
@eryksun eryksun added stdlib Python modules in the Lib dir 3.12 bugs and security fixes topic-pathlib labels Jan 13, 2023
AlexWaygood added a commit to barneygale/cpython that referenced this issue Jan 22, 2023
AlexWaygood added a commit that referenced this issue Jan 27, 2023
Co-authored-by: Eryk Sun <eryksun@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@AlexWaygood
Copy link
Member

Added in #101002. Thanks @barneygale 🎉🥳

@barneygale
Copy link
Contributor Author

Thank you very much Alex! \o/

mdboom pushed a commit to mdboom/cpython that referenced this issue Jan 31, 2023
Co-authored-by: Eryk Sun <eryksun@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes stdlib Python modules in the Lib dir topic-pathlib type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants