]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-93156 - fix negative indexing into absolute `pathlib.PurePath().parents` (GH-93273)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 3 Jun 2022 21:57:54 +0000 (14:57 -0700)
committerGitHub <noreply@github.com>
Fri, 3 Jun 2022 21:57:54 +0000 (14:57 -0700)
commit9f101c23a41e739f5f80cf38419df1281835d452
treee05dc572a9c2ab713dc808a1eb063964d2ae9cb6
parente4113be63c27faf52c5ce79f1afa3b9f6e33de46
gh-93156 - fix negative indexing into absolute `pathlib.PurePath().parents` (GH-93273)

When a `_PathParents` object has a drive or a root, the length of the
object is *one less* than than the length of `self._parts`, which resulted
in an off-by-one error when `path.parents[-n]` was fed through to
`self._parts[:-n - 1]`. In particular, `path.parents[-1]` was a malformed
path object with spooky properties.

This is addressed by adding `len(self)` to negative indices.
(cherry picked from commit f32e6b48d12834ba3bde01ec21c14da33abd26d6)

Co-authored-by: Barney Gale <barney.gale@gmail.com>
Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/NEWS.d/next/Library/2022-05-26-23-10-55.gh-issue-93156.4XfDVN.rst [new file with mode: 0644]