Skip to content

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

Merged
merged 1 commit into from
Jul 26, 2023

Conversation

kukovecz
Copy link
Contributor

@kukovecz kukovecz commented Jul 22, 2023

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 Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

@ghost
Copy link

ghost commented Jul 22, 2023

All commit authors signed the Contributor License Agreement.
CLA signed

@kukovecz
Copy link
Contributor Author

CC: @pablogsal

@barneygale
Copy link
Contributor

barneygale commented Jul 22, 2023

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 walk_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.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)
 

@kukovecz kukovecz force-pushed the gh-105002-pathlib-relative-to branch from 7098809 to 77e9b5b Compare July 23, 2023 14:13
@kukovecz
Copy link
Contributor Author

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

@kukovecz kukovecz force-pushed the gh-105002-pathlib-relative-to branch from 77e9b5b to 0eb590e Compare July 24, 2023 07:48
@kukovecz
Copy link
Contributor Author

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 other 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 the other to be relative, absolute, with and without walk_up=True, but hopefully now all things are covered.

It makes sense to raise an Error because ".." can not
be resolved and the current working directory is unknown.
@kukovecz kukovecz force-pushed the gh-105002-pathlib-relative-to branch from 0eb590e to 2c6b18c Compare July 25, 2023 07:33
Copy link
Contributor

@barneygale barneygale 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!

@barneygale barneygale merged commit e7e6e4b into python:main Jul 26, 2023
@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 the 3.12 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 26, 2023
…." (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]>
@bedevere-bot bedevere-bot removed the needs backport to 3.12 only security fixes label Jul 26, 2023
barneygale pushed a commit that referenced this pull request Jul 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 not
be resolved and the current working directory is unknown.
(cherry picked from commit e7e6e4b)

Co-authored-by: János Kukovecz <[email protected]>
@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.

@kukovecz kukovecz deleted the gh-105002-pathlib-relative-to branch July 26, 2023 20:30
@kukovecz
Copy link
Contributor Author

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
Copy link
Contributor

barneygale commented Jul 26, 2023

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

jtcave pushed a commit to jtcave/cpython that referenced this pull request Jul 27, 2023
…." (python#107014)

It makes sense to raise an Error because ".." can not
be resolved and the current working directory is unknown.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants