summaryrefslogtreecommitdiffstats
path: root/Lib/pathlib.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-07-26 20:17:31 (GMT)
committerGitHub <noreply@github.com>2023-07-26 20:17:31 (GMT)
commit4f6d7a5890760434015d9e90a6e81bf9e9b5c52f (patch)
tree337ef4553fa2da054d1b9b607023b9841ed0e6da /Lib/pathlib.py
parent58af565c5085c427203ee424585a2c3ed0db4981 (diff)
downloadcpython-4f6d7a5890760434015d9e90a6e81bf9e9b5c52f.zip
cpython-4f6d7a5890760434015d9e90a6e81bf9e9b5c52f.tar.gz
cpython-4f6d7a5890760434015d9e90a6e81bf9e9b5c52f.tar.bz2
[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@gmail.com>
Diffstat (limited to 'Lib/pathlib.py')
-rw-r--r--Lib/pathlib.py6
1 files changed, 4 insertions, 2 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index b99bf6e..65631b7 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -679,10 +679,12 @@ class PurePath(object):
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)