]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105002: [pathlib] Fix relative_to with walk_up=True using ".." (#107014)
authorJános Kukovecz <kukoveczjanos@gmail.com>
Wed, 26 Jul 2023 19:44:55 +0000 (21:44 +0200)
committerGitHub <noreply@github.com>
Wed, 26 Jul 2023 19:44:55 +0000 (20:44 +0100)
It makes sense to raise an Error because ".." can not
be resolved and the current working directory is unknown.

Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/NEWS.d/next/Library/2023-07-22-12-53-53.gh-issue-105002.gkfsW0.rst [new file with mode: 0644]

index 8ff4d4ea19168f6aeb002e61a87eb96f17a1abfd..c83cf3d2ef696d41049a6c7653a939a3181b9e40 100644 (file)
@@ -633,10 +633,12 @@ class PurePath:
         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)
 
index 78948e3b72032098c17492a2faad871f903b6e5f..5789a932c590376bc9b482bc67288a396fbde8ad 100644 (file)
@@ -693,8 +693,14 @@ class PurePathTest(unittest.TestCase):
         self.assertRaises(ValueError, p.relative_to, P('a/b/c'))
         self.assertRaises(ValueError, p.relative_to, P('a/c'))
         self.assertRaises(ValueError, p.relative_to, P('/a'))
+        self.assertRaises(ValueError, p.relative_to, P("../a"))
+        self.assertRaises(ValueError, p.relative_to, P("a/.."))
+        self.assertRaises(ValueError, p.relative_to, P("/a/.."))
         self.assertRaises(ValueError, p.relative_to, P('/'), walk_up=True)
         self.assertRaises(ValueError, p.relative_to, P('/a'), walk_up=True)
+        self.assertRaises(ValueError, p.relative_to, P("../a"), walk_up=True)
+        self.assertRaises(ValueError, p.relative_to, P("a/.."), walk_up=True)
+        self.assertRaises(ValueError, p.relative_to, P("/a/.."), walk_up=True)
         p = P('/a/b')
         self.assertEqual(p.relative_to(P('/')), P('a/b'))
         self.assertEqual(p.relative_to('/'), P('a/b'))
@@ -723,8 +729,14 @@ class PurePathTest(unittest.TestCase):
         self.assertRaises(ValueError, p.relative_to, P())
         self.assertRaises(ValueError, p.relative_to, '')
         self.assertRaises(ValueError, p.relative_to, P('a'))
+        self.assertRaises(ValueError, p.relative_to, P("../a"))
+        self.assertRaises(ValueError, p.relative_to, P("a/.."))
+        self.assertRaises(ValueError, p.relative_to, P("/a/.."))
         self.assertRaises(ValueError, p.relative_to, P(''), walk_up=True)
         self.assertRaises(ValueError, p.relative_to, P('a'), walk_up=True)
+        self.assertRaises(ValueError, p.relative_to, P("../a"), walk_up=True)
+        self.assertRaises(ValueError, p.relative_to, P("a/.."), walk_up=True)
+        self.assertRaises(ValueError, p.relative_to, P("/a/.."), walk_up=True)
 
     def test_is_relative_to_common(self):
         P = self.cls
diff --git a/Misc/NEWS.d/next/Library/2023-07-22-12-53-53.gh-issue-105002.gkfsW0.rst b/Misc/NEWS.d/next/Library/2023-07-22-12-53-53.gh-issue-105002.gkfsW0.rst
new file mode 100644 (file)
index 0000000..b4c133a
--- /dev/null
@@ -0,0 +1,3 @@
+Fix invalid result from :meth:`PurePath.relative_to` method when attempting to walk
+a "``..``" segment in *other* with *walk_up* enabled. A :exc:`ValueError` exception
+is now raised in this case.