[Python-checkins] r80784 - in python/trunk: Lib/test/test_uuid.py Lib/uuid.py Misc/NEWS

ronald.oussoren python-checkins at python.org
Wed May 5 16:48:37 CEST 2010


Author: ronald.oussoren
Date: Wed May  5 16:48:37 2010
New Revision: 80784

Log:
The C function used by uuid.uuid4 is broken on
OSX 10.6 in that after os.fork() the parent and
child generate the same sequence of UUIDs.

This patch falls back to the the Python implementation
on OSX 10.6 or later.

Fixes issue #8621.


Modified:
   python/trunk/Lib/test/test_uuid.py
   python/trunk/Lib/uuid.py
   python/trunk/Misc/NEWS

Modified: python/trunk/Lib/test/test_uuid.py
==============================================================================
--- python/trunk/Lib/test/test_uuid.py	(original)
+++ python/trunk/Lib/test/test_uuid.py	Wed May  5 16:48:37 2010
@@ -446,6 +446,34 @@
             equal(u, uuid.UUID(v))
             equal(str(u), v)
 
+    def testIssue8621(self):
+        import os
+        import sys
+        if os.name != 'posix':
+            return
+
+        # On at least some versions of OSX uuid.uuid4 generates
+        # the same sequence of UUIDs in the parent and any
+        # children started using fork.
+        fds = os.pipe()
+        pid = os.fork()
+        if pid == 0:
+            os.close(fds[0])
+            value = uuid.uuid4()
+            os.write(fds[1], value.hex)
+            sys.exit(0)
+
+        else:
+            os.close(fds[1])
+            parent_value = uuid.uuid4().hex
+            os.waitpid(pid, 0)
+            child_value = os.read(fds[0], 100)
+
+            self.assertNotEqual(parent_value, child_value)
+
+
+
+
 
 def test_main():
     test_support.run_unittest(TestUUID)

Modified: python/trunk/Lib/uuid.py
==============================================================================
--- python/trunk/Lib/uuid.py	(original)
+++ python/trunk/Lib/uuid.py	Wed May  5 16:48:37 2010
@@ -406,6 +406,19 @@
         if hasattr(lib, 'uuid_generate_time'):
             _uuid_generate_time = lib.uuid_generate_time
 
+    # The uuid_generate_* functions are broken on MacOS X 10.6, as noted
+    # in issue #8621 the function generates the same sequence of values
+    # in the parent process and all children created using fork (unless
+    # those children use exec as well).
+    #
+    # Assume that the uuid_generate functions are broken from 10.6 onward,
+    # the test can be adjusted when a later version is fixed.
+    import sys
+    if sys.platform == 'darwin':
+        import os
+        if int(os.uname()[2].split('.')[0]) >= 10:
+            _uuid_generate_random = _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
     # random UUID and UuidCreateSequential gives a UUID containing the

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Wed May  5 16:48:37 2010
@@ -39,6 +39,9 @@
 Library
 -------
 
+- Issue #8621: uuid.uuid4() returned the same sequence of values in the
+  parent and any children created using ``os.fork`` on MacOS X 10.6.
+
 - Issue #8313: traceback.format_exception_only() encodes unicode message to
   ASCII with backslashreplace error handler if str(value) failed
 


More information about the Python-checkins mailing list