diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/os.py | 12 | ||||
-rw-r--r-- | Lib/test/test_os.py | 4 |
2 files changed, 11 insertions, 5 deletions
@@ -753,8 +753,10 @@ if not _exists("urandom"): _urandomfd = open("/dev/urandom", O_RDONLY) except (OSError, IOError): raise NotImplementedError("/dev/urandom (or equivalent) not found") - bytes = "" - while len(bytes) < n: - bytes += read(_urandomfd, n - len(bytes)) - close(_urandomfd) - return bytes + try: + bs = b"" + while n - len(bs) >= 1: + bs += read(_urandomfd, n - len(bs)) + finally: + close(_urandomfd) + return bs diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index cec023d..a618af4 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -497,6 +497,10 @@ class URandomTests (unittest.TestCase): self.assertEqual(len(os.urandom(10)), 10) self.assertEqual(len(os.urandom(100)), 100) self.assertEqual(len(os.urandom(1000)), 1000) + # see http://bugs.python.org/issue3708 + self.assertEqual(len(os.urandom(0.9)), 0) + self.assertEqual(len(os.urandom(1.1)), 1) + self.assertEqual(len(os.urandom(2.0)), 2) except NotImplementedError: pass |