summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2013-08-24 18:52:45 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2013-08-24 18:52:45 (GMT)
commitf5e30d8b54cdc07c53ffa741353a83ed3263d7d2 (patch)
treeea15d3a18457644b51e9df8a6e705c46e32f55ab
parent095668914c38c03760aaf6f2cbe04c0bb1aead51 (diff)
parenteba25bafc744a3889f9a791268c085598e416060 (diff)
downloadcpython-f5e30d8b54cdc07c53ffa741353a83ed3263d7d2.zip
cpython-f5e30d8b54cdc07c53ffa741353a83ed3263d7d2.tar.gz
cpython-f5e30d8b54cdc07c53ffa741353a83ed3263d7d2.tar.bz2
Issue #18756: make test_urandom_failure more robust by executing its code in a subprocess
-rw-r--r--Lib/test/test_os.py29
1 files changed, 19 insertions, 10 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index a4be0bb..1d41b77 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -1020,17 +1020,26 @@ class URandomTests(unittest.TestCase):
@unittest.skipUnless(resource, "test requires the resource module")
def test_urandom_failure(self):
- soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
- resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit))
- try:
- with self.assertRaises(OSError) as cm:
+ # Check urandom() failing when it is not able to open /dev/random.
+ # We spawn a new process to make the test more robust (if getrlimit()
+ # failed to restore the file descriptor limit after this, the whole
+ # test suite would crash; this actually happened on the OS X Tiger
+ # buildbot).
+ code = """if 1:
+ import errno
+ import os
+ import resource
+
+ soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
+ resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit))
+ try:
os.urandom(16)
- self.assertEqual(cm.exception.errno, errno.EMFILE)
- finally:
- # We restore the old limit as soon as possible. If doing it
- # using addCleanup(), code running in between would fail
- # creating any file descriptor.
- resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_limit))
+ except OSError as e:
+ assert e.errno == errno.EMFILE, e.errno
+ else:
+ raise AssertionError("OSError not raised")
+ """
+ assert_python_ok('-c', code)
@contextlib.contextmanager