1. Antoine Pitrou
  2. pathlib

Commits

Antoine Pitrou  committed 8ec3c42

Replace __iter__ with iterdir()

  • Participants
  • Parent commits 963f2e4
  • Branches pep428

Comments (0)

Files changed (3)

File docs/index.rst

View file
 Listing subdirectories::
 
    >>> p = Path('.')
-   >>> [x for x in p if x.is_dir()]
+   >>> [x for x in p.iterdir() if x.is_dir()]
    [PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'),
     PosixPath('__pycache__'), PosixPath('build')]
 
    NotImplementedError: cannot instantiate 'NTPath' on your system
 
 
-Iterating
-^^^^^^^^^
-
-When a concrete path points to a directory, iterating over it yields path
-objects of the directory contents::
-
-   >>> p = Path('docs')
-   >>> for child in p: child
-   ...
-   PosixPath('docs/conf.py')
-   PosixPath('docs/_templates')
-   PosixPath('docs/make.bat')
-   PosixPath('docs/index.rst')
-   PosixPath('docs/_build')
-   PosixPath('docs/_static')
-   PosixPath('docs/Makefile')
-
-
 Methods
 ^^^^^^^
 
    as permission errors) are propagated.
 
 
+.. method:: Path.iterdir()
+
+   When the path points to a directory, yield path objects of the directory
+   contents::
+
+      >>> p = Path('docs')
+      >>> for child in p.iterdir(): child
+      ...
+      PosixPath('docs/conf.py')
+      PosixPath('docs/_templates')
+      PosixPath('docs/make.bat')
+      PosixPath('docs/index.rst')
+      PosixPath('docs/_build')
+      PosixPath('docs/_static')
+      PosixPath('docs/Makefile')
+
 .. method:: Path.lchmod(mode)
 
    Like :meth:`Path.chmod` but, if the path points to a symbolic link, the

File pathlib.py

View file
         """
         return cls(os.getcwd())
 
-    def __iter__(self):
+    def iterdir(self):
         """Iterate over the files in this directory.  Does not yield any
         result for the special paths '.' and '..'.
         """

File test_pathlib.py

View file
             self.assertIsInstance(f, io.RawIOBase)
             self.assertEqual(f.read().strip(), b"this is file A")
 
-    def test_iter(self):
+    def test_iterdir(self):
         P = self.cls
         p = P(BASE)
-        self.assertIsInstance(p, collections.Iterable)
-        it = iter(p)
+        it = p.iterdir()
         paths = set(it)
         expected = ['dirA', 'dirB', 'dirC', 'fileA']
         if not symlink_skip_reason:
         self.assertEqual(paths, { P(BASE, q) for q in expected })
 
     @with_symlinks
-    def test_iter_symlink(self):
+    def test_iterdir_symlink(self):
         # __iter__ on a symlink to a directory
         P = self.cls
         p = P(BASE, 'linkB')
-        paths = set(p)
+        paths = set(p.iterdir())
         expected = { P(BASE, 'linkB', q) for q in ['fileB', 'linkD'] }
         self.assertEqual(paths, expected)
 
-    def test_iter_nodir(self):
+    def test_iterdir_nodir(self):
         # __iter__ on something that is not a directory
         p = self.cls(BASE, 'fileA')
         with self.assertRaises(OSError) as cm:
-            list(p)
+            next(p.iterdir())
         # ENOENT or EINVAL under Windows, ENOTDIR otherwise
         # (see issue #12802)
         self.assertIn(cm.exception.errno, (errno.ENOTDIR,
 
     def test_with(self):
         p = self.cls(BASE)
-        it = iter(p)
-        it2 = iter(p)
+        it = p.iterdir()
+        it2 = p.iterdir()
         next(it2)
         with p:
             pass
 
     def test_rmdir(self):
         p = self.cls(BASE) / 'dirA'
-        for q in p:
+        for q in p.iterdir():
             q.unlink()
         p.rmdir()
         self.assertFileNotFound(p.stat)
         self.assertEqual(link.stat(), target.stat())
         self.assertNotEqual(link.lstat(), target.stat())
         self.assertTrue(link.is_dir())
-        self.assertTrue(list(link))
+        self.assertTrue(list(link.iterdir()))
 
     def test_is_dir(self):
         P = self.cls(BASE)