Skip to content

Commit

Permalink
pythonGH-78722: Raise exceptions from pathlib.Path.iterdir() withou…
Browse files Browse the repository at this point in the history
…t delay.

`pathlib.Path.iterdir()` now immediately raises any `OSError`
exception from `os.listdir()`, rather than waiting until its
result is iterated over.
  • Loading branch information
barneygale committed Jul 26, 2023
1 parent c6c5665 commit 4d552aa
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
3 changes: 1 addition & 2 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,7 @@ def iterdir(self):
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
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,7 @@ def test_iterdir_nodir(self):
# __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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix issue where :meth:`pathlib.Path.iterdir` did not raise :exc:`OSError`
until iterated.

0 comments on commit 4d552aa

Please sign in to comment.