Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(41725)

Side by Side Diff: Lib/uuid.py

Issue 20519: ctypes.create_string_buffer creates reference cycles
Patch Set: Created 3 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Modules/_uuidmodule.c » ('j') | Modules/_uuidmodule.c » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 r"""UUID objects (universally unique identifiers) according to RFC 4122. 1 r"""UUID objects (universally unique identifiers) according to RFC 4122.
2 2
3 This module provides immutable UUID objects (class UUID) and the functions 3 This module provides immutable UUID objects (class UUID) and the functions
4 uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5 4 uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5
5 UUIDs as specified in RFC 4122. 5 UUIDs as specified in RFC 4122.
6 6
7 If all you want is a unique ID, you should probably call uuid1() or uuid4(). 7 If all you want is a unique ID, you should probably call uuid1() or uuid4().
8 Note that uuid1() may compromise privacy since it creates a UUID containing 8 Note that uuid1() may compromise privacy since it creates a UUID containing
9 the computer's network address. uuid4() creates a random UUID. 9 the computer's network address. uuid4() creates a random UUID.
10 10
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 (bytes[3]<<16) + (bytes[4]<<8) + bytes[5]) 421 (bytes[3]<<16) + (bytes[4]<<8) + bytes[5])
422 422
423 # Thanks to Thomas Heller for ctypes and for his help with its use here. 423 # Thanks to Thomas Heller for ctypes and for his help with its use here.
424 424
425 # If ctypes is available, use it to find system routines for UUID generation. 425 # If ctypes is available, use it to find system routines for UUID generation.
426 # XXX This makes the module non-thread-safe! 426 # XXX This makes the module non-thread-safe!
427 _uuid_generate_random = _uuid_generate_time = _UuidCreate = None 427 _uuid_generate_random = _uuid_generate_time = _UuidCreate = None
428 try: 428 try:
429 import ctypes, ctypes.util 429 import ctypes, ctypes.util
430 430
431 # The uuid_generate_* routines are provided by libuuid on at least 431 import sys
432 # Linux and FreeBSD, and provided by libc on Mac OS X. 432 if sys.platform == "darwin" and int(os.uname().release.split('.')[0]) < 9:
433 for libname in ['uuid', 'c']: 433 # The uuid_generate_* functions are broken on MacOS X 10.5, as noted
434 # in issue #8621 the function generates the same sequence of values
435 # in the parent process and all children created using fork (unless
436 # those children use exec as well).
437 #
438 # Assume that the uuid_generate functions are broken from 10.5 onward,
439 # the test can be adjusted when a later version is fixed.
440 _uuid = None
441 else:
434 try: 442 try:
435 lib = ctypes.CDLL(ctypes.util.find_library(libname)) 443 import _uuid
436 except: 444 except ImportError:
437 continue 445 _uuid = None
438 if hasattr(lib, 'uuid_generate_random'):
439 _uuid_generate_random = lib.uuid_generate_random
440 if hasattr(lib, 'uuid_generate_time'):
441 _uuid_generate_time = lib.uuid_generate_time
442 if _uuid_generate_random is not None:
443 break # found everything we were looking for
444
445 # The uuid_generate_* functions are broken on MacOS X 10.5, as noted
446 # in issue #8621 the function generates the same sequence of values
447 # in the parent process and all children created using fork (unless
448 # those children use exec as well).
449 #
450 # Assume that the uuid_generate functions are broken from 10.5 onward,
451 # the test can be adjusted when a later version is fixed.
452 import sys
453 if sys.platform == 'darwin':
454 import os
455 if int(os.uname().release.split('.')[0]) >= 9:
456 _uuid_generate_random = _uuid_generate_time = None
457 446
458 # On Windows prior to 2000, UuidCreate gives a UUID containing the 447 # On Windows prior to 2000, UuidCreate gives a UUID containing the
459 # hardware address. On Windows 2000 and later, UuidCreate makes a 448 # hardware address. On Windows 2000 and later, UuidCreate makes a
460 # random UUID and UuidCreateSequential gives a UUID containing the 449 # random UUID and UuidCreateSequential gives a UUID containing the
461 # hardware address. These routines are provided by the RPC runtime. 450 # hardware address. These routines are provided by the RPC runtime.
462 # NOTE: at least on Tim's WinXP Pro SP2 desktop box, while the last 451 # NOTE: at least on Tim's WinXP Pro SP2 desktop box, while the last
463 # 6 bytes returned by UuidCreateSequential are fixed, they don't appear 452 # 6 bytes returned by UuidCreateSequential are fixed, they don't appear
464 # to bear any relationship to the MAC address of any network device 453 # to bear any relationship to the MAC address of any network device
465 # on the box. 454 # on the box.
455 import ctypes, ctypes.util
haypo 2015/10/27 03:35:14 Maybe we should make this code specific to Windows
gustavo 2015/10/28 14:33:01 I made it so that ctypes is not imported if the _u
466 try: 456 try:
467 lib = ctypes.windll.rpcrt4 457 lib = ctypes.windll.rpcrt4
468 except: 458 except:
469 lib = None 459 lib = None
470 _UuidCreate = getattr(lib, 'UuidCreateSequential', 460 _UuidCreate = getattr(lib, 'UuidCreateSequential',
471 getattr(lib, 'UuidCreate', None)) 461 getattr(lib, 'UuidCreate', None))
472 except: 462 except:
473 pass 463 pass
474 464
475 def _unixdll_getnode(): 465 def _unixdll_getnode():
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 def uuid3(namespace, name): 548 def uuid3(namespace, name):
559 """Generate a UUID from the MD5 hash of a namespace UUID and a name.""" 549 """Generate a UUID from the MD5 hash of a namespace UUID and a name."""
560 from hashlib import md5 550 from hashlib import md5
561 hash = md5(namespace.bytes + bytes(name, "utf-8")).digest() 551 hash = md5(namespace.bytes + bytes(name, "utf-8")).digest()
562 return UUID(bytes=hash[:16], version=3) 552 return UUID(bytes=hash[:16], version=3)
563 553
564 def uuid4(): 554 def uuid4():
565 """Generate a random UUID.""" 555 """Generate a random UUID."""
566 556
567 # When the system provides a version-4 UUID generator, use it. 557 # When the system provides a version-4 UUID generator, use it.
568 if _uuid_generate_random: 558 if _uuid is not None:
569 _buffer = ctypes.create_string_buffer(16) 559 return UUID(bytes=_uuid.generate_random())
570 _uuid_generate_random(_buffer)
571 return UUID(bytes=bytes_(_buffer.raw))
572 560
573 # Otherwise, get randomness from urandom or the 'random' module. 561 # Otherwise, get randomness from urandom or the 'random' module.
574 try: 562 try:
575 import os 563 import os
576 return UUID(bytes=os.urandom(16), version=4) 564 return UUID(bytes=os.urandom(16), version=4)
577 except: 565 except:
578 import random 566 import random
579 bytes = bytes_(random.randrange(256) for i in range(16)) 567 bytes = bytes_(random.randrange(256) for i in range(16))
580 return UUID(bytes=bytes, version=4) 568 return UUID(bytes=bytes, version=4)
581 569
582 def uuid5(namespace, name): 570 def uuid5(namespace, name):
583 """Generate a UUID from the SHA-1 hash of a namespace UUID and a name.""" 571 """Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
584 from hashlib import sha1 572 from hashlib import sha1
585 hash = sha1(namespace.bytes + bytes(name, "utf-8")).digest() 573 hash = sha1(namespace.bytes + bytes(name, "utf-8")).digest()
586 return UUID(bytes=hash[:16], version=5) 574 return UUID(bytes=hash[:16], version=5)
587 575
588 # The following standard UUIDs are for use with uuid3() or uuid5(). 576 # The following standard UUIDs are for use with uuid3() or uuid5().
589 577
590 NAMESPACE_DNS = UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8') 578 NAMESPACE_DNS = UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')
591 NAMESPACE_URL = UUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8') 579 NAMESPACE_URL = UUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8')
592 NAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8') 580 NAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8')
593 NAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8') 581 NAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8')
OLDNEW
« no previous file with comments | « no previous file | Modules/_uuidmodule.c » ('j') | Modules/_uuidmodule.c » ('J')

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+