Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5e30d8

Browse files
committed
Issue #18756: make test_urandom_failure more robust by executing its code in a subprocess
2 parents 0956689 + eba25ba commit f5e30d8

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

Lib/test/test_os.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,17 +1020,26 @@ def test_urandom_subprocess(self):
10201020

10211021
@unittest.skipUnless(resource, "test requires the resource module")
10221022
def test_urandom_failure(self):
1023-
soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
1024-
resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit))
1025-
try:
1026-
with self.assertRaises(OSError) as cm:
1023+
# Check urandom() failing when it is not able to open /dev/random.
1024+
# We spawn a new process to make the test more robust (if getrlimit()
1025+
# failed to restore the file descriptor limit after this, the whole
1026+
# test suite would crash; this actually happened on the OS X Tiger
1027+
# buildbot).
1028+
code = """if 1:
1029+
import errno
1030+
import os
1031+
import resource
1032+
1033+
soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
1034+
resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit))
1035+
try:
10271036
os.urandom(16)
1028-
self.assertEqual(cm.exception.errno, errno.EMFILE)
1029-
finally:
1030-
# We restore the old limit as soon as possible. If doing it
1031-
# using addCleanup(), code running in between would fail
1032-
# creating any file descriptor.
1033-
resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_limit))
1037+
except OSError as e:
1038+
assert e.errno == errno.EMFILE, e.errno
1039+
else:
1040+
raise AssertionError("OSError not raised")
1041+
"""
1042+
assert_python_ok('-c', code)
10341043

10351044

10361045
@contextlib.contextmanager

0 commit comments

Comments
 (0)