GH-104996: Implement path joining algorithm in pathlib#105484
GH-104996: Implement path joining algorithm in pathlib#105484barneygale wants to merge 3 commits intopython:mainfrom
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().rootPurePosixPath("/a").rootPurePosixPath("/a", "b").rootPurePosixPath("/a", "b", "c").rootPureWindowsPath().rootPureWindowsPath("/a").rootPureWindowsPath("/a", "b").rootPureWindowsPath("/a", "b", "c").root