bpo-35498: Added slice support to PathLib parents attribute. (GH-11165)

Added slice support to the `pathlib.Path.parents` sequence. For a `Path` `p`, slices of `p.parents` should return the same thing as slices of `tuple(p.parents)`.
This commit is contained in:
Joshua Cannon 2020-11-20 09:40:39 -06:00 committed by GitHub
parent d20b7ed9c1
commit 4520584483
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 0 deletions

View File

@ -336,6 +336,8 @@ Pure paths provide the following methods and properties:
>>> p.parents[2]
PureWindowsPath('c:/')
.. versionchanged:: 3.10
Slice support was added.
.. data:: PurePath.parent

View File

@ -244,6 +244,12 @@ descriptors without copying between kernel address space and user
address space, where one of the file descriptors must refer to a
pipe. (Contributed by Pablo Galindo in :issue:`41625`.)
pathlib
-------
Added slice support to :meth:`~pathlib.Path.parents`.
(Contributed by Joshua Cannon in :issue:`35498`)
py_compile
----------

View File

@ -630,6 +630,8 @@ class _PathParents(Sequence):
return len(self._parts)
def __getitem__(self, idx):
if isinstance(idx, slice):
return tuple(self[i] for i in range(*idx.indices(len(self))))
if idx < 0 or idx >= len(self):
raise IndexError(idx)
return self._pathcls._from_parsed_parts(self._drv, self._root,

View File

@ -440,6 +440,12 @@ class _BasePurePathTest(object):
self.assertEqual(par[0], P('a/b'))
self.assertEqual(par[1], P('a'))
self.assertEqual(par[2], P('.'))
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')))
self.assertEqual(par[1:], (P('a'), P('.')))
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[-1]
@ -454,6 +460,12 @@ class _BasePurePathTest(object):
self.assertEqual(par[0], P('/a/b'))
self.assertEqual(par[1], P('/a'))
self.assertEqual(par[2], P('/'))
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')))
self.assertEqual(par[1:], (P('/a'), P('/')))
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[3]
@ -905,6 +917,12 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
self.assertEqual(len(par), 2)
self.assertEqual(par[0], P('z:a'))
self.assertEqual(par[1], P('z:'))
self.assertEqual(par[0:1], (P('z:a'),))
self.assertEqual(par[:-1], (P('z:a'),))
self.assertEqual(par[:2], (P('z:a'), P('z:')))
self.assertEqual(par[1:], (P('z:'),))
self.assertEqual(par[::2], (P('z:a'),))
self.assertEqual(par[::-1], (P('z:'), P('z:a')))
self.assertEqual(list(par), [P('z:a'), P('z:')])
with self.assertRaises(IndexError):
par[2]
@ -913,6 +931,12 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
self.assertEqual(len(par), 2)
self.assertEqual(par[0], P('z:/a'))
self.assertEqual(par[1], P('z:/'))
self.assertEqual(par[0:1], (P('z:/a'),))
self.assertEqual(par[0:-1], (P('z:/a'),))
self.assertEqual(par[:2], (P('z:/a'), P('z:/')))
self.assertEqual(par[1:], (P('z:/'),))
self.assertEqual(par[::2], (P('z:/a'),))
self.assertEqual(par[::-1], (P('z:/'), P('z:/a'),))
self.assertEqual(list(par), [P('z:/a'), P('z:/')])
with self.assertRaises(IndexError):
par[2]
@ -921,6 +945,12 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
self.assertEqual(len(par), 2)
self.assertEqual(par[0], P('//a/b/c'))
self.assertEqual(par[1], P('//a/b'))
self.assertEqual(par[0:1], (P('//a/b/c'),))
self.assertEqual(par[0:-1], (P('//a/b/c'),))
self.assertEqual(par[:2], (P('//a/b/c'), P('//a/b')))
self.assertEqual(par[1:], (P('//a/b'),))
self.assertEqual(par[::2], (P('//a/b/c'),))
self.assertEqual(par[::-1], (P('//a/b'), P('//a/b/c')))
self.assertEqual(list(par), [P('//a/b/c'), P('//a/b')])
with self.assertRaises(IndexError):
par[2]

View File

@ -270,6 +270,7 @@ Daniel Calvelo
Tony Campbell
Giovanni Cappellotto
Brett Cannon
Joshua Cannon
Tristan Carel
Mike Carlton
Pierre Carrier

View File

@ -0,0 +1 @@
Add slice support to :meth:`~pathlib.Path.parents`.