diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 69e7d558a05..62dd0fa1b12 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -299,6 +299,8 @@ class _WildcardSelector(_Selector): def _select_from(self, parent_path, is_dir, exists, scandir): try: + # We must close the scandir() object before proceeding to + # avoid exhausting file descriptors when globbing deep trees. with scandir(parent_path) as scandir_it: entries = list(scandir_it) for entry in entries: @@ -330,6 +332,8 @@ class _RecursiveWildcardSelector(_Selector): def _iterate_directories(self, parent_path, is_dir, scandir): yield parent_path try: + # We must close the scandir() object before proceeding to + # avoid exhausting file descriptors when globbing deep trees. with scandir(parent_path) as scandir_it: entries = list(scandir_it) for entry in entries: