Commit ec34ab50 authored by Antoine Pitrou's avatar Antoine Pitrou

Issue #18756: Improve error reporting in os.urandom() when the failure is due...

Issue #18756: Improve error reporting in os.urandom() when the failure is due to something else than /dev/urandom not existing.
parent 802bf8ae
...@@ -28,6 +28,11 @@ try: ...@@ -28,6 +28,11 @@ try:
import threading import threading
except ImportError: except ImportError:
threading = None threading = None
try:
import resource
except ImportError:
resource = None
from test.script_helper import assert_python_ok from test.script_helper import assert_python_ok
with warnings.catch_warnings(): with warnings.catch_warnings():
...@@ -997,6 +1002,21 @@ class URandomTests(unittest.TestCase): ...@@ -997,6 +1002,21 @@ class URandomTests(unittest.TestCase):
data2 = self.get_urandom_subprocess(16) data2 = self.get_urandom_subprocess(16)
self.assertNotEqual(data1, data2) 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 @contextlib.contextmanager
def _execvpe_mockup(defpath=None): def _execvpe_mockup(defpath=None):
""" """
......
...@@ -66,6 +66,10 @@ Core and Builtins ...@@ -66,6 +66,10 @@ Core and Builtins
Library 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).
- Issue #18405: Improve the entropy of crypt.mksalt(). - Issue #18405: Improve the entropy of crypt.mksalt().
- Issue #18676: Change 'positive' to 'non-negative' in queue.py put and get - Issue #18676: Change 'positive' to 'non-negative' in queue.py put and get
......
...@@ -165,8 +165,12 @@ dev_urandom_python(char *buffer, Py_ssize_t size) ...@@ -165,8 +165,12 @@ dev_urandom_python(char *buffer, Py_ssize_t size)
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if (fd < 0) if (fd < 0)
{ {
PyErr_SetString(PyExc_NotImplementedError, if (errno == ENOENT || errno == ENXIO ||
"/dev/urandom (or equivalent) not found"); errno == ENODEV || errno == EACCES)
PyErr_SetString(PyExc_NotImplementedError,
"/dev/urandom (or equivalent) not found");
else
PyErr_SetFromErrno(PyExc_OSError);
return -1; return -1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment