8000 gh-117335: Handle non-iterables for `ntpath.commonpath` by nineteendo · Pull Request #117336 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-117335: Handle non-iterables for ntpath.commonpath #117336

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

Merged
merged 4 commits into from
Mar 28, 2024
Merged
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
Revert unnecessary changes
  • Loading branch information
nineteendo committed Mar 28, 2024
commit 7f6902948acb09e25f144ff5ebf2d49d7cd75cc7
49 changes: 24 additions & 25 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,7 @@ def commonpath(paths):
if not paths:
raise ValueError('commonpath() arg is an empty iterable')

path = paths[0]
if isinstance(path, bytes):
if isinstance(paths[0], bytes):
sep = b'\\'
altsep = b'/'
curdir = b'.'
Expand All @@ -859,33 +858,33 @@ def commonpath(paths):

try:
rootsplits = [splitroot(p.replace(altsep, sep).lower()) for p in paths]

# Check that all drive letters or UNC paths match. The check is made
# only now otherwise type errors for mixing strings and bytes would not
# be caught.
if len({d for d, _, _ in rootsplits}) != 1:
raise ValueError("Paths don't have the same drive")

if len({r for _, r, _ in rootsplits}) != 1:
raise ValueError("Can't mix absolute and relative paths")

drive, root, tail = splitroot(paths[0].replace(altsep, sep))
common = [c for c in tail.split(sep) if c and c != curdir]

split_paths = [
[c for c in t.split(sep) if c and c != curdir]
for _, _, t in rootsplits
]
s1 = min(split_paths)
s2 = max(split_paths)
for i, c in enumerate(s1):
if c != s2[i]:
return drive + root + sep.join(common[:i])
return drive + root + sep.join(common[:len(s1)])
except (TypeError, AttributeError):
genericpath._check_arg_types('commonpath', *paths)
raise

# Check that all drive letters or UNC paths match. The check is made only
# now otherwise type errors for mixing strings and bytes would not be
# caught.
if len({drt[0] for drt in rootsplits}) != 1:
raise ValueError("Paths don't have the same drive")

if len({drt[1] for drt in rootsplits}) != 1:
raise ValueError("Can't mix absolute and relative paths")

drive, root, tail = splitroot(path.replace(altsep, sep))
common = [c for c in tail.split(sep) if c and c != curdir]
split_paths = [
[c for c in drt[2].split(sep) if c and c != curdir]
for drt in rootsplits
]
s1 = min(split_paths)
s2 = max(split_paths)
for i, c in enumerate(s1):
if c != s2[i]:
return drive + root + sep.join(common[:i])

return drive + root + sep.join(common[:len(s1)])


try:
# The isdir(), isfile(), islink() and exists() implementations in
Expand Down
0