Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
bedevere-bot commentedJul 22, 2023
Most changes to Pythonrequire a NEWS entry. Please add it using theblurb_it web app or theblurb command-line tool. |
ghost commentedJul 22, 2023 • edited by ghost
Loading Uh oh!
There was an error while loading.Please reload this page.
edited by ghost
Uh oh!
There was an error while loading.Please reload this page.
CC:@pablogsal |
barneygale commentedJul 22, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 (mostrecentcalllast):File"<stdin>",line1,in<module>File"/home/barney/projects/cpython/Lib/pathlib.py",line637,inrelative_toraiseValueError(f"'..' segment in{str(other)!r} cannot be walked")ValueError:'..'segmentin'a/b/..'cannotbewalked When we set Something like this might work better: diff --git a/Lib/pathlib.py b/Lib/pathlib.pyindex 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 to77e9b5bCompareThank 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 to0eb590eCompareThank 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 OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
It makes sense to raise an Error because ".." can notbe resolved and the current working directory is unknown.
0eb590e to2c6b18cCompareThere 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. |
bedevere-bot commentedJul 26, 2023
GH-107315 is a backport of this pull request to the3.12 branch. |
…." (pythonGH-107014)It makes sense to raise an Error because ".." can notbe resolved and the current working directory is unknown.(cherry picked from commite7e6e4b)Co-authored-by: János Kukovecz <kukoveczjanos@gmail.com>
….." (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 notbe resolved and the current working directory is unknown.(cherry picked from commite7e6e4b)Co-authored-by: János Kukovecz <kukoveczjanos@gmail.com>
@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. |
barneygale commentedJul 26, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 notbe resolved and the current working directory is unknown.
Uh oh!
There was an error while loading.Please reload this page.
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.