summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2013-08-16 18:44:38 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2013-08-16 18:44:38 (GMT)
commitec34ab501025f7fd677e0f6b8a8d0c9f960f069f (patch)
tree8355f48954c0e12ac739ea58218aeafbe7780129 /Lib/test
parent802bf8aea1cff2122fc64cca33dc73b50b85e09c (diff)
downloadcpython-ec34ab501025f7fd677e0f6b8a8d0c9f960f069f.zip
cpython-ec34ab501025f7fd677e0f6b8a8d0c9f960f069f.tar.gz
cpython-ec34ab501025f7fd677e0f6b8a8d0c9f960f069f.tar.bz2
Issue #18756: Improve error reporting in os.urandom() when the failure is due to something else than /dev/urandom not existing.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_os.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 8d69623..2db030e 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -28,6 +28,11 @@ try:
import threading
except ImportError:
threading = None
+try:
+ import resource
+except ImportError:
+ resource = None
+
from test.script_helper import assert_python_ok
with warnings.catch_warnings():
@@ -997,6 +1002,21 @@ class URandomTests(unittest.TestCase):
data2 = self.get_urandom_subprocess(16)
self.assertNotEqual(data1, data2)
+ @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:
+ 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))
+
+
@contextlib.contextmanager
def _execvpe_mockup(defpath=None):
"""