summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/pathlib.py2
-rw-r--r--Lib/test/test_pathlib.py5
-rw-r--r--Misc/NEWS.d/next/Library/2022-05-26-23-10-55.gh-issue-93156.4XfDVN.rst2
3 files changed, 9 insertions, 0 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index c608ba0..26da4ed 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -443,6 +443,8 @@ class _PathParents(Sequence):
if idx >= len(self) or idx < -len(self):
raise IndexError(idx)
+ if idx < 0:
+ idx += len(self)
return self._pathcls._from_parsed_parts(self._drv, self._root,
self._parts[:-idx - 1])
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 6694f1b..2215134 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -465,6 +465,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')))
@@ -473,6 +476,8 @@ class _BasePurePathTest(object):
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]
def test_drive_common(self):
diff --git a/Misc/NEWS.d/next/Library/2022-05-26-23-10-55.gh-issue-93156.4XfDVN.rst b/Misc/NEWS.d/next/Library/2022-05-26-23-10-55.gh-issue-93156.4XfDVN.rst
new file mode 100644
index 0000000..165baa0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-05-26-23-10-55.gh-issue-93156.4XfDVN.rst
@@ -0,0 +1,2 @@
+Accessing the :attr:`pathlib.PurePath.parents` sequence of an absolute path
+using negative index values produced incorrect results.