mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
bug #1177468: don't cache /dev/urandom file descriptor in os.urandom
This commit is contained in:
parent
290f3829d7
commit
9e43acf2f3
2 changed files with 7 additions and 8 deletions
12
Lib/os.py
12
Lib/os.py
|
@ -715,22 +715,18 @@ except NameError: # statvfs_result may not exist
|
|||
pass
|
||||
|
||||
if not _exists("urandom"):
|
||||
_urandomfd = None
|
||||
def urandom(n):
|
||||
"""urandom(n) -> str
|
||||
|
||||
Return a string of n random bytes suitable for cryptographic use.
|
||||
|
||||
"""
|
||||
global _urandomfd
|
||||
if _urandomfd is None:
|
||||
try:
|
||||
_urandomfd = open("/dev/urandom", O_RDONLY)
|
||||
except:
|
||||
_urandomfd = NotImplementedError
|
||||
if _urandomfd is NotImplementedError:
|
||||
try:
|
||||
_urandomfd = open("/dev/urandom", O_RDONLY)
|
||||
except:
|
||||
raise NotImplementedError("/dev/urandom (or equivalent) not found")
|
||||
bytes = ""
|
||||
while len(bytes) < n:
|
||||
bytes += read(_urandomfd, n - len(bytes))
|
||||
close(_urandomfd)
|
||||
return bytes
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue