Skip to content

gh-101000: Add os.path.splitroot()#101002

Merged
AlexWaygood merged 31 commits intopython:mainfrom
barneygale:gh-101000-splitroot
Jan 27, 2023
Merged

gh-101000: Add os.path.splitroot()#101002
AlexWaygood merged 31 commits intopython:mainfrom
barneygale:gh-101000-splitroot

Conversation

@barneygale
Copy link
Contributor

@barneygale barneygale commented Jan 12, 2023

This PR introduces os.path.splitroot(). See #101000 for motivation.

In ntpath, the implementation derives from splitdrive(). The splitdrive() function now calls splitroot(), and returns drive, root + tail. Other functions now call splitroot() rather than splitdrive(). In most cases this replaces their own parsing of path roots. It also avoids adding a stack frame.

In posixpath, the normpath() function now calls splitroot() rather than parsing path roots itself.

In pathlib, path constructors now call splitroot() rather than using a slow OS-agnostic implementation. Performance:

$ ./python -m timeit -s 'from pathlib import PureWindowsPath' 'PureWindowsPath("C:/", "foo", "bar")'
50000 loops, best of 5: 6.04 usec per loop  # before
50000 loops, best of 5: 4.03 usec per loop  # after
$ ./python -m timeit -s 'from pathlib import PurePosixPath' 'PurePosixPath("/", "etc", "hosts")'
100000 loops, best of 5: 3.11 usec per loop  # before
100000 loops, best of 5: 2.77 usec per loop  # after

Future work:

  • Improve performance by using native nt._path_splitroot()

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic-pathlib type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants