[Python-checkins] r66142 - in python/trunk: Lib/os.py Lib/test/test_os.py Misc/NEWS

gregory.p.smith python-checkins at python.org
Tue Sep 2 07:36:11 CEST 2008


Author: gregory.p.smith
Date: Tue Sep  2 07:36:11 2008
New Revision: 66142

Log:
Issue #3708: os.urandom no longer goes into an infinite loop when passed a
non-integer floating point number.


Modified:
   python/trunk/Lib/os.py
   python/trunk/Lib/test/test_os.py
   python/trunk/Misc/NEWS

Modified: python/trunk/Lib/os.py
==============================================================================
--- python/trunk/Lib/os.py	(original)
+++ python/trunk/Lib/os.py	Tue Sep  2 07:36:11 2008
@@ -753,8 +753,10 @@
             _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

Modified: python/trunk/Lib/test/test_os.py
==============================================================================
--- python/trunk/Lib/test/test_os.py	(original)
+++ python/trunk/Lib/test/test_os.py	Tue Sep  2 07:36:11 2008
@@ -497,6 +497,10 @@
             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
 

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Tue Sep  2 07:36:11 2008
@@ -72,6 +72,9 @@
 - 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
 -----------------
 


More information about the Python-checkins mailing list