Uh oh!
There was an error while loading. Please reload this page.
gh-117201: Handle leading // for posixpath.commonpath - #117334
gh-117201: Handle leading // for posixpath.commonpath#117334nineteendo wants to merge 33 commits into
// for posixpath.commonpath#117334Conversation
…into fix-commonpath
barneygale
commented
Mar 29, 2024
Could you please add test cases covering: commonpath(['//foo', '/foo', 'foo'])
commonpath(['//foo', '/foo'])
commonpath(['//foo', 'foo'])I think I'd expect all three to raise |
Uh oh!
There was an error while loading. Please reload this page.
nineteendo
commented
Mar 29, 2024
Do I have permission to change this test then to 3 slashes? In the current implementation I made sure it wouldn't break. cpython/Lib/test/test_posixpath.py Line 716 in 54f7e14 Achieving the desired behaviour would be easy: ifmin(roots) !=max(roots): # -> not all roots are the sameraiseValueErrorBut I'm not sure what the error message would have to be. |
barneygale
commented
Mar 29, 2024
Ah interesting, OK! Preserving existing behaviour for |
barneygale
commented
Mar 29, 2024
The patch seems larger than necessary to fix the bug. Could something like this work instead? diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index 4fc02be69b..4cbe1b1367 100644
--- a/Lib/posixpath.py+++ b/Lib/posixpath.py@@ -541,10 +541,9 @@ def commonpath(paths):
try:
split_paths = [path.split(sep) for path in paths]
- try:- isabs, = set(p[:1] == sep for p in paths)- except ValueError:- raise ValueError("Can't mix absolute and relative paths") from None+ prefixes = [splitroot(path)[1] for path in paths]+ if any(prefixes) != all(prefixes):+ raise ValueError("Can't mix absolute and relative paths")
split_paths = [[c for c in s if c and c != curdir] for s in split_paths]
s1 = min(split_paths)
@@ -555,7 +554,7 @@ def commonpath(paths):
common = s1[:i]
break
- prefix = sep if isabs else sep[:0]+ prefix = min(prefixes)
return prefix + sep.join(common)
except (TypeError, AttributeError):
genericpath._check_arg_types('commonpath', *paths) |
nineteendo
commented
Mar 29, 2024
I've tried to keep as much of the old structure as possible. Your code appears to do more work. |
nineteendo
commented
Mar 30, 2024
|
nineteendo
commented
Apr 1, 2024
I've made some changes to further reduce the diff. Now the fix is way more readable. |
barneygale
left a comment
There was a problem hiding this comment.
Might be faster to sort the roots up front and then check the first and last elements? (Untested).
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
nineteendo
commented
Apr 2, 2024
@barneygale, do you think this can be merged now? There don't seem to be any optimisations remaining. |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
This change can break user code which expects the result always has a single slash prefix. It is unsafe to backport it. So it should be documented as a new feature.
How does this change affect performance? Please test for short (2 items) and long (e.g. /usr/lib/**) sequences.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
nineteendo
commented
Apr 4, 2024
The speed difference is smaller for long paths, but bigger for many paths (20). See, the updated benchmark. |
erlend-aasland
commented
Apr 4, 2024
We should be careful about subtle breaking changes like this. @nineteendo: Ideally, a discussion would happen on the issue first, and then if a consensus on how to proceed has been reached, a PR can be created. Creating a lot of PRs before a decision has been reached tie up a reviewer resources, creating unneeded review churn and CI churn. |
NOTE: this is NOT a duplicate of #117202, the branch has been renamed, causing the old pull request to be closed.
Benchmark:
//forposixpath.commonpath#117201