]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
backport bug #1177468: don't cache /dev/urandom file descriptor in os.urandom
authorGeorg Brandl <georg@python.org>
Mon, 4 Jul 2005 17:16:53 +0000 (17:16 +0000)
committerGeorg Brandl <georg@python.org>
Mon, 4 Jul 2005 17:16:53 +0000 (17:16 +0000)
Lib/os.py
Misc/NEWS

index 514d440b28152a54989b67226f0c788a00f687d8..eb7dd59d3109870cb72470d02785a0968131d510 100644 (file)
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -708,22 +708,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
index 426ec28352188142f9d9c7363d4a0356fe2d0aa1..fb0391f4cb2aa4158f1de53e3f48f9d3ab8a1c42 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -39,6 +39,9 @@ Extension Modules
 Library
 -------
 
+- Bug #1177468: Don't cache the /dev/urandom file descriptor for os.urandom,
+  as this can cause problems with apps closing all file descriptors.
+
 - Bug #839151: Fix an attempt to access sys.argv in the warnings module
   though this can be missing in embedded interpreters