mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
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 f32e6b48d1
)
Co-authored-by: Barney Gale <barney.gale@gmail.com>
This commit is contained in:
parent
9cdfd1b01a
commit
b382bf50c5
3 changed files with 9 additions and 0 deletions
|
@ -463,6 +463,9 @@ class _BasePurePathTest(object):
|
|||
self.assertEqual(par[0], P('/a/b'))
|
||||
self.assertEqual(par[1], P('/a'))
|
||||
self.assertEqual(par[2], P('/'))
|
||||
self.assertEqual(par[-1], P('/'))
|
||||
self.assertEqual(par[-2], P('/a'))
|
||||
self.assertEqual(par[-3], P('/a/b'))
|
||||
self.assertEqual(par[0:1], (P('/a/b'),))
|
||||
self.assertEqual(par[:2], (P('/a/b'), P('/a')))
|
||||
self.assertEqual(par[:-1], (P('/a/b'), P('/a')))
|
||||
|
@ -470,6 +473,8 @@ class _BasePurePathTest(object):
|
|||
self.assertEqual(par[::2], (P('/a/b'), P('/')))
|
||||
self.assertEqual(par[::-1], (P('/'), P('/a'), P('/a/b')))
|
||||
self.assertEqual(list(par), [P('/a/b'), P('/a'), P('/')])
|
||||
with self.assertRaises(IndexError):
|
||||
par[-4]
|
||||
with self.assertRaises(IndexError):
|
||||
par[3]
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue