]> granicus.if.org Git - python/commitdiff
Issue #3708: os.urandom no longer goes into an infinite loop when passed a
authorGregory P. Smith <greg@mad-scientist.com>
Tue, 2 Sep 2008 05:36:11 +0000 (05:36 +0000)
committerGregory P. Smith <greg@mad-scientist.com>
Tue, 2 Sep 2008 05:36:11 +0000 (05:36 +0000)
non-integer floating point number.

Lib/os.py
Lib/test/test_os.py
Misc/NEWS

index 715ff202f48dbf3a2c51ea66ca6d5e4a30ba4b30..abbadc41e44b0a44d09f04504f8554655fe3f36b 100644 (file)
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -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
index cec023d308d2569e5c49ec807719c5e33fb5b89f..a618af42b451f9c2b4ace64da709b6a77f2c5135 100644 (file)
@@ -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
 
index a1074736aeaf068ff9fe851e4f114c5af6eed81e..00dfd33d824fe7d9e4bbab91ccde8baf02472c95 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -72,6 +72,9 @@ Library
 - Issue #3703: _fileio.FileIO gave unhelpful error message when trying to open a
   directory.
 
+- Issue #3708: os.urandom no longer goes into an infinite loop when passed a
+  non-integer floating point number.
+
 Extension Modules
 -----------------