Ticket #16260: uuid.patch

File uuid.patch, 7.1 KB (added by jpflori, 7 years ago)

Rebased patch

  • Lib/uuid.py

    diff -druN python-2.7.5.orig/Lib/uuid.py python-2.7.5/Lib/uuid.py
    old new  
    5050    'reserved for NCS compatibility', 'specified in RFC 4122',
    5151    'reserved for Microsoft compatibility', 'reserved for future definition']
    5252
     53
    5354class UUID(object):
    5455    """Instances of the UUID class represent UUIDs as specified in RFC 4122.
    5556    UUID objects are immutable, hashable, and usable as dictionary keys.
     
    291292
    292293    version = property(get_version)
    293294
     295
    294296def _find_mac(command, args, hw_identifiers, get_index):
    295297    import os
    296298    for dir in ['', '/sbin/', '/usr/sbin']:
     
    390392        return ((bytes[0]<<40L) + (bytes[1]<<32L) + (bytes[2]<<24L) +
    391393                (bytes[3]<<16L) + (bytes[4]<<8L) + bytes[5])
    392394
    393 # Thanks to Thomas Heller for ctypes and for his help with its use here.
    394395
    395 # If ctypes is available, use it to find system routines for UUID generation.
    396 _uuid_generate_random = _uuid_generate_time = _UuidCreate = None
    397 try:
    398     import ctypes, ctypes.util
     396_ctypes_lib = None
    399397
    400     # The uuid_generate_* routines are provided by libuuid on at least
    401     # Linux and FreeBSD, and provided by libc on Mac OS X.
    402     for libname in ['uuid', 'c']:
    403         try:
    404             lib = ctypes.CDLL(ctypes.util.find_library(libname))
    405         except:
    406             continue
    407         if hasattr(lib, 'uuid_generate_random'):
    408             _uuid_generate_random = lib.uuid_generate_random
    409         if hasattr(lib, 'uuid_generate_time'):
    410             _uuid_generate_time = lib.uuid_generate_time
    411             if _uuid_generate_random is not None:
    412                 break  # found everything we were looking for
    413398
    414     # The uuid_generate_* functions are broken on MacOS X 10.5, as noted
    415     # in issue #8621 the function generates the same sequence of values
    416     # in the parent process and all children created using fork (unless
    417     # those children use exec as well).
    418     #
    419     # Assume that the uuid_generate functions are broken from 10.5 onward,
    420     # the test can be adjusted when a later version is fixed.
    421     import sys
    422     if sys.platform == 'darwin':
    423         import os
    424         if int(os.uname()[2].split('.')[0]) >= 9:
    425             _uuid_generate_random = _uuid_generate_time = None
     399def _uuid_generate(attr):
     400    """Find system routines for UUID generation"""
    426401
    427     # On Windows prior to 2000, UuidCreate gives a UUID containing the
    428     # hardware address.  On Windows 2000 and later, UuidCreate makes a
    429     # random UUID and UuidCreateSequential gives a UUID containing the
    430     # hardware address.  These routines are provided by the RPC runtime.
    431     # NOTE:  at least on Tim's WinXP Pro SP2 desktop box, while the last
    432     # 6 bytes returned by UuidCreateSequential are fixed, they don't appear
    433     # to bear any relationship to the MAC address of any network device
    434     # on the box.
     402    # Thanks to Thomas Heller for ctypes and for his help with its use here.
    435403    try:
    436         lib = ctypes.windll.rpcrt4
     404        import ctypes
     405        import ctypes.util
     406
     407        global _ctypes_lib
     408
     409        uuid = None
     410        # The uuid_generate_* routines are provided by libuuid on at least
     411        # Linux and FreeBSD, and provided by libc on Mac OS X.
     412        for libname in ['uuid', 'c']:
     413            try:
     414                if _ctypes_lib is None:
     415                    _ctypes_lib = ctypes.CDLL(ctypes.util.find_library(libname))
     416                lib = _ctypes_lib
     417            except:
     418                continue
     419            if hasattr(lib, attr):
     420                uuid = getattr(lib, attr)
     421                break   # found what we were looking for
     422
     423        # The uuid_generate_* functions are broken on MacOS X 10.5, as noted
     424        # in issue #8621 the function generates the same sequence of values
     425        # in the parent process and all children created using fork (unless
     426        # those children use exec as well).
     427        #
     428        # Assume that the uuid_generate functions are broken from 10.5 onward,
     429        # the test can be adjusted when a later version is fixed.
     430        import sys
     431        if sys.platform == 'darwin':
     432            import os
     433            if int(os.uname()[2].split('.')[0]) >= 9:
     434                uuid = None
     435        return uuid
    437436    except:
    438         lib = None
    439     _UuidCreate = getattr(lib, 'UuidCreateSequential',
    440                           getattr(lib, 'UuidCreate', None))
    441 except:
    442     pass
     437        pass
     438
     439
     440def _uuid_create():
     441    """Get random UUID on Windows platform."""
     442
     443    try:
     444        # On Windows prior to 2000, UuidCreate gives a UUID containing the
     445        # hardware address.  On Windows 2000 and later, UuidCreate makes a
     446        # random UUID and UuidCreateSequential gives a UUID containing the
     447        # hardware address.  These routines are provided by the RPC runtime.
     448        # NOTE:  at least on Tim's WinXP Pro SP2 desktop box, while the last
     449        # 6 bytes returned by UuidCreateSequential are fixed, they don't appear
     450        # to bear any relationship to the MAC address of any network device
     451        # on the box.
     452        try:
     453            import ctypes
     454            lib = ctypes.windll.rpcrt4
     455        except:
     456            lib = None
     457        uuid = getattr(lib, 'UuidCreateSequential',
     458                       getattr(lib, 'UuidCreate', None))
     459        return uuid
     460    except:
     461        pass
    443462
    444463def _unixdll_getnode():
    445464    """Get the hardware address on Unix using ctypes."""
     465    import ctypes
    446466    _buffer = ctypes.create_string_buffer(16)
    447     _uuid_generate_time(_buffer)
     467    uuid_generate_time = _uuid_generate("uuid_generate_time")
     468    uuid_generate_time(_buffer)
    448469    return UUID(bytes=_buffer.raw).node
    449470
    450471def _windll_getnode():
    451472    """Get the hardware address on Windows using ctypes."""
     473    import ctypes
    452474    _buffer = ctypes.create_string_buffer(16)
    453     if _UuidCreate(_buffer) == 0:
     475    UuidCreate = _uuid_create()
     476    if UuidCreate(_buffer) == 0:
    454477        return UUID(bytes=_buffer.raw).node
    455478
    456479def _random_getnode():
     
    497520
    498521    # When the system provides a version-1 UUID generator, use it (but don't
    499522    # use UuidCreate here because its UUIDs don't conform to RFC 4122).
    500     if _uuid_generate_time and node is clock_seq is None:
     523    uuid_generate_time = _uuid_generate("uuid_generate_time")
     524
     525    if uuid_generate_time and node is clock_seq is None:
     526        import ctypes
    501527        _buffer = ctypes.create_string_buffer(16)
    502         _uuid_generate_time(_buffer)
     528        uuid_generate_time(_buffer)
    503529        return UUID(bytes=_buffer.raw)
    504530
    505531    global _last_timestamp
     
    534560    """Generate a random UUID."""
    535561
    536562    # When the system provides a version-4 UUID generator, use it.
    537     if _uuid_generate_random:
     563    uuid_generate_random = _uuid_generate("uuid_generate_random")
     564    if uuid_generate_random:
     565        import ctypes
    538566        _buffer = ctypes.create_string_buffer(16)
    539         _uuid_generate_random(_buffer)
     567        uuid_generate_random(_buffer)
    540568        return UUID(bytes=_buffer.raw)
    541569
    542570    # Otherwise, get randomness from urandom or the 'random' module.