SVN: r25215 - trunk/quixote

Neil Schemenauer <nascheme-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]> Tue, 28 Sep 2004 11:31:06 -0400
Newsgroups gmane.comp.web.quixote.cvs
Message-ID <[email protected]>
Author: nascheme
Date: 2004-09-28 11:30:51 -0400 (Tue, 28 Sep 2004)
New Revision: 25215

Modified:
   trunk/quixote/session.py
Log:
Use os.urandom() function if it is available.


Modified: trunk/quixote/session.py
===================================================================
--- trunk/quixote/session.py	2004-09-28 15:16:16 UTC (rev 25214)
+++ trunk/quixote/session.py	2004-09-28 15:30:51 UTC (rev 25215)
@@ -23,21 +23,27 @@
 
 __revision__ = "$Id$"
 
+import os
 import binascii
 from time import time, localtime, strftime, clock
 
 from quixote import get_publisher
 from quixote.errors import SessionError
 
-try:
+if hasattr(os, 'urandom'):
+    # available in Python 2.4 and also works on win32
+    def randbytes(bytes):
+        """Return bits of random data as a hex string."""
+        return binascii.hexlify(os.urandom(bytes))
+
+elif os.path.exists('/dev/urandom'):
     # /dev/urandom is just as good as /dev/random for cookies (assuming
     # SHA-1 is secure) and it never blocks.
-    open("/dev/urandom")
     def randbytes(bytes):
         """Return bits of random data as a hex string."""
         return binascii.hexlify(open("/dev/urandom").read(bytes))
 
-except IOError:
+else:
     # this is much less secure than the above function
     import sha
     _randstate = sha.new(str(time() + clock()))