summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authordomragusa <64558788+domragusa@users.noreply.github.com>2022-10-28 23:20:14 (GMT)
committerGitHub <noreply@github.com>2022-10-28 23:20:14 (GMT)
commite089f23bbbb27a84c6354147b99f7ec897ca9925 (patch)
treec90fd2f1187d725ba7839eef4cc3f1429b9fa574 /Doc
parent72fa57a8fe2e9df637170dc97f994ac70931e8e9 (diff)
downloadcpython-e089f23bbbb27a84c6354147b99f7ec897ca9925.zip
cpython-e089f23bbbb27a84c6354147b99f7ec897ca9925.tar.gz
cpython-e089f23bbbb27a84c6354147b99f7ec897ca9925.tar.bz2
gh-84538: add strict argument to pathlib.PurePath.relative_to (GH-19813)
By default, :meth:`pathlib.PurePath.relative_to` doesn't deal with paths that are not a direct prefix of the other, raising an exception in that instance. This change adds a *walk_up* parameter that can be set to allow for using ``..`` to calculate the relative path. example: ``` >>> p = PurePosixPath('/etc/passwd') >>> p.relative_to('/etc') PurePosixPath('passwd') >>> p.relative_to('/usr') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "pathlib.py", line 940, in relative_to raise ValueError(error_message.format(str(self), str(formatted))) ValueError: '/etc/passwd' does not start with '/usr' >>> p.relative_to('/usr', strict=False) PurePosixPath('../etc/passwd') ``` https://bugs.python.org/issue40358 Automerge-Triggered-By: GH:brettcannon
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/pathlib.rst34
-rw-r--r--Doc/whatsnew/3.12.rst5
2 files changed, 33 insertions, 6 deletions
diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst
index 4e9ea39..a6daca9 100644
--- a/Doc/library/pathlib.rst
+++ b/Doc/library/pathlib.rst
@@ -564,10 +564,10 @@ Pure paths provide the following methods and properties:
True
-.. method:: PurePath.relative_to(*other)
+.. method:: PurePath.relative_to(*other, walk_up=False)
Compute a version of this path relative to the path represented by
- *other*. If it's impossible, ValueError is raised::
+ *other*. If it's impossible, :exc:`ValueError` is raised::
>>> p = PurePosixPath('/etc/passwd')
>>> p.relative_to('/')
@@ -577,11 +577,33 @@ Pure paths provide the following methods and properties:
>>> p.relative_to('/usr')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
- File "pathlib.py", line 694, in relative_to
- .format(str(self), str(formatted)))
- ValueError: '/etc/passwd' is not in the subpath of '/usr' OR one path is relative and the other absolute.
+ File "pathlib.py", line 941, in relative_to
+ raise ValueError(error_message.format(str(self), str(formatted)))
+ ValueError: '/etc/passwd' is not in the subpath of '/usr' OR one path is relative and the other is absolute.
+
+When *walk_up* is False (the default), the path must start with *other*.
+ When the argument is True, ``..`` entries may be added to form the
+ relative path. In all other cases, such as the paths referencing
+ different drives, :exc:`ValueError` is raised.::
+
+ >>> p.relative_to('/usr', walk_up=True)
+ PurePosixPath('../etc/passwd')
+ >>> p.relative_to('foo', walk_up=True)
+ Traceback (most recent call last):
+ File "<stdin>", line 1, in <module>
+ File "pathlib.py", line 941, in relative_to
+ raise ValueError(error_message.format(str(self), str(formatted)))
+ ValueError: '/etc/passwd' is not on the same drive as 'foo' OR one path is relative and the other is absolute.
- NOTE: This function is part of :class:`PurePath` and works with strings. It does not check or access the underlying file structure.
+ .. warning::
+ This function is part of :class:`PurePath` and works with strings.
+ It does not check or access the underlying file structure.
+ This can impact the *walk_up* option as it assumes that no symlinks
+ are present in the path; call :meth:`~Path.resolve` first if
+ necessary to resolve symlinks.
+
+ .. versionadded:: 3.12
+ The *walk_up* argument (old behavior is the same as ``walk_up=False``).
.. method:: PurePath.with_name(name)
diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst
index 39ad3ac..0eb2879 100644
--- a/Doc/whatsnew/3.12.rst
+++ b/Doc/whatsnew/3.12.rst
@@ -149,6 +149,11 @@ pathlib
all file or directory names within them, similar to :func:`os.walk`.
(Contributed by Stanislav Zmiev in :gh:`90385`.)
+* Add *walk_up* optional parameter to :meth:`pathlib.PurePath.relative_to`
+ to allow the insertion of ``..`` entries in the result; this behavior is
+ more consistent with :func:`os.path.relpath`.
+ (Contributed by Domenico Ragusa in :issue:`40358`.)
+
dis
---