Skip to content

gh-103501: Propagate OSError if errno is EMFILE in glob.glob #109517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Lib/glob.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Filename globbing utility."""

import contextlib
import errno
import os
import re
import fnmatch
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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'))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Raise :exc:`OSError` instead of return ``[]`` in :func:`glob.glob` when max open
file limit reached.