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
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 5 additions & 6 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,23 +831,22 @@ def relpath(path, start=None):
raise


# Return the longest common sub-path of the sequence of paths given as input.
# Return the longest common sub-path of the iterable of paths given as input.
# The function is case-insensitive and 'separator-insensitive', i.e. if the
# only difference between two paths is the use of '\' versus '/' as separator,
# they are deemed to be equal.
#
# However, the returned path will have the standard '\' separator (even if the
# given paths had the alternative '/' separator) and will have the case of the
# first path given in the sequence. Additionally, any trailing separator is
# first path given in the iterable. Additionally, any trailing separator is
# stripped from the returned path.

def commonpath(paths):
"""Given a sequence of path names, returns the longest common sub-path."""

"""Given an iterable of path names, returns the longest common sub-path."""
paths = tuple(map(os.fspath, paths))
if not paths:
raise ValueError('commonpath() arg is an empty sequence')
raise ValueError('commonpath() arg is an empty iterable')

paths = tuple(map(os.fspath, paths))
if isinstance(paths[0], bytes):
sep = b'\\'
altsep = b'/'
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,11 +871,14 @@ def check_error(exc, paths):
self.assertRaises(exc, ntpath.commonpath,
[os.fsencode(p) for p in paths])

self.assertRaises(TypeError, ntpath.commonpath, None)
self.assertRaises(ValueError, ntpath.commonpath, [])
self.assertRaises(ValueError, ntpath.commonpath, iter([]))
check_error(ValueError, ['C:\\Program Files', 'Program Files'])
check_error(ValueError, ['C:\\Program Files', 'C:Program Files'])
check_error(ValueError, ['\\Program Files', 'Program Files'])
check_error(ValueError, ['Program Files', 'C:\\Program Files'])

check(['C:\\Program Files'], 'C:\\Program Files')
check(['C:\\Program Files', 'C:\\Program Files'], 'C:\\Program Files')
check(['C:\\Program Files\\', 'C:\\Program Files'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Raise TypeError for non-sequences for :func:`ntpath.commonpath`.