bug #1177468: don't cache /dev/urandom file descriptor in os.urandom
diff --git a/Lib/os.py b/Lib/os.py
index 5824609..bf1b086 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -715,22 +715,18 @@
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