Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged

Conversation

@kukovecz
Copy link
Contributor

@kukoveczkukovecz commentedJul 22, 2023
edited
Loading

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.

@bedevere-bot
Copy link

Most changes to Pythonrequire a NEWS entry.

Please add it using theblurb_it web app or theblurb command-line tool.

kukovecz reacted with thumbs up emoji

@ghost
Copy link

ghost commentedJul 22, 2023
edited by ghost
Loading

All commit authors signed the Contributor License Agreement.
CLA signed

@kukovecz
Copy link
ContributorAuthor

CC:@pablogsal

@barneygale
Copy link
Contributor

barneygale commentedJul 22, 2023
edited
Loading

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 setwalk_up=False (the default), we shouldn't get an error about the '..' segment not being walkable, because we haven't asked to walk it.

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)

@kukoveczkukoveczforce-pushed thegh-105002-pathlib-relative-to branch from7098809 to77e9b5bCompareJuly 23, 2023 14:13
@kukovecz
Copy link
ContributorAuthor

Thank you@barneygale for the fast reponse! Absolutely makes sense, I have applied your requested changes.

@barneygale
Copy link
Contributor

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 setwalk_up=True. I think that's important to mention in the news entry.

@kukoveczkukoveczforce-pushed thegh-105002-pathlib-relative-to branch from77e9b5b to0eb590eCompareJuly 24, 2023 07:48
@kukovecz
Copy link
ContributorAuthor

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 usedother as relative path in the new test asserts. Now maybe a little bit overdone because for both relative and absolute test cases the I have done some asserts for theother to be relative, absolute, with and withoutwalk_up=True, but hopefully now all things are covered.

It makes sense to raise an Error because ".." can notbe resolved and the current working directory is unknown.
@kukoveczkukoveczforce-pushed thegh-105002-pathlib-relative-to branch from0eb590e to2c6b18cCompareJuly 25, 2023 07:33
Copy link
Contributor

@barneygalebarneygale left a 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!

@miss-islington
Copy link
Contributor

Thanks@kukovecz for the PR, and@barneygale for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-107315 is a backport of this pull request to the3.12 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestJul 26, 2023
…." (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>
@bedevere-botbedevere-bot removed the needs backport to 3.12only security fixes labelJul 26, 2023
barneygale pushed a commit that referenced this pull requestJul 26, 2023
….." (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>
@barneygale
Copy link
Contributor

@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.

@kukoveczkukovecz deleted the gh-105002-pathlib-relative-to branchJuly 26, 2023 20:30
@kukovecz
Copy link
ContributorAuthor

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 reacted with heart emoji

@barneygale
Copy link
Contributor

barneygale commentedJul 26, 2023
edited
Loading

No worries, it didn't make this review any harder! Just a tip for any future PRs you might make :-)

kukovecz reacted with thumbs up emoji

jtcave pushed a commit to jtcave/cpython that referenced this pull requestJul 27, 2023
…." (python#107014)It makes sense to raise an Error because ".." can notbe resolved and the current working directory is unknown.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@barneygalebarneygalebarneygale approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@kukovecz@bedevere-bot@barneygale@miss-islington

[8]ページ先頭

©2009-2025 Movatter.jp