Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ Pure paths provide the following methods and properties:
PureWindowsPath('c:/')

.. versionchanged:: 3.10
Slice support was added.
The parents sequence now supports :term:`slices <slice>` and negative index values.

.. data:: PurePath.parent

Expand Down
6 changes: 5 additions & 1 deletion Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,13 @@ pipe. (Contributed by Pablo Galindo in :issue:`41625`.)
pathlib
-------

Added slice support to :meth:`~pathlib.Path.parents`.
Added slice support to :attr:`PurePath.parents <pathlib.PurePath.parents>`.
(Contributed by Joshua Cannon in :issue:`35498`)

Added negative indexing support to :attr:`PurePath.parents
<pathlib.PurePath.parents>`.
(Contributed by Yaroslav Pankovych in :issue:`21041`)

py_compile
----------

Expand Down
3 changes: 2 additions & 1 deletion Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,8 @@ def __len__(self):
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):

if idx >= len(self) or idx < -len(self):
raise IndexError(idx)
return self._pathcls._from_parsed_parts(self._drv, self._root,
self._parts[:-idx - 1])
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,9 @@ def test_parents_common(self):
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')))
Expand All @@ -448,7 +451,7 @@ def test_parents_common(self):
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]
par[-4]
with self.assertRaises(IndexError):
par[3]
with self.assertRaises(TypeError):
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,7 @@ Michael Otteneder
Richard Oudkerk
Russel Owen
Joonas Paalasmaa
Yaroslav Pankovych
Martin Packman
Elisha Paine
Shriphani Palakodety
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Add slice support to :meth:`~pathlib.Path.parents`.
Add slice support to :attr:`pathlib.PurePath.parents`.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:attr:`pathlib.PurePath.parents` now supports negative indexing. Patch contributed by Yaroslav Pankovych.