summaryrefslogtreecommitdiff
path: root/Lib/test/test_pathlib.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-06-03 14:53:00 -0700
committerGitHub <noreply@github.com>2022-06-03 14:53:00 -0700
commitb382bf50c53e6eab09f3e3bf0802ab052cb0289d (patch)
tree027824ea85c4e67602e4f14ed78c2e5d267632b4 /Lib/test/test_pathlib.py
parent9cdfd1b01aee8bd624c2fb5f5447e0ad5858ff03 (diff)
downloadcpython-git-b382bf50c53e6eab09f3e3bf0802ab052cb0289d.tar.gz
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>
Diffstat (limited to 'Lib/test/test_pathlib.py')
-rw-r--r--Lib/test/test_pathlib.py5
1 files changed, 5 insertions, 0 deletions
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 555c7ee795..bf3fc5fb24 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -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')))
@@ -471,6 +474,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):