[Python-checkins] cpython (2.7): Issue #18756: Improve error reporting in os.urandom() when the failure is due

antoine.pitrou python-checkins at python.org
Fri Aug 16 20:54:19 CEST 2013


http://hg.python.org/cpython/rev/ec296a36156b
changeset:   85200:ec296a36156b
branch:      2.7
parent:      85195:6bc88d61f302
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Fri Aug 16 20:44:38 2013 +0200
summary:
  Issue #18756: Improve error reporting in os.urandom() when the failure is due to something else than /dev/urandom not existing.

files:
  Lib/test/test_os.py |  18 ++++++++++++++++++
  Misc/NEWS           |   4 ++++
  Python/random.c     |   8 ++++++--
  3 files changed, 28 insertions(+), 2 deletions(-)


diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -10,6 +10,10 @@
 import signal
 import subprocess
 import time
+try:
+    import resource
+except ImportError:
+    resource = None
 
 from test import test_support
 import mmap
@@ -563,6 +567,20 @@
         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))
+
     def test_execvpe_with_bad_arglist(self):
         self.assertRaises(ValueError, os.execvpe, 'notepad', [], None)
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,10 @@
 Library
 -------
 
+- Issue #18756: Improve error reporting in os.urandom() when the failure
+  is due to something else than /dev/urandom not existing (for example,
+  exhausting the file descriptor limit).
+
 - Fix tkinter regression introduced by the security fix in issue #16248.
 
 - Issue #18676: Change 'positive' to 'non-negative' in queue.py put and get
diff --git a/Python/random.c b/Python/random.c
--- a/Python/random.c
+++ b/Python/random.c
@@ -165,8 +165,12 @@
     Py_END_ALLOW_THREADS
     if (fd < 0)
     {
-        PyErr_SetString(PyExc_NotImplementedError,
-                        "/dev/urandom (or equivalent) not found");
+        if (errno == ENOENT || errno == ENXIO ||
+            errno == ENODEV || errno == EACCES)
+            PyErr_SetString(PyExc_NotImplementedError,
+                            "/dev/urandom (or equivalent) not found");
+        else
+            PyErr_SetFromErrno(PyExc_OSError);
         return -1;
     }
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list