r5443 - branches/stability/wooly/python/wooly

tmckay at fedoraproject.org tmckay at fedoraproject.org
Mon Aug 13 16:56:57 UTC 2012


Author: tmckay
Date: 2012-08-13 16:56:57 +0000 (Mon, 13 Aug 2012)
New Revision: 5443

Modified:
   branches/stability/wooly/python/wooly/util.py
Log:
BZ840106
svn merge -c 5400 svn+ssh://svn.fedorahosted.org/svn/cumin/trunk .
svn merge -c 5437 svn+ssh://svn.fedorahosted.org/svn/cumin/trunk .


Modified: branches/stability/wooly/python/wooly/util.py
===================================================================
--- branches/stability/wooly/python/wooly/util.py	2012-08-13 16:53:06 UTC (rev 5442)
+++ branches/stability/wooly/python/wooly/util.py	2012-08-13 16:56:57 UTC (rev 5443)
@@ -6,6 +6,7 @@
 import random
 import sys
 import time
+import struct
 
 from copy import copy
 from cStringIO import StringIO
@@ -24,13 +25,19 @@
     return string
 
 def unique_id():
-    bits0 = random.getrandbits(32)
-    bits1 = random.getrandbits(32)
-    bits2 = random.getrandbits(32)
-    bits3 = random.getrandbits(32)
 
-    return "%08x-%08x-%08x-%08x" % (bits0, bits1, bits2, bits3)
+    # Generate 4 unsigned integers.
+    # Take 4 bytes from urandom and interpret
+    # them as a standard-sized ('=') unsigned long ('L').
+    # Unpack returns a tuple, hence the [0].
+    try:
+        bits = [struct.unpack("=L", os.urandom(4))[0] for x in range(4)]
+    except NotImplementedError:
+        bits = [random.getrandbits(32) for x in range(4)]
+        log.debug("Wooly: os.urandom raised NotImplementedError")
 
+    return "%08x-%08x-%08x-%08x" % (bits[0], bits[1], bits[2], bits[3])
+
 def escape_amp(string):
     return str(string).replace("&", "&")
 



More information about the cumin-developers mailing list