]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-78722: Raise exceptions from `pathlib.Path.iterdir()` without delay. (#107320)
authorBarney Gale <barney.gale@gmail.com>
Sat, 2 Sep 2023 15:08:03 +0000 (16:08 +0100)
committerGitHub <noreply@github.com>
Sat, 2 Sep 2023 15:08:03 +0000 (16:08 +0100)
`pathlib.Path.iterdir()` now immediately raises any `OSError`
exception from `os.listdir()`, rather than waiting until its
result is iterated over.

Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/NEWS.d/next/Library/2023-07-26-22-52-48.gh-issue-78722.6SKBLt.rst [new file with mode: 0644]

index 758f70f5ba7077591784ded11b0d2df4bd6e4e5a..f4ec315da6b4fa4c5401c55400515f13de7b62e9 100644 (file)
@@ -1009,8 +1009,7 @@ class Path(PurePath):
         The children are yielded in arbitrary order, and the
         special entries '.' and '..' are not included.
         """
-        for name in os.listdir(self):
-            yield self._make_child_relpath(name)
+        return (self._make_child_relpath(name) for name in os.listdir(self))
 
     def _scandir(self):
         # bpo-24132: a future version of pathlib will support subclassing of
index 74deec84336f725e0d0b5734b9ba8432a4a19890..09df3fe471fc3ef3aa1ef3ff418600dbe605ab02 100644 (file)
@@ -1766,7 +1766,7 @@ class PathTest(unittest.TestCase):
         # __iter__ on something that is not a directory.
         p = self.cls(BASE, 'fileA')
         with self.assertRaises(OSError) as cm:
-            next(p.iterdir())
+            p.iterdir()
         # ENOENT or EINVAL under Windows, ENOTDIR otherwise
         # (see issue #12802).
         self.assertIn(cm.exception.errno, (errno.ENOTDIR,
diff --git a/Misc/NEWS.d/next/Library/2023-07-26-22-52-48.gh-issue-78722.6SKBLt.rst b/Misc/NEWS.d/next/Library/2023-07-26-22-52-48.gh-issue-78722.6SKBLt.rst
new file mode 100644 (file)
index 0000000..aea26ee
--- /dev/null
@@ -0,0 +1,2 @@
+Fix issue where :meth:`pathlib.Path.iterdir` did not raise :exc:`OSError`
+until iterated.