Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove redundant check
  • Loading branch information
nineteendo committed Mar 27, 2024
commit d83743d05cad06c6ebda2d748806142f163a5e14
18 changes: 10 additions & 8 deletions Lib/posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,23 +545,25 @@ def relpath(path, start=None):

def commonpath(paths):
"""Given a iterable of path names, returns the longest common sub-path."""
_, roots, tails = zip(*map(splitroot, paths))
if not roots:
raise ValueError('commonpath() arg is an empty iterable')
try:
# Raises TypeError if paths is not iterable
_, roots, tails = zip(*map(splitroot, paths))
except ValueError:
raise ValueError('commonpath() arg is an empty iterable') from None

try:
prefix = min(roots)
root = min(roots)
except (TypeError, AttributeError):
genericpath._check_arg_types('commonpath', *(
# Can't use paths, can be an iterable
root + tail for root, tail in zip(roots, tails)
))
raise

if not prefix and max(roots):
if not root and max(roots):
raise ValueError("Can't mix absolute and relative paths")

if isinstance(prefix, bytes):
if isinstance(root, bytes):
sep = b'/'
curdir = b'.'
else:
Expand All @@ -575,6 +577,6 @@ def commonpath(paths):
s2 = max(split_paths)
for i, c in enumerate(s1):
if c != s2[i]:
return prefix + sep.join(s1[:i])
return root + sep.join(s1[:i])

return prefix + sep.join(s1)
return root + sep.join(s1)
2 changes: 1 addition & 1 deletion Lib/test/test_posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ def check_error(exc, paths):
self.assertRaises(TypeError, posixpath.commonpath,
['usr/lib/', b'/usr/lib/python3'])

# gh-117201: `posixpath.commonpath` doesn't handle leading `//` properly
# gh-117201: Handle leading `//` for `posixpath.commonpath`
check(['//foo/bar', '//foo/baz'], '//foo')


Expand Down