]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-84538: add strict argument to pathlib.PurePath.relative_to (GH-19813)
authordomragusa <64558788+domragusa@users.noreply.github.com>
Fri, 28 Oct 2022 23:20:14 +0000 (01:20 +0200)
committerGitHub <noreply@github.com>
Fri, 28 Oct 2022 23:20:14 +0000 (16:20 -0700)
commite089f23bbbb27a84c6354147b99f7ec897ca9925
treec90fd2f1187d725ba7839eef4cc3f1429b9fa574
parent72fa57a8fe2e9df637170dc97f994ac70931e8e9
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
Doc/library/pathlib.rst
Doc/whatsnew/3.12.rst
Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/ACKS
Misc/NEWS.d/next/Library/2020-04-30-02-15-08.bpo-40358.A4ygqe.rst [new file with mode: 0644]