Skip to content
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 @@ -165,7 +166,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 import is_wasi, Py_DEBUG
from test.support.os_helper import (TESTFN, skip_unless_symlink,
Expand Down Expand Up @@ -392,6 +394,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.
Loading