annotate Lib/uuid.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b36cb4602e21
children 6d8a5cbb94c9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
1 r"""UUID objects (universally unique identifiers) according to RFC 4122.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
2
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
3 This module provides immutable UUID objects (class UUID) and the functions
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
4 uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
5 UUIDs as specified in RFC 4122.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
6
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
7 If all you want is a unique ID, you should probably call uuid1() or uuid4().
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
8 Note that uuid1() may compromise privacy since it creates a UUID containing
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
9 the computer's network address. uuid4() creates a random UUID.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
10
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
11 Typical usage:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
12
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
13 >>> import uuid
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
14
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
15 # make a UUID based on the host ID and current time
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
16 >>> uuid.uuid1()
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
17 UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
18
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
19 # make a UUID using an MD5 hash of a namespace UUID and a name
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
20 >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
21 UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
22
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
23 # make a random UUID
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
24 >>> uuid.uuid4()
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
25 UUID('16fd2706-8baf-433b-82eb-8c7fada847da')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
26
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
27 # make a UUID using a SHA-1 hash of a namespace UUID and a name
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
28 >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
29 UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
30
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
31 # make a UUID from a string of hex digits (braces and hyphens ignored)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
32 >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
33
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
34 # convert a UUID to a string of hex digits in standard form
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
35 >>> str(x)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
36 '00010203-0405-0607-0809-0a0b0c0d0e0f'
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
37
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
38 # get the raw 16 bytes of the UUID
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
39 >>> x.bytes
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
40 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
41
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
42 # make a UUID from a 16-byte string
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
43 >>> uuid.UUID(bytes=x.bytes)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
44 UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
38884
5b987d21231b Remove Python 2.3 compatibility comment.
Ka-Ping Yee <ping@zesty.ca>
parents: 38783
diff changeset
45 """
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
46
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
47 __author__ = 'Ka-Ping Yee <ping@zesty.ca>'
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
48
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
49 RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
50 'reserved for NCS compatibility', 'specified in RFC 4122',
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
51 'reserved for Microsoft compatibility', 'reserved for future definition']
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
52
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
53 class UUID(object):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
54 """Instances of the UUID class represent UUIDs as specified in RFC 4122.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
55 UUID objects are immutable, hashable, and usable as dictionary keys.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
56 Converting a UUID to a string with str() yields something in the form
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
57 '12345678-1234-1234-1234-123456789abc'. The UUID constructor accepts
39513
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
58 five possible forms: a similar string of hexadecimal digits, or a tuple
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
59 of six integer fields (with 32-bit, 16-bit, 16-bit, 8-bit, 8-bit, and
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
60 48-bit values respectively) as an argument named 'fields', or a string
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
61 of 16 bytes (with all the integer fields in big-endian order) as an
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
62 argument named 'bytes', or a string of 16 bytes (with the first three
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
63 fields in little-endian order) as an argument named 'bytes_le', or a
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
64 single 128-bit integer as an argument named 'int'.
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
65
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
66 UUIDs have these read-only attributes:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
67
39513
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
68 bytes the UUID as a 16-byte string (containing the six
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
69 integer fields in big-endian byte order)
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
70
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
71 bytes_le the UUID as a 16-byte string (with time_low, time_mid,
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
72 and time_hi_version in little-endian byte order)
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
73
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
74 fields a tuple of the six integer fields of the UUID,
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
75 which are also available as six individual attributes
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
76 and two derived attributes:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
77
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
78 time_low the first 32 bits of the UUID
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
79 time_mid the next 16 bits of the UUID
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
80 time_hi_version the next 16 bits of the UUID
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
81 clock_seq_hi_variant the next 8 bits of the UUID
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
82 clock_seq_low the next 8 bits of the UUID
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
83 node the last 48 bits of the UUID
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
84
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
85 time the 60-bit timestamp
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
86 clock_seq the 14-bit sequence number
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
87
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
88 hex the UUID as a 32-character hexadecimal string
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
89
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
90 int the UUID as a 128-bit integer
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
91
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
92 urn the UUID as a URN as specified in RFC 4122
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
93
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
94 variant the UUID variant (one of the constants RESERVED_NCS,
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
95 RFC_4122, RESERVED_MICROSOFT, or RESERVED_FUTURE)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
96
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
97 version the UUID version number (1 through 5, meaningful only
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
98 when the variant is RFC_4122)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
99 """
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
100
39513
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
101 def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
102 int=None, version=None):
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
103 r"""Create a UUID from either a string of 32 hexadecimal digits,
39513
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
104 a string of 16 bytes as the 'bytes' argument, a string of 16 bytes
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
105 in little-endian order as the 'bytes_le' argument, a tuple of six
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
106 integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version,
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
107 8-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
108 the 'fields' argument, or a single 128-bit integer as the 'int'
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
109 argument. When a string of hex digits is given, curly braces,
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
110 hyphens, and a URN prefix are all optional. For example, these
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
111 expressions all yield the same UUID:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
112
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
113 UUID('{12345678-1234-5678-1234-567812345678}')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
114 UUID('12345678123456781234567812345678')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
115 UUID('urn:uuid:12345678-1234-5678-1234-567812345678')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
116 UUID(bytes='\x12\x34\x56\x78'*4)
39513
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
117 UUID(bytes_le='\x78\x56\x34\x12\x34\x12\x78\x56' +
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
118 '\x12\x34\x56\x78\x12\x34\x56\x78')
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
119 UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
120 UUID(int=0x12345678123456781234567812345678)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
121
39513
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
122 Exactly one of 'hex', 'bytes', 'bytes_le', 'fields', or 'int' must
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
123 be given. The 'version' argument is optional; if given, the resulting
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
124 UUID will have its variant and version set according to RFC 4122,
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
125 overriding the given 'hex', 'bytes', 'bytes_le', 'fields', or 'int'.
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
126 """
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
127
39513
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
128 if [hex, bytes, bytes_le, fields, int].count(None) != 4:
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
129 raise TypeError('need one of hex, bytes, bytes_le, fields, or int')
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
130 if hex is not None:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
131 hex = hex.replace('urn:', '').replace('uuid:', '')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
132 hex = hex.strip('{}').replace('-', '')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
133 if len(hex) != 32:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
134 raise ValueError('badly formed hexadecimal UUID string')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
135 int = long(hex, 16)
39513
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
136 if bytes_le is not None:
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
137 if len(bytes_le) != 16:
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
138 raise ValueError('bytes_le is not a 16-char string')
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
139 bytes = (bytes_le[3] + bytes_le[2] + bytes_le[1] + bytes_le[0] +
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
140 bytes_le[5] + bytes_le[4] + bytes_le[7] + bytes_le[6] +
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
141 bytes_le[8:])
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
142 if bytes is not None:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
143 if len(bytes) != 16:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
144 raise ValueError('bytes is not a 16-char string')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
145 int = long(('%02x'*16) % tuple(map(ord, bytes)), 16)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
146 if fields is not None:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
147 if len(fields) != 6:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
148 raise ValueError('fields is not a 6-tuple')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
149 (time_low, time_mid, time_hi_version,
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
150 clock_seq_hi_variant, clock_seq_low, node) = fields
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
151 if not 0 <= time_low < 1<<32L:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
152 raise ValueError('field 1 out of range (need a 32-bit value)')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
153 if not 0 <= time_mid < 1<<16L:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
154 raise ValueError('field 2 out of range (need a 16-bit value)')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
155 if not 0 <= time_hi_version < 1<<16L:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
156 raise ValueError('field 3 out of range (need a 16-bit value)')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
157 if not 0 <= clock_seq_hi_variant < 1<<8L:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
158 raise ValueError('field 4 out of range (need an 8-bit value)')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
159 if not 0 <= clock_seq_low < 1<<8L:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
160 raise ValueError('field 5 out of range (need an 8-bit value)')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
161 if not 0 <= node < 1<<48L:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
162 raise ValueError('field 6 out of range (need a 48-bit value)')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
163 clock_seq = (clock_seq_hi_variant << 8L) | clock_seq_low
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
164 int = ((time_low << 96L) | (time_mid << 80L) |
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
165 (time_hi_version << 64L) | (clock_seq << 48L) | node)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
166 if int is not None:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
167 if not 0 <= int < 1<<128L:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
168 raise ValueError('int is out of range (need a 128-bit value)')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
169 if version is not None:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
170 if not 1 <= version <= 5:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
171 raise ValueError('illegal version number')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
172 # Set the variant to RFC 4122.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
173 int &= ~(0xc000 << 48L)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
174 int |= 0x8000 << 48L
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
175 # Set the version number.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
176 int &= ~(0xf000 << 64L)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
177 int |= version << 76L
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
178 self.__dict__['int'] = int
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
179
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
180 def __cmp__(self, other):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
181 if isinstance(other, UUID):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
182 return cmp(self.int, other.int)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
183 return NotImplemented
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
184
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
185 def __hash__(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
186 return hash(self.int)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
187
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
188 def __int__(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
189 return self.int
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
190
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
191 def __repr__(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
192 return 'UUID(%r)' % str(self)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
193
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
194 def __setattr__(self, name, value):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
195 raise TypeError('UUID objects are immutable')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
196
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
197 def __str__(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
198 hex = '%032x' % self.int
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
199 return '%s-%s-%s-%s-%s' % (
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
200 hex[:8], hex[8:12], hex[12:16], hex[16:20], hex[20:])
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
201
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
202 def get_bytes(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
203 bytes = ''
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
204 for shift in range(0, 128, 8):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
205 bytes = chr((self.int >> shift) & 0xff) + bytes
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
206 return bytes
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
207
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
208 bytes = property(get_bytes)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
209
39513
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
210 def get_bytes_le(self):
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
211 bytes = self.bytes
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
212 return (bytes[3] + bytes[2] + bytes[1] + bytes[0] +
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
213 bytes[5] + bytes[4] + bytes[7] + bytes[6] + bytes[8:])
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
214
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
215 bytes_le = property(get_bytes_le)
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
216
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
217 def get_fields(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
218 return (self.time_low, self.time_mid, self.time_hi_version,
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
219 self.clock_seq_hi_variant, self.clock_seq_low, self.node)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
220
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
221 fields = property(get_fields)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
222
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
223 def get_time_low(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
224 return self.int >> 96L
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
225
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
226 time_low = property(get_time_low)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
227
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
228 def get_time_mid(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
229 return (self.int >> 80L) & 0xffff
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
230
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
231 time_mid = property(get_time_mid)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
232
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
233 def get_time_hi_version(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
234 return (self.int >> 64L) & 0xffff
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
235
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
236 time_hi_version = property(get_time_hi_version)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
237
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
238 def get_clock_seq_hi_variant(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
239 return (self.int >> 56L) & 0xff
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
240
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
241 clock_seq_hi_variant = property(get_clock_seq_hi_variant)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
242
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
243 def get_clock_seq_low(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
244 return (self.int >> 48L) & 0xff
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
245
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
246 clock_seq_low = property(get_clock_seq_low)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
247
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
248 def get_time(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
249 return (((self.time_hi_version & 0x0fffL) << 48L) |
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
250 (self.time_mid << 32L) | self.time_low)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
251
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
252 time = property(get_time)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
253
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
254 def get_clock_seq(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
255 return (((self.clock_seq_hi_variant & 0x3fL) << 8L) |
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
256 self.clock_seq_low)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
257
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
258 clock_seq = property(get_clock_seq)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
259
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
260 def get_node(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
261 return self.int & 0xffffffffffff
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
262
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
263 node = property(get_node)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
264
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
265 def get_hex(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
266 return '%032x' % self.int
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
267
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
268 hex = property(get_hex)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
269
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
270 def get_urn(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
271 return 'urn:uuid:' + str(self)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
272
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
273 urn = property(get_urn)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
274
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
275 def get_variant(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
276 if not self.int & (0x8000 << 48L):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
277 return RESERVED_NCS
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
278 elif not self.int & (0x4000 << 48L):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
279 return RFC_4122
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
280 elif not self.int & (0x2000 << 48L):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
281 return RESERVED_MICROSOFT
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
282 else:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
283 return RESERVED_FUTURE
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
284
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
285 variant = property(get_variant)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
286
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
287 def get_version(self):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
288 # The version bits are only meaningful for RFC 4122 UUIDs.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
289 if self.variant == RFC_4122:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
290 return int((self.int >> 76L) & 0xf)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
291
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
292 version = property(get_version)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
293
39240
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
294 def _find_mac(command, args, hw_identifiers, get_index):
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
295 import os
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
296 for dir in ['', '/sbin/', '/usr/sbin']:
39287
6a5f79a4e687 If the executable doesn't exist, there's no reason to try to start it.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39240
diff changeset
297 executable = os.path.join(dir, command)
6a5f79a4e687 If the executable doesn't exist, there's no reason to try to start it.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39240
diff changeset
298 if not os.path.exists(executable):
6a5f79a4e687 If the executable doesn't exist, there's no reason to try to start it.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39240
diff changeset
299 continue
6a5f79a4e687 If the executable doesn't exist, there's no reason to try to start it.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39240
diff changeset
300
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
301 try:
39210
a110a4462189 Make uuid test suite pass on this box by requesting output with LC_ALL=C.
Georg Brandl <georg@python.org>
parents: 38884
diff changeset
302 # LC_ALL to get English output, 2>/dev/null to
a110a4462189 Make uuid test suite pass on this box by requesting output with LC_ALL=C.
Georg Brandl <georg@python.org>
parents: 38884
diff changeset
303 # prevent output on stderr
39240
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
304 cmd = 'LC_ALL=C %s %s 2>/dev/null' % (executable, args)
65793
305e93fd5b6d Merged revisions 86009 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 62544
diff changeset
305 with os.popen(cmd) as pipe:
305e93fd5b6d Merged revisions 86009 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 62544
diff changeset
306 for line in pipe:
305e93fd5b6d Merged revisions 86009 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 62544
diff changeset
307 words = line.lower().split()
305e93fd5b6d Merged revisions 86009 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 62544
diff changeset
308 for i in range(len(words)):
305e93fd5b6d Merged revisions 86009 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 62544
diff changeset
309 if words[i] in hw_identifiers:
305e93fd5b6d Merged revisions 86009 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 62544
diff changeset
310 return int(
305e93fd5b6d Merged revisions 86009 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 62544
diff changeset
311 words[get_index(i)].replace(':', ''), 16)
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
312 except IOError:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
313 continue
39240
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
314 return None
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
315
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
316 def _ifconfig_getnode():
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
317 """Get the hardware address on Unix by running ifconfig."""
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
318
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
319 # This works on Linux ('' or '-a'), Tru64 ('-av'), but not all Unixes.
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
320 for args in ('', '-a', '-av'):
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
321 mac = _find_mac('ifconfig', args, ['hwaddr', 'ether'], lambda i: i+1)
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
322 if mac:
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
323 return mac
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
324
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
325 import socket
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
326 ip_addr = socket.gethostbyname(socket.gethostname())
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
327
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
328 # Try getting the MAC addr from arp based on our IP address (Solaris).
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
329 mac = _find_mac('arp', '-an', [ip_addr], lambda i: -1)
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
330 if mac:
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
331 return mac
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
332
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
333 # This might work on HP-UX.
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
334 mac = _find_mac('lanscan', '-ai', ['lan0'], lambda i: 0)
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
335 if mac:
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
336 return mac
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
337
bf1984c13e2f Try to find the MAC addr on various flavours of Unix. This seems hopeless.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39239
diff changeset
338 return None
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
339
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
340 def _ipconfig_getnode():
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
341 """Get the hardware address on Windows by running ipconfig.exe."""
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
342 import os, re
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
343 dirs = ['', r'c:\windows\system32', r'c:\winnt\system32']
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
344 try:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
345 import ctypes
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
346 buffer = ctypes.create_string_buffer(300)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
347 ctypes.windll.kernel32.GetSystemDirectoryA(buffer, 300)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
348 dirs.insert(0, buffer.value.decode('mbcs'))
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
349 except:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
350 pass
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
351 for dir in dirs:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
352 try:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
353 pipe = os.popen(os.path.join(dir, 'ipconfig') + ' /all')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
354 except IOError:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
355 continue
65964
7d28087fbaf3 Merged revisions 86192 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65793
diff changeset
356 else:
7d28087fbaf3 Merged revisions 86192 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65793
diff changeset
357 for line in pipe:
7d28087fbaf3 Merged revisions 86192 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65793
diff changeset
358 value = line.split(':')[-1].strip().lower()
7d28087fbaf3 Merged revisions 86192 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65793
diff changeset
359 if re.match('([0-9a-f][0-9a-f]-){5}[0-9a-f][0-9a-f]', value):
7d28087fbaf3 Merged revisions 86192 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65793
diff changeset
360 return int(value.replace('-', ''), 16)
7d28087fbaf3 Merged revisions 86192 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65793
diff changeset
361 finally:
7d28087fbaf3 Merged revisions 86192 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65793
diff changeset
362 pipe.close()
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
363
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
364 def _netbios_getnode():
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
365 """Get the hardware address on Windows using NetBIOS calls.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
366 See http://support.microsoft.com/kb/118623 for details."""
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
367 import win32wnet, netbios
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
368 ncb = netbios.NCB()
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
369 ncb.Command = netbios.NCBENUM
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
370 ncb.Buffer = adapters = netbios.LANA_ENUM()
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
371 adapters._pack()
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
372 if win32wnet.Netbios(ncb) != 0:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
373 return
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
374 adapters._unpack()
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
375 for i in range(adapters.length):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
376 ncb.Reset()
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
377 ncb.Command = netbios.NCBRESET
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
378 ncb.Lana_num = ord(adapters.lana[i])
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
379 if win32wnet.Netbios(ncb) != 0:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
380 continue
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
381 ncb.Reset()
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
382 ncb.Command = netbios.NCBASTAT
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
383 ncb.Lana_num = ord(adapters.lana[i])
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
384 ncb.Callname = '*'.ljust(16)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
385 ncb.Buffer = status = netbios.ADAPTER_STATUS()
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
386 if win32wnet.Netbios(ncb) != 0:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
387 continue
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
388 status._unpack()
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
389 bytes = map(ord, status.adapter_address)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
390 return ((bytes[0]<<40L) + (bytes[1]<<32L) + (bytes[2]<<24L) +
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
391 (bytes[3]<<16L) + (bytes[4]<<8L) + bytes[5])
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
392
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
393 # Thanks to Thomas Heller for ctypes and for his help with its use here.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
394
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
395 # If ctypes is available, use it to find system routines for UUID generation.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
396 _uuid_generate_random = _uuid_generate_time = _UuidCreate = None
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
397 try:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
398 import ctypes, ctypes.util
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
399
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
400 # The uuid_generate_* routines are provided by libuuid on at least
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
401 # Linux and FreeBSD, and provided by libc on Mac OS X.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
402 for libname in ['uuid', 'c']:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
403 try:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
404 lib = ctypes.CDLL(ctypes.util.find_library(libname))
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
405 except:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
406 continue
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
407 if hasattr(lib, 'uuid_generate_random'):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
408 _uuid_generate_random = lib.uuid_generate_random
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
409 if hasattr(lib, 'uuid_generate_time'):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
410 _uuid_generate_time = lib.uuid_generate_time
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
411
62545
c50563824094 Merged revisions 82276 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 61239
diff changeset
412 # The uuid_generate_* functions are broken on MacOS X 10.5, as noted
61239
813daee94f73 Merged revisions 80784 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 49794
diff changeset
413 # in issue #8621 the function generates the same sequence of values
813daee94f73 Merged revisions 80784 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 49794
diff changeset
414 # in the parent process and all children created using fork (unless
813daee94f73 Merged revisions 80784 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 49794
diff changeset
415 # those children use exec as well).
813daee94f73 Merged revisions 80784 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 49794
diff changeset
416 #
62545
c50563824094 Merged revisions 82276 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 61239
diff changeset
417 # Assume that the uuid_generate functions are broken from 10.5 onward,
61239
813daee94f73 Merged revisions 80784 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 49794
diff changeset
418 # the test can be adjusted when a later version is fixed.
813daee94f73 Merged revisions 80784 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 49794
diff changeset
419 import sys
813daee94f73 Merged revisions 80784 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 49794
diff changeset
420 if sys.platform == 'darwin':
813daee94f73 Merged revisions 80784 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 49794
diff changeset
421 import os
62545
c50563824094 Merged revisions 82276 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 61239
diff changeset
422 if int(os.uname()[2].split('.')[0]) >= 9:
61239
813daee94f73 Merged revisions 80784 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 49794
diff changeset
423 _uuid_generate_random = _uuid_generate_time = None
813daee94f73 Merged revisions 80784 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 49794
diff changeset
424
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
425 # On Windows prior to 2000, UuidCreate gives a UUID containing the
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
426 # hardware address. On Windows 2000 and later, UuidCreate makes a
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
427 # random UUID and UuidCreateSequential gives a UUID containing the
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
428 # hardware address. These routines are provided by the RPC runtime.
39239
55865d7bc6ea Live with that "the hardware address" is an ill-defined
Tim Peters <tim.peters@gmail.com>
parents: 39210
diff changeset
429 # NOTE: at least on Tim's WinXP Pro SP2 desktop box, while the last
55865d7bc6ea Live with that "the hardware address" is an ill-defined
Tim Peters <tim.peters@gmail.com>
parents: 39210
diff changeset
430 # 6 bytes returned by UuidCreateSequential are fixed, they don't appear
55865d7bc6ea Live with that "the hardware address" is an ill-defined
Tim Peters <tim.peters@gmail.com>
parents: 39210
diff changeset
431 # to bear any relationship to the MAC address of any network device
55865d7bc6ea Live with that "the hardware address" is an ill-defined
Tim Peters <tim.peters@gmail.com>
parents: 39210
diff changeset
432 # on the box.
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
433 try:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
434 lib = ctypes.windll.rpcrt4
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
435 except:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
436 lib = None
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
437 _UuidCreate = getattr(lib, 'UuidCreateSequential',
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
438 getattr(lib, 'UuidCreate', None))
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
439 except:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
440 pass
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
441
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
442 def _unixdll_getnode():
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
443 """Get the hardware address on Unix using ctypes."""
42771
7323331876dd uuid creation is now threadsafe, backport from py3k rev. 57375.
Georg Brandl <georg@python.org>
parents: 41924
diff changeset
444 _buffer = ctypes.create_string_buffer(16)
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
445 _uuid_generate_time(_buffer)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
446 return UUID(bytes=_buffer.raw).node
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
447
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
448 def _windll_getnode():
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
449 """Get the hardware address on Windows using ctypes."""
42771
7323331876dd uuid creation is now threadsafe, backport from py3k rev. 57375.
Georg Brandl <georg@python.org>
parents: 41924
diff changeset
450 _buffer = ctypes.create_string_buffer(16)
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
451 if _UuidCreate(_buffer) == 0:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
452 return UUID(bytes=_buffer.raw).node
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
453
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
454 def _random_getnode():
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
455 """Get a random node ID, with eighth bit set as suggested by RFC 4122."""
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
456 import random
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
457 return random.randrange(0, 1<<48L) | 0x010000000000L
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
458
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
459 _node = None
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
460
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
461 def getnode():
39239
55865d7bc6ea Live with that "the hardware address" is an ill-defined
Tim Peters <tim.peters@gmail.com>
parents: 39210
diff changeset
462 """Get the hardware address as a 48-bit positive integer.
55865d7bc6ea Live with that "the hardware address" is an ill-defined
Tim Peters <tim.peters@gmail.com>
parents: 39210
diff changeset
463
55865d7bc6ea Live with that "the hardware address" is an ill-defined
Tim Peters <tim.peters@gmail.com>
parents: 39210
diff changeset
464 The first time this runs, it may launch a separate program, which could
55865d7bc6ea Live with that "the hardware address" is an ill-defined
Tim Peters <tim.peters@gmail.com>
parents: 39210
diff changeset
465 be quite slow. If all attempts to obtain the hardware address fail, we
55865d7bc6ea Live with that "the hardware address" is an ill-defined
Tim Peters <tim.peters@gmail.com>
parents: 39210
diff changeset
466 choose a random 48-bit number with its eighth bit set to 1 as recommended
55865d7bc6ea Live with that "the hardware address" is an ill-defined
Tim Peters <tim.peters@gmail.com>
parents: 39210
diff changeset
467 in RFC 4122.
55865d7bc6ea Live with that "the hardware address" is an ill-defined
Tim Peters <tim.peters@gmail.com>
parents: 39210
diff changeset
468 """
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
469
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
470 global _node
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
471 if _node is not None:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
472 return _node
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
473
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
474 import sys
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
475 if sys.platform == 'win32':
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
476 getters = [_windll_getnode, _netbios_getnode, _ipconfig_getnode]
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
477 else:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
478 getters = [_unixdll_getnode, _ifconfig_getnode]
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
479
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
480 for getter in getters + [_random_getnode]:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
481 try:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
482 _node = getter()
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
483 except:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
484 continue
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
485 if _node is not None:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
486 return _node
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
487
39513
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
488 _last_timestamp = None
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
489
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
490 def uuid1(node=None, clock_seq=None):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
491 """Generate a UUID from a host ID, sequence number, and the current time.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
492 If 'node' is not given, getnode() is used to obtain the hardware
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
493 address. If 'clock_seq' is given, it is used as the sequence number;
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
494 otherwise a random 14-bit sequence number is chosen."""
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
495
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
496 # When the system provides a version-1 UUID generator, use it (but don't
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
497 # use UuidCreate here because its UUIDs don't conform to RFC 4122).
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
498 if _uuid_generate_time and node is clock_seq is None:
49794
3250fb71a9e0 Merged revisions 67318 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 42771
diff changeset
499 _buffer = ctypes.create_string_buffer(16)
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
500 _uuid_generate_time(_buffer)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
501 return UUID(bytes=_buffer.raw)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
502
39513
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
503 global _last_timestamp
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
504 import time
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
505 nanoseconds = int(time.time() * 1e9)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
506 # 0x01b21dd213814000 is the number of 100-ns intervals between the
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
507 # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
63639
4c0b37d869ca Merged revisions 75407,75409-75413,75415,75419-75421 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62545
diff changeset
508 timestamp = int(nanoseconds//100) + 0x01b21dd213814000L
4c0b37d869ca Merged revisions 75407,75409-75413,75415,75419-75421 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62545
diff changeset
509 if _last_timestamp is not None and timestamp <= _last_timestamp:
39554
a87320ebd0e6 Bug #1541863: uuid.uuid1 failed to generate unique identifiers
Martin v. Löwis <martin@v.loewis.de>
parents: 39513
diff changeset
510 timestamp = _last_timestamp + 1
39513
9133076c0bf9 Update code and tests to support the 'bytes_le' attribute (for
Ka-Ping Yee <ping@zesty.ca>
parents: 39287
diff changeset
511 _last_timestamp = timestamp
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
512 if clock_seq is None:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
513 import random
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
514 clock_seq = random.randrange(1<<14L) # instead of stable storage
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
515 time_low = timestamp & 0xffffffffL
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
516 time_mid = (timestamp >> 32L) & 0xffffL
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
517 time_hi_version = (timestamp >> 48L) & 0x0fffL
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
518 clock_seq_low = clock_seq & 0xffL
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
519 clock_seq_hi_variant = (clock_seq >> 8L) & 0x3fL
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
520 if node is None:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
521 node = getnode()
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
522 return UUID(fields=(time_low, time_mid, time_hi_version,
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
523 clock_seq_hi_variant, clock_seq_low, node), version=1)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
524
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
525 def uuid3(namespace, name):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
526 """Generate a UUID from the MD5 hash of a namespace UUID and a name."""
41917
0e29bb80e227 Have md5 raise a DeprecationWarning as per PEP 4.
Brett Cannon <bcannon@gmail.com>
parents: 39554
diff changeset
527 from hashlib import md5
0e29bb80e227 Have md5 raise a DeprecationWarning as per PEP 4.
Brett Cannon <bcannon@gmail.com>
parents: 39554
diff changeset
528 hash = md5(namespace.bytes + name).digest()
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
529 return UUID(bytes=hash[:16], version=3)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
530
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
531 def uuid4():
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
532 """Generate a random UUID."""
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
533
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
534 # When the system provides a version-4 UUID generator, use it.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
535 if _uuid_generate_random:
49794
3250fb71a9e0 Merged revisions 67318 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 42771
diff changeset
536 _buffer = ctypes.create_string_buffer(16)
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
537 _uuid_generate_random(_buffer)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
538 return UUID(bytes=_buffer.raw)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
539
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
540 # Otherwise, get randomness from urandom or the 'random' module.
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
541 try:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
542 import os
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
543 return UUID(bytes=os.urandom(16), version=4)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
544 except:
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
545 import random
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
546 bytes = [chr(random.randrange(256)) for i in range(16)]
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
547 return UUID(bytes=bytes, version=4)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
548
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
549 def uuid5(namespace, name):
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
550 """Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
41924
b380c9c49738 Have the sha module raise a DeprecationWarning as specified in PEP 4.
Brett Cannon <bcannon@gmail.com>
parents: 41917
diff changeset
551 from hashlib import sha1
b380c9c49738 Have the sha module raise a DeprecationWarning as specified in PEP 4.
Brett Cannon <bcannon@gmail.com>
parents: 41917
diff changeset
552 hash = sha1(namespace.bytes + name).digest()
38783
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
553 return UUID(bytes=hash[:16], version=5)
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
554
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
555 # The following standard UUIDs are for use with uuid3() or uuid5().
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
556
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
557 NAMESPACE_DNS = UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
558 NAMESPACE_URL = UUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
559 NAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8')
765fb1ccc3d8 Added missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 38782
diff changeset
560 NAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8')