]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
GH-113528: Deoptimise `pathlib._abc.PurePathBase.relative_to()` (again) (#113882)
authorBarney Gale <barney.gale@gmail.com>
Tue, 9 Jan 2024 23:04:14 +0000 (23:04 +0000)
committerGitHub <noreply@github.com>
Tue, 9 Jan 2024 23:04:14 +0000 (23:04 +0000)
commitcdca0ce0ad47604b7007229415817a7a152f7f9a
tree843651d436302f0e1fba686623e86bfb48dc51f5
parent5c7bd0e39839b27bc524e1790fe4936d987f384a
GH-113528: Deoptimise `pathlib._abc.PurePathBase.relative_to()` (again) (#113882)

Restore full battle-tested implementations of `PurePath.[is_]relative_to()`. These were recently split up in 3375dfe and a15a773.

In `PurePathBase`, add entirely new implementations based on `_stack`, which itself calls `pathmod.split()` repeatedly to disassemble a path. These new implementations preserve features like trailing slashes where possible, while still observing that a `..` segment cannot be added to traverse an empty or `.` segment in *walk_up* mode. They do not rely on `parents` nor `__eq__()`, nor do they spin up temporary path objects.

Unfortunately calling `pathmod.relpath()` isn't an option, as it calls `abspath()` and in turn `os.getcwd()`, which is impure.
Lib/pathlib/__init__.py
Lib/pathlib/_abc.py