Skip to content
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

pathlib.PurePath.relative_to(walk_up=True) mishandles '..' components #105002

Closed
barneygale opened this issue May 26, 2023 · 2 comments
Closed

pathlib.PurePath.relative_to(walk_up=True) mishandles '..' components #105002

barneygale opened this issue May 26, 2023 · 2 comments
Labels
3.12 bugs and security fixes 3.13 new features, bugs and security fixes topic-pathlib type-bug An unexpected behavior, bug, or error

Comments

@barneygale
Copy link
Contributor

barneygale commented May 26, 2023

pathlib.PurePath.relative_to(other, walk_up=True) doesn't handle '..' segments in its other argument correctly:

>>> from pathlib import PurePath, Path
>>> Path.cwd()
PosixPath('/home/barney/projects/cpython')
>>> PurePath('a/b').relative_to('a/..', walk_up=True)
PurePosixPath('../b')
# expected: ValueError (ideal world: 'a/b')
>>> PurePath('a/b').relative_to('a/../..', walk_up=True)
PurePosixPath('../../b')
# expected: ValueError (ideal world: 'cpython/a/b')

PurePath objects do not know the current working directory, nor can they safely eliminate .. segments without resolving symlinks, so I think raising ValueError is the only reasonable thing to do.

Linked PRs

@barneygale barneygale added type-bug An unexpected behavior, bug, or error 3.12 bugs and security fixes topic-pathlib 3.13 new features, bugs and security fixes labels May 26, 2023
@barneygale
Copy link
Contributor Author

barneygale commented May 27, 2023

@domragusa FYI! A patch like this should do the trick:

diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 8cb5279d73..e6bf388f1f 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -602,6 +602,8 @@ 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 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")

kukovecz added a commit to kukovecz/cpython that referenced this issue 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.
kukovecz added a commit to kukovecz/cpython that referenced this issue Jul 23, 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.
kukovecz added a commit to kukovecz/cpython that referenced this issue Jul 24, 2023
It makes sense to raise an Error because ".." can not
be resolved and the current working directory is unknown.
kukovecz added a commit to kukovecz/cpython that referenced this issue Jul 25, 2023
It makes sense to raise an Error because ".." can not
be resolved and the current working directory is unknown.
barneygale pushed a commit that referenced this issue Jul 26, 2023
…07014)

It makes sense to raise an Error because ".." can not
be resolved and the current working directory is unknown.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue 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 <kukoveczjanos@gmail.com>
barneygale pushed a commit that referenced this issue 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 <kukoveczjanos@gmail.com>
@barneygale
Copy link
Contributor Author

Resolved - thanks @kukovecz!

jtcave pushed a commit to jtcave/cpython that referenced this issue 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
Labels
3.12 bugs and security fixes 3.13 new features, bugs and security fixes topic-pathlib type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant