Skip to content

Commit 365628c

Browse files
authored
test_too_many_open_files: assert on errno.EMFILE instead of strerror (#3001)
1 parent 5d09a90 commit 365628c

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

tests/unit/test_file_limit.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
@pytest.mark.skipif(sys.platform == "win32", reason="resource module not available on Windows")
1414
def test_too_many_open_files(tmp_path):
1515
"""
16-
Test that we get a specific error message when we have too many open files.
16+
Test that we get a specific error when we have too many open files.
1717
"""
1818
import resource # noqa: PLC0415
1919

2020
soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
2121

22-
# Lower the soft limit to a small number to trigger the error
2322
try:
2423
resource.setrlimit(resource.RLIMIT_NOFILE, (32, hard_limit))
2524
except ValueError:
@@ -28,24 +27,27 @@ def test_too_many_open_files(tmp_path):
2827
if "module 'resource' has no attribute 'setrlimit'" in str(exc):
2928
pytest.skip(f"{IMPLEMENTATION} does not support resource.setrlimit")
3029

31-
# Keep some file descriptors open to make it easier to trigger the error
3230
fds = []
3331
try:
34-
# JIT implementations use more file descriptors up front so we can run out early
32+
# JIT implementations may use more file descriptors up front, so we can run out early
3533
try:
3634
fds.extend(os.open(os.devnull, os.O_RDONLY) for _ in range(20))
37-
except OSError as jit_exceptions: # pypy, graalpy
38-
assert jit_exceptions.errno == errno.EMFILE # noqa: PT017
39-
assert "Too many open files" in str(jit_exceptions) # noqa: PT017
35+
except OSError as jit_exc: # pypy, graalpy
36+
assert jit_exc.errno == errno.EMFILE # noqa: PT017
4037

41-
expected_exceptions = SystemExit, OSError, RuntimeError
42-
with pytest.raises(expected_exceptions) as too_many_open_files_exc:
38+
expected_exceptions = (SystemExit, OSError, RuntimeError)
39+
with pytest.raises(expected_exceptions) as excinfo:
4340
cli_run([str(tmp_path / "venv")])
4441

45-
if isinstance(too_many_open_files_exc, SystemExit):
46-
assert too_many_open_files_exc.code != 0
42+
exc = excinfo.value
43+
if isinstance(exc, SystemExit):
44+
assert exc.code != 0
45+
elif isinstance(exc, OSError):
46+
assert exc.errno == errno.EMFILE
4747
else:
48-
assert "Too many open files" in str(too_many_open_files_exc.value)
48+
# RuntimeError wrapper path: don't assert on libc-specific strerror text.
49+
msg = str(exc)
50+
assert ("code 24" in msg) or ("errno 24" in msg) or ("EMFILE" in msg)
4951

5052
finally:
5153
for fd in fds:

0 commit comments

Comments
 (0)