Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 6 additions & 3 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,16 +860,19 @@ def commonpath(paths):
drivesplits = [splitroot(p.replace(altsep, sep).lower()) for p in paths]
split_paths = [p.split(sep) for d, r, p in drivesplits]

if len({r for d, r, p in drivesplits}) != 1:
raise ValueError("Can't mix absolute and relative 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, r, p in drivesplits}) != 1:
raise ValueError("Paths don't have the same drive")

drive, root, path = splitroot(paths[0].replace(altsep, sep))
if len({r for d, r, p in drivesplits}) != 1:
if drive:
raise ValueError("Can't mix absolute and relative paths")
else:
raise ValueError("Can't mix rooted and not-rooted paths")

common = path.split(sep)
common = [c for c in common if c and c != curdir]

Expand Down
26 changes: 17 additions & 9 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,18 +866,25 @@ def test_commonpath(self):
def check(paths, expected):
tester(('ntpath.commonpath(%r)' % paths).replace('\\\\', '\\'),
expected)
def check_error(exc, paths):
self.assertRaises(exc, ntpath.commonpath, paths)
self.assertRaises(exc, ntpath.commonpath,
def check_error(exc, exp, paths):
self.assertRaisesRegex(exc, exp, ntpath.commonpath, paths)
self.assertRaisesRegex(exc, exp, 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'])

# gh-117381: Logical error messages, feel free to change drive error
# to mix error
check_error(ValueError, "Paths don't have the same drive", ['C:\\Program Files', '\\Program Files'])
Comment thread
nineteendo marked this conversation as resolved.
Outdated
check_error(ValueError, "Paths don't have the same drive", ['C:\\Program Files', 'Program Files'])
check_error(ValueError, "Can't mix absolute and relative paths", ['C:\\Program Files', 'C:Program Files'])
check_error(ValueError, "Paths don't have the same drive", ['C:\\Program Files', 'D:Program Files'])
check_error(ValueError, "Paths don't have the same drive", ['C:Program Files', '\\Program Files'])
check_error(ValueError, "Paths don't have the same drive", ['C:Program Files', 'Program Files'])
check_error(ValueError, "Can't mix rooted and not-rooted paths", ['\\Program Files', 'Program Files'])
check_error(ValueError, "Paths don't have the same drive", ['Program Files', 'C:\\Program Files'])

check(['C:\\Program Files'], 'C:\\Program Files')
check(['C:\\Program Files', 'C:\\Program Files'], 'C:\\Program Files')
Expand Down Expand Up @@ -905,7 +912,8 @@ def check_error(exc, paths):
check(['c:/program files/bar', 'C:\\Program Files\\Foo'],
'c:\\program files')

check_error(ValueError, ['C:\\Program Files', 'D:\\Program Files'])
check_error(ValueError, "Paths don't have the same drive", ['C:\\Program Files', 'D:\\Program Files'])
Comment thread
nineteendo marked this conversation as resolved.
Outdated
check_error(ValueError, "Paths don't have the same drive", ['C:Program Files', 'D:Program Files'])

check(['spam'], 'spam')
check(['spam', 'spam'], 'spam')
Expand All @@ -919,7 +927,7 @@ def check_error(exc, paths):

check([''], '')
check(['', 'spam\\alot'], '')
check_error(ValueError, ['', '\\spam\\alot'])
check_error(ValueError, "Can't mix rooted and not-rooted paths", ['', '\\spam\\alot'])

self.assertRaises(TypeError, ntpath.commonpath,
[b'C:\\Program Files', 'C:\\Program Files\\Foo'])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix error message for :func:`ntpath.commonpath`.