[Python-checkins] [3.12] gh-105002: [pathlib] Fix relative_to with walk_up=True using ".." (GH-107014) (#107315)

barneygale webhook-mailer at python.org
Wed Jul 26 16:17:35 EDT 2023


https://github.com/python/cpython/commit/4f6d7a5890760434015d9e90a6e81bf9e9b5c52f
commit: 4f6d7a5890760434015d9e90a6e81bf9e9b5c52f
branch: 3.12
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: barneygale <barney.gale at gmail.com>
date: 2023-07-26T20:17:31Z
summary:

[3.12] gh-105002: [pathlib] Fix relative_to with walk_up=True using ".." (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 e7e6e4b035f51ab4a962b45a957254859f264f4f)

Co-authored-by: János Kukovecz <kukoveczjanos at gmail.com>

files:
A Misc/NEWS.d/next/Library/2023-07-22-12-53-53.gh-issue-105002.gkfsW0.rst
M Lib/pathlib.py
M Lib/test/test_pathlib.py

diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index b99bf6e7dd240..65631b7039631 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -679,10 +679,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)
 
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 1d28a782f44ab..012dacf10ea80 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -626,8 +626,14 @@ def test_relative_to_common(self):
         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'))
@@ -656,8 +662,14 @@ def test_relative_to_common(self):
         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
index 0000000000000..b4c133a5cb124
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-07-22-12-53-53.gh-issue-105002.gkfsW0.rst
@@ -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.



More information about the Python-checkins mailing list