1. Antoine Pitrou
  2. pathlib


Antoine Pitrou  committed 5d81ead

Issue #18: fix iterating over .parents to observe the sequence length

  • Participants
  • Parent commits 15bc4af
  • Branches pep428

Comments (0)

Files changed (2)

File pathlib.py

View file
  • Ignore whitespace
             return len(self._parts)
+    def __iter__(self):
+        for i in range(len(self)):
+            yield self[i]
     def __getitem__(self, idx):
         if idx < 0:
             raise IndexError(idx)

File test_pathlib.py

View file
  • Ignore whitespace
         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(TypeError):
         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('/')])
     def test_drive_common(self):
         P = self.cls
         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:')])
         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:/')])
         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')])
     def test_drive(self):
         P = self.cls