-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-105002: [pathlib] Fix relative_to with walk_up=True using ".." #107014
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
gh-105002: [pathlib] Fix relative_to with walk_up=True using ".." #107014
Conversation
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
CC: @pablogsal |
Thanks very much for taking a look at this I think the exception message is wrong here: >>> PurePath("a/b/c").relative_to("a/b/..", walk_up=False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/barney/projects/cpython/Lib/pathlib.py", line 637, in relative_to
raise ValueError(f"'..' segment in {str(other)!r} cannot be walked")
ValueError: '..' segment in 'a/b/..' cannot be walked When we set Something like this might work better: diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 8ff4d4ea19..c83cf3d2ef 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -633,10 +633,12 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
for step, path in enumerate([other] + list(other.parents)):
if self.is_relative_to(path):
break
+ elif not walk_up:
+ raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
+ elif path.name == '..':
+ raise ValueError(f"'..' segment in {str(other)!r} cannot be walked")
else:
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
- if step and not walk_up:
- raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
parts = ['..'] * step + self._tail[len(path._tail):]
return self.with_segments(*parts)
|
7098809
to
77e9b5b
Compare
Thank you @barneygale for the fast reponse! Absolutely makes sense, I have applied your requested changes. |
Thanks for making that change! The bug seems to be reproducible with absolute paths too: >>> PurePath('/a/b').relative_to('/a/..', walk_up=True)
PurePosixPath('../b') So it would be good to add a test case for that, and adjust the news entry to remove the mention of relative paths. Also, the bug only occurs when users set |
77e9b5b
to
0eb590e
Compare
Thank you, I appreciate these suggestions! I have modified the news entry to better reflect the changes. Also, for the testing: You were right, sadly I always used |
Misc/NEWS.d/next/Library/2023-07-22-12-53-53.gh-issue-105002.gkfsW0.rst
Outdated
Show resolved
Hide resolved
It makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown.
0eb590e
to
2c6b18c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thank you!
Thanks @kukovecz for the PR, and @barneygale for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12. |
GH-107315 is a backport of this pull request to the 3.12 branch. |
…." (pythonGH-107014) It makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown. (cherry picked from commit e7e6e4b) Co-authored-by: János Kukovecz <[email protected]>
….." (GH-107014) (#107315) gh-105002: [pathlib] Fix relative_to with walk_up=True using ".." (GH-107014) It makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown. (cherry picked from commit e7e6e4b) Co-authored-by: János Kukovecz <[email protected]>
@kukovecz thanks again for this. By the way, we always squash PRs on merge, so there's no need to force-push. It can make PRs a little harder to follow for reviewers. |
Thanks @barneygale for the quick round of reviews and suggestions along the way. I was not sure of your settings / policies about updating the PR so I was trying to keep the history acceptable, manually. Maybe I should have read the contributor guide more carefully. I will take extra care next time not make the review harder that it needs to be. |
No worries, it didn't make this review any harder! Just a tip for any future PRs you might make :-) |
…." (python#107014) It makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown.
For absolute paths, this was working as intended, only new test cases were added.
For relative paths it makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown.