summaryrefslogtreecommitdiffstats
path: root/Lib/os.py
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2005-07-04 17:16:07 (GMT)
committerGeorg Brandl <georg@python.org>2005-07-04 17:16:07 (GMT)
commit9e43acf2f31e67a976475c2c9f8674dd27571cc6 (patch)
treebfb6a9b8964ed76eb818738357c68c11471717ba /Lib/os.py
parent290f3829d79f5ed4073ea0bef8345fcd72ad9ec9 (diff)
downloadcpython-9e43acf2f31e67a976475c2c9f8674dd27571cc6.zip
cpython-9e43acf2f31e67a976475c2c9f8674dd27571cc6.tar.gz
cpython-9e43acf2f31e67a976475c2c9f8674dd27571cc6.tar.bz2
bug #1177468: don't cache /dev/urandom file descriptor in os.urandom
Diffstat (limited to 'Lib/os.py')
-rw-r--r--Lib/os.py12
1 files changed, 4 insertions, 8 deletions
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 @@ 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