summaryrefslogtreecommitdiffstats
path: root/Lib/pathlib.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2023-05-11 00:01:39 (GMT)
committerGitHub <noreply@github.com>2023-05-11 00:01:39 (GMT)
commit94f30c75576bb8a20724b2ac758fa33af089a522 (patch)
treee6c33824c353d12ff76eafd1af69d1d2aa834760 /Lib/pathlib.py
parent373bca0cc5256dc512ffc22bdff4424f7ee8baa2 (diff)
downloadcpython-94f30c75576bb8a20724b2ac758fa33af089a522.zip
cpython-94f30c75576bb8a20724b2ac758fa33af089a522.tar.gz
cpython-94f30c75576bb8a20724b2ac758fa33af089a522.tar.bz2
GH-90208: Suppress OSError exceptions from `pathlib.Path.glob()` (GH-104141)
`pathlib.Path.glob()` now suppresses all OSError exceptions, except those raised from calling `is_dir()` on the top-level path. Previously, `glob()` suppressed ENOENT, ENOTDIR, EBADF and ELOOP errors and their Windows equivalents. PermissionError was also suppressed unless it occurred when calling `is_dir()` on the top-level path. However, the selector would abort prematurely if a PermissionError was raised, and so `glob()` could return incomplete results.
Diffstat (limited to 'Lib/pathlib.py')
-rw-r--r--Lib/pathlib.py33
1 files changed, 13 insertions, 20 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 25863c7..40b7293 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -142,25 +142,21 @@ class _WildcardSelector(_Selector):
# avoid exhausting file descriptors when globbing deep trees.
with scandir(parent_path) as scandir_it:
entries = list(scandir_it)
+ except OSError:
+ pass
+ else:
for entry in entries:
if self.dironly:
try:
- # "entry.is_dir()" can raise PermissionError
- # in some cases (see bpo-38894), which is not
- # among the errors ignored by _ignore_error()
if not entry.is_dir():
continue
- except OSError as e:
- if not _ignore_error(e):
- raise
+ except OSError:
continue
name = entry.name
if self.match(name):
path = parent_path._make_child_relpath(name)
for p in self.successor._select_from(path, scandir):
yield p
- except PermissionError:
- return
class _RecursiveWildcardSelector(_Selector):
@@ -175,28 +171,25 @@ class _RecursiveWildcardSelector(_Selector):
# avoid exhausting file descriptors when globbing deep trees.
with scandir(parent_path) as scandir_it:
entries = list(scandir_it)
+ except OSError:
+ pass
+ else:
for entry in entries:
entry_is_dir = False
try:
entry_is_dir = entry.is_dir(follow_symlinks=False)
- except OSError as e:
- if not _ignore_error(e):
- raise
+ except OSError:
+ pass
if entry_is_dir:
path = parent_path._make_child_relpath(entry.name)
for p in self._iterate_directories(path, scandir):
yield p
- except PermissionError:
- return
def _select_from(self, parent_path, scandir):
- try:
- successor_select = self.successor._select_from
- for starting_point in self._iterate_directories(parent_path, scandir):
- for p in successor_select(starting_point, scandir):
- yield p
- except PermissionError:
- return
+ successor_select = self.successor._select_from
+ for starting_point in self._iterate_directories(parent_path, scandir):
+ for p in successor_select(starting_point, scandir):
+ yield p
class _DoubleRecursiveWildcardSelector(_RecursiveWildcardSelector):