Antoine Pitrou avatar Antoine Pitrou committed ffcf2df

Disallow .parents[i] where i goes beyond the number of parents

Comments (0)

Files changed (2)

             return len(self._parts)
-    def __iter__(self):
-        for i in range(len(self)):
-            yield self[i]
     def __getitem__(self, idx):
-        if idx < 0:
+        if idx < 0 or idx >= len(self):
             raise IndexError(idx)
-        if (self._drv or self._root) and idx >= len(self._parts) - 1:
-            # Anchored path, make sure the anchor remains
-            new_parts = self._parts[:1]
-        else:
-            new_parts = self._parts[:-idx-1]
         return self._pathcls._from_parsed_parts(self._drv, self._root,
-                                                new_parts)
+                                                self._parts[:-idx - 1])
     def __repr__(self):
         return "<{}.parents>".format(self._pathcls.__name__)
         self.assertEqual(par[0], P('a/b'))
         self.assertEqual(par[1], P('a'))
         self.assertEqual(par[2], P('.'))
-        self.assertEqual(par[3], P('.'))
         self.assertEqual(list(par), [P('a/b'), P('a'), P('.')])
         with self.assertRaises(IndexError):
+        with self.assertRaises(IndexError):
+            par[3]
         with self.assertRaises(TypeError):
             par[0] = p
         # Anchored
         self.assertEqual(par[0], P('/a/b'))
         self.assertEqual(par[1], P('/a'))
         self.assertEqual(par[2], P('/'))
-        self.assertEqual(par[3], P('/'))
         self.assertEqual(list(par), [P('/a/b'), P('/a'), P('/')])
+        with self.assertRaises(IndexError):
+            par[3]
     def test_drive_common(self):
         P = self.cls
         self.assertEqual(len(par), 2)
         self.assertEqual(par[0], P('z:a'))
         self.assertEqual(par[1], P('z:'))
-        self.assertEqual(par[2], P('z:'))
         self.assertEqual(list(par), [P('z:a'), P('z:')])
+        with self.assertRaises(IndexError):
+            par[2]
         p = P('z:/a/b/')
         par = p.parents
         self.assertEqual(len(par), 2)
         self.assertEqual(par[0], P('z:/a'))
         self.assertEqual(par[1], P('z:/'))
-        self.assertEqual(par[2], P('z:/'))
         self.assertEqual(list(par), [P('z:/a'), P('z:/')])
+        with self.assertRaises(IndexError):
+            par[2]
         p = P('//a/b/c/d')
         par = p.parents
         self.assertEqual(len(par), 2)
         self.assertEqual(par[0], P('//a/b/c'))
         self.assertEqual(par[1], P('//a/b'))
-        self.assertEqual(par[2], P('//a/b'))
         self.assertEqual(list(par), [P('//a/b/c'), P('//a/b')])
+        with self.assertRaises(IndexError):
+            par[2]
     def test_drive(self):
         P = self.cls
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.