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

Skip to content

Commit eba25ba

Browse files
committed
Issue #18756: make test_urandom_failure more robust by executing its code in a subprocess
1 parent 71fe8c0 commit eba25ba

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
@@ -1007,17 +1007,26 @@ def test_urandom_subprocess(self):
10071007

10081008
@unittest.skipUnless(resource, "test requires the resource module")
10091009
def test_urandom_failure(self):
1010-
soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
1011-
resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit))
1012-
try:
1013-
with self.assertRaises(OSError) as cm:
1010+
# Check urandom() failing when it is not able to open /dev/random.
1011+
# We spawn a new process to make the test more robust (if getrlimit()
1012+
# failed to restore the file descriptor limit after this, the whole
1013+
# test suite would crash; this actually happened on the OS X Tiger
1014+
# buildbot).
1015+
code = """if 1:
1016+
import errno
1017+
import os
1018+
import resource
1019+
1020+
soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
1021+
resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit))
1022+
try:
10141023
os.urandom(16)
1015-
self.assertEqual(cm.exception.errno, errno.EMFILE)
1016-
finally:
1017-
# We restore the old limit as soon as possible. If doing it
1018-
# using addCleanup(), code running in between would fail
1019-
# creating any file descriptor.
1020-
resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_limit))
1024+
except OSError as e:
1025+
assert e.errno == errno.EMFILE, e.errno
1026+
else:
1027+
raise AssertionError("OSError not raised")
1028+
"""
1029+
assert_python_ok('-c', code)
10211030

10221031

10231032
@contextlib.contextmanager

0 commit comments

Comments
 (0)