diff -druN python-2.7.5.orig/Lib/uuid.py python-2.7.5/Lib/uuid.py
old
|
new
|
|
50 | 50 | 'reserved for NCS compatibility', 'specified in RFC 4122', |
51 | 51 | 'reserved for Microsoft compatibility', 'reserved for future definition'] |
52 | 52 | |
| 53 | |
53 | 54 | class UUID(object): |
54 | 55 | """Instances of the UUID class represent UUIDs as specified in RFC 4122. |
55 | 56 | UUID objects are immutable, hashable, and usable as dictionary keys. |
… |
… |
|
291 | 292 | |
292 | 293 | version = property(get_version) |
293 | 294 | |
| 295 | |
294 | 296 | def _find_mac(command, args, hw_identifiers, get_index): |
295 | 297 | import os |
296 | 298 | for dir in ['', '/sbin/', '/usr/sbin']: |
… |
… |
|
390 | 392 | return ((bytes[0]<<40L) + (bytes[1]<<32L) + (bytes[2]<<24L) + |
391 | 393 | (bytes[3]<<16L) + (bytes[4]<<8L) + bytes[5]) |
392 | 394 | |
393 | | # Thanks to Thomas Heller for ctypes and for his help with its use here. |
394 | 395 | |
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 |
399 | 397 | |
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 |
413 | 398 | |
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 |
| 399 | def _uuid_generate(attr): |
| 400 | """Find system routines for UUID generation""" |
426 | 401 | |
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. |
435 | 403 | 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 |
437 | 436 | except: |
438 | | lib = None |
439 | | _UuidCreate = getattr(lib, 'UuidCreateSequential', |
440 | | getattr(lib, 'UuidCreate', None)) |
441 | | except: |
442 | | pass |
| 437 | pass |
| 438 | |
| 439 | |
| 440 | def _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 |
443 | 462 | |
444 | 463 | def _unixdll_getnode(): |
445 | 464 | """Get the hardware address on Unix using ctypes.""" |
| 465 | import ctypes |
446 | 466 | _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) |
448 | 469 | return UUID(bytes=_buffer.raw).node |
449 | 470 | |
450 | 471 | def _windll_getnode(): |
451 | 472 | """Get the hardware address on Windows using ctypes.""" |
| 473 | import ctypes |
452 | 474 | _buffer = ctypes.create_string_buffer(16) |
453 | | if _UuidCreate(_buffer) == 0: |
| 475 | UuidCreate = _uuid_create() |
| 476 | if UuidCreate(_buffer) == 0: |
454 | 477 | return UUID(bytes=_buffer.raw).node |
455 | 478 | |
456 | 479 | def _random_getnode(): |
… |
… |
|
497 | 520 | |
498 | 521 | # When the system provides a version-1 UUID generator, use it (but don't |
499 | 522 | # 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 |
501 | 527 | _buffer = ctypes.create_string_buffer(16) |
502 | | _uuid_generate_time(_buffer) |
| 528 | uuid_generate_time(_buffer) |
503 | 529 | return UUID(bytes=_buffer.raw) |
504 | 530 | |
505 | 531 | global _last_timestamp |
… |
… |
|
534 | 560 | """Generate a random UUID.""" |
535 | 561 | |
536 | 562 | # 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 |
538 | 566 | _buffer = ctypes.create_string_buffer(16) |
539 | | _uuid_generate_random(_buffer) |
| 567 | uuid_generate_random(_buffer) |
540 | 568 | return UUID(bytes=_buffer.raw) |
541 | 569 | |
542 | 570 | # Otherwise, get randomness from urandom or the 'random' module. |