mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
Issue #3708: os.urandom no longer goes into an infinite loop when passed a
non-integer floating point number.
This commit is contained in:
parent
2bb25cc1e2
commit
d712203d17
3 changed files with 14 additions and 5 deletions
12
Lib/os.py
12
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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
-----------------
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue