[Python-checkins] cpython (2.7): always use os.urandom for the uuid4 algorithm (closes #25515)

benjamin.peterson python-checkins at python.org
Thu Oct 29 23:40:42 EDT 2015


https://hg.python.org/cpython/rev/24bdc4940e81
changeset: 98889:24bdc4940e81
branch: 2.7
parent: 98877:c307ae7b3d9f
user: Benjamin Peterson <benjamin at python.org>
date: Thu Oct 29 20:38:04 2015 -0700
summary:
 always use os.urandom for the uuid4 algorithm (closes #25515)
files:
 Lib/uuid.py | 27 ++++++---------------------
 Misc/NEWS | 2 ++
 2 files changed, 8 insertions(+), 21 deletions(-)
diff --git a/Lib/uuid.py b/Lib/uuid.py
--- a/Lib/uuid.py
+++ b/Lib/uuid.py
@@ -44,6 +44,8 @@
 UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
 """
 
+import os
+
 __author__ = 'Ka-Ping Yee <ping at zesty.ca>'
 
 RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [
@@ -438,7 +440,7 @@
 # Thanks to Thomas Heller for ctypes and for his help with its use here.
 
 # If ctypes is available, use it to find system routines for UUID generation.
-_uuid_generate_random = _uuid_generate_time = _UuidCreate = None
+_uuid_generate_time = _UuidCreate = None
 try:
 import ctypes, ctypes.util
 import sys
@@ -453,12 +455,9 @@
 lib = ctypes.CDLL(ctypes.util.find_library(libname))
 except:
 continue
- if hasattr(lib, 'uuid_generate_random'):
- _uuid_generate_random = lib.uuid_generate_random
 if hasattr(lib, 'uuid_generate_time'):
 _uuid_generate_time = lib.uuid_generate_time
- if _uuid_generate_random is not None:
- break # found everything we were looking for
+ break
 del _libnames
 
 # The uuid_generate_* functions are broken on MacOS X 10.5, as noted
@@ -471,7 +470,7 @@
 if sys.platform == 'darwin':
 import os
 if int(os.uname()[2].split('.')[0]) >= 9:
- _uuid_generate_random = _uuid_generate_time = None
+ _uuid_generate_time = None
 
 # On Windows prior to 2000, UuidCreate gives a UUID containing the
 # hardware address. On Windows 2000 and later, UuidCreate makes a
@@ -582,21 +581,7 @@
 
 def uuid4():
 """Generate a random UUID."""
-
- # When the system provides a version-4 UUID generator, use it.
- if _uuid_generate_random:
- _buffer = ctypes.create_string_buffer(16)
- _uuid_generate_random(_buffer)
- return UUID(bytes=_buffer.raw)
-
- # Otherwise, get randomness from urandom or the 'random' module.
- try:
- import os
- return UUID(bytes=os.urandom(16), version=4)
- except:
- import random
- bytes = [chr(random.randrange(256)) for i in range(16)]
- return UUID(bytes=bytes, version=4)
+ return UUID(bytes=os.urandom(16), version=4)
 
 def uuid5(namespace, name):
 """Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -46,6 +46,8 @@
 Library
 -------
 
+- Issue #25515: Always use os.urandom as a source of randomness in uuid.uuid4.
+
 - Issue #21827: Fixed textwrap.dedent() for the case when largest common
 whitespace is a substring of smallest leading whitespace.
 Based on patch by Robert Li.
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /