summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorJános Kukovecz <kukoveczjanos@gmail.com>2023-07-26 19:44:55 (GMT)
committerGitHub <noreply@github.com>2023-07-26 19:44:55 (GMT)
commite7e6e4b035f51ab4a962b45a957254859f264f4f (patch)
tree85f651d2d1252a9e60d30b214cd212ffdbd91b17 /Lib
parent6d5b6e71c87fca7c5c26f5dd8f325087962215cc (diff)
downloadcpython-e7e6e4b035f51ab4a962b45a957254859f264f4f.zip
cpython-e7e6e4b035f51ab4a962b45a957254859f264f4f.tar.gz
cpython-e7e6e4b035f51ab4a962b45a957254859f264f4f.tar.bz2
gh-105002: [pathlib] Fix relative_to with walk_up=True using ".." (#107014)
It makes sense to raise an Error because ".." can not be resolved and the current working directory is unknown.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/pathlib.py6
-rw-r--r--Lib/test/test_pathlib.py12
2 files changed, 16 insertions, 2 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 8ff4d4e..c83cf3d 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -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)
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 78948e3..5789a93 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -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