diff --git a/Lib/glob.py b/Lib/glob.py index 4a335a10766cf4..bfacdae17c7ece 100644 --- a/Lib/glob.py +++ b/Lib/glob.py @@ -1,6 +1,7 @@ """Filename globbing utility.""" import contextlib +import errno import os import re import fnmatch @@ -169,7 +170,9 @@ def _iterdir(dirname, dir_fd, dironly): finally: if fd is not None: os.close(fd) - except OSError: + except OSError as e: + if e.errno == errno.EMFILE: + raise return def _listdir(dirname, dir_fd, dironly): diff --git a/Lib/test/test_glob.py b/Lib/test/test_glob.py index aa5fac8eca1354..4720810d1d737e 100644 --- a/Lib/test/test_glob.py +++ b/Lib/test/test_glob.py @@ -4,6 +4,8 @@ import shutil import sys import unittest +from errno import EMFILE +from unittest import mock from test.support.os_helper import (TESTFN, skip_unless_symlink, can_symlink, create_empty_file, change_cwd) @@ -350,6 +352,12 @@ def test_glob_many_open_files(self): for it in iters: self.assertEqual(next(it), p) + def test_glob_too_many_open_files(self): + with mock.patch('os.scandir') as mocked_func: + mocked_func.side_effect = OSError(EMFILE, os.strerror(EMFILE), '.') + + self.assertRaises(OSError, glob.glob, '*') + def test_translate_matching(self): match = re.compile(glob.translate('*')).match self.assertIsNotNone(match('foo')) diff --git a/Misc/NEWS.d/next/Library/2023-09-17-22-56-49.gh-issue-103501.qbepu7.rst b/Misc/NEWS.d/next/Library/2023-09-17-22-56-49.gh-issue-103501.qbepu7.rst new file mode 100644 index 00000000000000..1c53aea320f2e2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-09-17-22-56-49.gh-issue-103501.qbepu7.rst @@ -0,0 +1,2 @@ +Raise :exc:`OSError` instead of return ``[]`` in :func:`glob.glob` when max open +file limit reached.