mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
GH-78722: Raise exceptions from pathlib.Path.iterdir()
without delay. (#107320)
`pathlib.Path.iterdir()` now immediately raises any `OSError` exception from `os.listdir()`, rather than waiting until its result is iterated over.
This commit is contained in:
parent
594b00057e
commit
bdc3c884cd
3 changed files with 4 additions and 3 deletions
|
@ -1009,8 +1009,7 @@ class Path(PurePath):
|
||||||
The children are yielded in arbitrary order, and the
|
The children are yielded in arbitrary order, and the
|
||||||
special entries '.' and '..' are not included.
|
special entries '.' and '..' are not included.
|
||||||
"""
|
"""
|
||||||
for name in os.listdir(self):
|
return (self._make_child_relpath(name) for name in os.listdir(self))
|
||||||
yield self._make_child_relpath(name)
|
|
||||||
|
|
||||||
def _scandir(self):
|
def _scandir(self):
|
||||||
# bpo-24132: a future version of pathlib will support subclassing of
|
# bpo-24132: a future version of pathlib will support subclassing of
|
||||||
|
|
|
@ -1766,7 +1766,7 @@ class PathTest(unittest.TestCase):
|
||||||
# __iter__ on something that is not a directory.
|
# __iter__ on something that is not a directory.
|
||||||
p = self.cls(BASE, 'fileA')
|
p = self.cls(BASE, 'fileA')
|
||||||
with self.assertRaises(OSError) as cm:
|
with self.assertRaises(OSError) as cm:
|
||||||
next(p.iterdir())
|
p.iterdir()
|
||||||
# ENOENT or EINVAL under Windows, ENOTDIR otherwise
|
# ENOENT or EINVAL under Windows, ENOTDIR otherwise
|
||||||
# (see issue #12802).
|
# (see issue #12802).
|
||||||
self.assertIn(cm.exception.errno, (errno.ENOTDIR,
|
self.assertIn(cm.exception.errno, (errno.ENOTDIR,
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix issue where :meth:`pathlib.Path.iterdir` did not raise :exc:`OSError`
|
||||||
|
until iterated.
|
Loading…
Add table
Add a link
Reference in a new issue