-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
GH-104996: Implement path joining algorithm in pathlib #105484
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
Conversation
Copy the `ntpath.join()` algorithm into pathlib and adjust it to remove string concatenation. The resulting drive, root and tail are stored on the path object without creating an intermediate joined path.
@eryksun do you think you could you review this please? It's a variant of the |
I'm not thrilled about duplicating the code from def join(path, *paths):
paths = [os.fspath(path), *paths]
if isinstance(paths[0], bytes):
path = b''
sep = b'\\'
seps = b'\\/'
colon = b':'
else:
path = ''
sep = '\\'
seps = '\\/'
colon = ':'
try:
drive, root, path_list = splitseq(paths)
for p in path_list:
if path and path[-1] not in seps:
path += sep
path += p
# If needed, add a separator between a UNC drive and path.
if path and not root and drive and drive[-1:] != colon:
return drive + sep + path
return drive + root + path
except (TypeError, AttributeError, BytesWarning):
genericpath._check_arg_types('join', *paths)
raise |
The only problem I see there is that |
I think a better function name would help. Maybe
|
Maybe |
I'm withdrawing this PR as it bakes elements of pathlib's current normalisation logic into the path parsing/joining, and the overlap precludes user customisation and some other optimisations I have in mind. |
Copy the
ntpath.join()
algorithm into pathlib and adjust it to remove string concatenation. The resulting drive, root and tail are stored on the path object without creating an intermediate joined path.Timings in microseconds:
PurePosixPath().root
PurePosixPath("/a").root
PurePosixPath("/a", "b").root
PurePosixPath("/a", "b", "c").root
PureWindowsPath().root
PureWindowsPath("/a").root
PureWindowsPath("/a", "b").root
PureWindowsPath("/a", "b", "c").root