Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 65b6a80

Browse files
committed
Make test_uuid passed.
Change the UUID properties to use @Property. Change the UUID API so that .bytes and .bytes_le return, and the corresponding arguments to __init__() require, values of type 'bytes'.
1 parent 5e23d57 commit 65b6a80

2 files changed

Lines changed: 79 additions & 88 deletions

File tree

Lib/test/test_uuid.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,125 +21,125 @@ def test_UUID(self):
2121
('00000000-0000-0000-0000-000000000000',
2222
'{00000000-0000-0000-0000-000000000000}',
2323
'00000000000000000000000000000000',
24-
'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
25-
'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
24+
b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
25+
b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0',
2626
(0, 0, 0, 0, 0, 0),
2727
0,
2828
'urn:uuid:00000000-0000-0000-0000-000000000000',
2929
0, 0, uuid.RESERVED_NCS, None),
3030
('00010203-0405-0607-0809-0a0b0c0d0e0f',
3131
'{00010203-0405-0607-0809-0a0b0c0d0e0f}',
3232
'000102030405060708090a0b0c0d0e0f',
33-
'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
34-
'\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f',
33+
b'\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x0d\x0e\x0f',
34+
b'\x03\x02\x01\0\x05\x04\x07\x06\x08\t\n\x0b\x0c\x0d\x0e\x0f',
3535
(0x00010203, 0x0405, 0x0607, 8, 9, 0x0a0b0c0d0e0f),
3636
0x000102030405060708090a0b0c0d0e0f,
3737
'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f',
3838
0x607040500010203, 0x809, uuid.RESERVED_NCS, None),
3939
('02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
4040
'{02d9e6d5-9467-382e-8f9b-9300a64ac3cd}',
4141
'02d9e6d59467382e8f9b9300a64ac3cd',
42-
'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
43-
'\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
42+
b'\x02\xd9\xe6\xd5\x94\x67\x38\x2e\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
43+
b'\xd5\xe6\xd9\x02\x67\x94\x2e\x38\x8f\x9b\x93\x00\xa6\x4a\xc3\xcd',
4444
(0x02d9e6d5, 0x9467, 0x382e, 0x8f, 0x9b, 0x9300a64ac3cd),
4545
0x02d9e6d59467382e8f9b9300a64ac3cd,
4646
'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd',
4747
0x82e946702d9e6d5, 0xf9b, uuid.RFC_4122, 3),
4848
('12345678-1234-5678-1234-567812345678',
4949
'{12345678-1234-5678-1234-567812345678}',
5050
'12345678123456781234567812345678',
51-
'\x12\x34\x56\x78'*4,
52-
'\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
51+
b'\x12\x34\x56\x78'*4,
52+
b'\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78',
5353
(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678),
5454
0x12345678123456781234567812345678,
5555
'urn:uuid:12345678-1234-5678-1234-567812345678',
5656
0x678123412345678, 0x1234, uuid.RESERVED_NCS, None),
5757
('6ba7b810-9dad-11d1-80b4-00c04fd430c8',
5858
'{6ba7b810-9dad-11d1-80b4-00c04fd430c8}',
5959
'6ba7b8109dad11d180b400c04fd430c8',
60-
'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
61-
'\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
60+
b'\x6b\xa7\xb8\x10\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
61+
b'\x10\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
6262
(0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
6363
0x6ba7b8109dad11d180b400c04fd430c8,
6464
'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8',
6565
0x1d19dad6ba7b810, 0xb4, uuid.RFC_4122, 1),
6666
('6ba7b811-9dad-11d1-80b4-00c04fd430c8',
6767
'{6ba7b811-9dad-11d1-80b4-00c04fd430c8}',
6868
'6ba7b8119dad11d180b400c04fd430c8',
69-
'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
70-
'\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
69+
b'\x6b\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
70+
b'\x11\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
7171
(0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
7272
0x6ba7b8119dad11d180b400c04fd430c8,
7373
'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8',
7474
0x1d19dad6ba7b811, 0xb4, uuid.RFC_4122, 1),
7575
('6ba7b812-9dad-11d1-80b4-00c04fd430c8',
7676
'{6ba7b812-9dad-11d1-80b4-00c04fd430c8}',
7777
'6ba7b8129dad11d180b400c04fd430c8',
78-
'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
79-
'\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
78+
b'\x6b\xa7\xb8\x12\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
79+
b'\x12\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
8080
(0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
8181
0x6ba7b8129dad11d180b400c04fd430c8,
8282
'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8',
8383
0x1d19dad6ba7b812, 0xb4, uuid.RFC_4122, 1),
8484
('6ba7b814-9dad-11d1-80b4-00c04fd430c8',
8585
'{6ba7b814-9dad-11d1-80b4-00c04fd430c8}',
8686
'6ba7b8149dad11d180b400c04fd430c8',
87-
'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
88-
'\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
87+
b'\x6b\xa7\xb8\x14\x9d\xad\x11\xd1\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
88+
b'\x14\xb8\xa7\x6b\xad\x9d\xd1\x11\x80\xb4\x00\xc0\x4f\xd4\x30\xc8',
8989
(0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00c04fd430c8),
9090
0x6ba7b8149dad11d180b400c04fd430c8,
9191
'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8',
9292
0x1d19dad6ba7b814, 0xb4, uuid.RFC_4122, 1),
9393
('7d444840-9dc0-11d1-b245-5ffdce74fad2',
9494
'{7d444840-9dc0-11d1-b245-5ffdce74fad2}',
9595
'7d4448409dc011d1b2455ffdce74fad2',
96-
'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
97-
'\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
96+
b'\x7d\x44\x48\x40\x9d\xc0\x11\xd1\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
97+
b'\x40\x48\x44\x7d\xc0\x9d\xd1\x11\xb2\x45\x5f\xfd\xce\x74\xfa\xd2',
9898
(0x7d444840, 0x9dc0, 0x11d1, 0xb2, 0x45, 0x5ffdce74fad2),
9999
0x7d4448409dc011d1b2455ffdce74fad2,
100100
'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2',
101101
0x1d19dc07d444840, 0x3245, uuid.RFC_4122, 1),
102102
('e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
103103
'{e902893a-9d22-3c7e-a7b8-d6e313b71d9f}',
104104
'e902893a9d223c7ea7b8d6e313b71d9f',
105-
'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
106-
'\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
105+
b'\xe9\x02\x89\x3a\x9d\x22\x3c\x7e\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
106+
b'\x3a\x89\x02\xe9\x22\x9d\x7e\x3c\xa7\xb8\xd6\xe3\x13\xb7\x1d\x9f',
107107
(0xe902893a, 0x9d22, 0x3c7e, 0xa7, 0xb8, 0xd6e313b71d9f),
108108
0xe902893a9d223c7ea7b8d6e313b71d9f,
109109
'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f',
110110
0xc7e9d22e902893a, 0x27b8, uuid.RFC_4122, 3),
111111
('eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
112112
'{eb424026-6f54-4ef8-a4d0-bb658a1fc6cf}',
113113
'eb4240266f544ef8a4d0bb658a1fc6cf',
114-
'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
115-
'\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
114+
b'\xeb\x42\x40\x26\x6f\x54\x4e\xf8\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
115+
b'\x26\x40\x42\xeb\x54\x6f\xf8\x4e\xa4\xd0\xbb\x65\x8a\x1f\xc6\xcf',
116116
(0xeb424026, 0x6f54, 0x4ef8, 0xa4, 0xd0, 0xbb658a1fc6cf),
117117
0xeb4240266f544ef8a4d0bb658a1fc6cf,
118118
'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf',
119119
0xef86f54eb424026, 0x24d0, uuid.RFC_4122, 4),
120120
('f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
121121
'{f81d4fae-7dec-11d0-a765-00a0c91e6bf6}',
122122
'f81d4fae7dec11d0a76500a0c91e6bf6',
123-
'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
124-
'\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
123+
b'\xf8\x1d\x4f\xae\x7d\xec\x11\xd0\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
124+
b'\xae\x4f\x1d\xf8\xec\x7d\xd0\x11\xa7\x65\x00\xa0\xc9\x1e\x6b\xf6',
125125
(0xf81d4fae, 0x7dec, 0x11d0, 0xa7, 0x65, 0x00a0c91e6bf6),
126126
0xf81d4fae7dec11d0a76500a0c91e6bf6,
127127
'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
128128
0x1d07decf81d4fae, 0x2765, uuid.RFC_4122, 1),
129129
('fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
130130
'{fffefdfc-fffe-fffe-fffe-fffefdfcfbfa}',
131131
'fffefdfcfffefffefffefffefdfcfbfa',
132-
'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
133-
'\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
132+
b'\xff\xfe\xfd\xfc\xff\xfe\xff\xfe\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
133+
b'\xfc\xfd\xfe\xff\xfe\xff\xfe\xff\xff\xfe\xff\xfe\xfd\xfc\xfb\xfa',
134134
(0xfffefdfc, 0xfffe, 0xfffe, 0xff, 0xfe, 0xfffefdfcfbfa),
135135
0xfffefdfcfffefffefffefffefdfcfbfa,
136136
'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa',
137137
0xffefffefffefdfc, 0x3ffe, uuid.RESERVED_FUTURE, None),
138138
('ffffffff-ffff-ffff-ffff-ffffffffffff',
139139
'{ffffffff-ffff-ffff-ffff-ffffffffffff}',
140140
'ffffffffffffffffffffffffffffffff',
141-
'\xff'*16,
142-
'\xff'*16,
141+
b'\xff'*16,
142+
b'\xff'*16,
143143
(0xffffffff, 0xffff, 0xffff, 0xff, 0xff, 0xffffffffffff),
144144
0xffffffffffffffffffffffffffffffff,
145145
'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff',
@@ -238,7 +238,7 @@ def test_exceptions(self):
238238
badvalue(lambda: uuid.UUID(int=1<<128))
239239

240240
# Must supply exactly one of hex, bytes, fields, int.
241-
h, b, f, i = '00'*16, '\0'*16, (0, 0, 0, 0, 0, 0), 0
241+
h, b, f, i = '00'*16, b'\0'*16, (0, 0, 0, 0, 0, 0), 0
242242
uuid.UUID(h)
243243
uuid.UUID(hex=h)
244244
uuid.UUID(bytes=b)

Lib/uuid.py

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
3838
# get the raw 16 bytes of the UUID
3939
>>> x.bytes
40-
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
40+
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
4141
4242
# make a UUID from a 16-byte string
4343
>>> uuid.UUID(bytes=x.bytes)
@@ -50,7 +50,8 @@
5050
'reserved for NCS compatibility', 'specified in RFC 4122',
5151
'reserved for Microsoft compatibility', 'reserved for future definition']
5252

53-
int_ = int # The built-in int function
53+
int_ = int # The built-in int type
54+
bytes_ = bytes # The built-in bytes type
5455

5556
class UUID(object):
5657
"""Instances of the UUID class represent UUIDs as specified in RFC 4122.
@@ -138,13 +139,15 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
138139
if bytes_le is not None:
139140
if len(bytes_le) != 16:
140141
raise ValueError('bytes_le is not a 16-char string')
141-
bytes = (bytes_le[3] + bytes_le[2] + bytes_le[1] + bytes_le[0] +
142-
bytes_le[5] + bytes_le[4] + bytes_le[7] + bytes_le[6] +
142+
bytes = (bytes_(reversed(bytes_le[0:4])) +
143+
bytes_(reversed(bytes_le[4:6])) +
144+
bytes_(reversed(bytes_le[6:8])) +
143145
bytes_le[8:])
144146
if bytes is not None:
145147
if len(bytes) != 16:
146148
raise ValueError('bytes is not a 16-char string')
147-
int = int_(('%02x'*16) % tuple(map(ord, bytes)), 16)
149+
assert isinstance(bytes, bytes_), repr(bytes)
150+
int = int_(('%02x'*16) % tuple(bytes), 16)
148151
if fields is not None:
149152
if len(fields) != 6:
150153
raise ValueError('fields is not a 6-tuple')
@@ -189,7 +192,8 @@ def __ne__(self, other):
189192
return self.int != other.int
190193
return NotImplemented
191194

192-
# XXX What's the value of being able to sort UUIDs?
195+
# Q. What's the value of being able to sort UUIDs?
196+
# A. Use them as keys in a B-Tree or similar mapping.
193197

194198
def __lt__(self, other):
195199
if isinstance(other, UUID):
@@ -228,80 +232,70 @@ def __str__(self):
228232
return '%s-%s-%s-%s-%s' % (
229233
hex[:8], hex[8:12], hex[12:16], hex[16:20], hex[20:])
230234

231-
def get_bytes(self):
232-
bytes = ''
235+
@property
236+
def bytes(self):
237+
bytes = b''
233238
for shift in range(0, 128, 8):
234-
bytes = chr((self.int >> shift) & 0xff) + bytes
239+
bytes.insert(0, (self.int >> shift) & 0xff)
235240
return bytes
236241

237-
bytes = property(get_bytes)
238-
239-
def get_bytes_le(self):
242+
@property
243+
def bytes_le(self):
240244
bytes = self.bytes
241-
return (bytes[3] + bytes[2] + bytes[1] + bytes[0] +
242-
bytes[5] + bytes[4] + bytes[7] + bytes[6] + bytes[8:])
243-
244-
bytes_le = property(get_bytes_le)
245+
return (bytes_(reversed(bytes[0:4])) +
246+
bytes_(reversed(bytes[4:6])) +
247+
bytes_(reversed(bytes[6:8])) +
248+
bytes[8:])
245249

246-
def get_fields(self):
250+
@property
251+
def fields(self):
247252
return (self.time_low, self.time_mid, self.time_hi_version,
248253
self.clock_seq_hi_variant, self.clock_seq_low, self.node)
249254

250-
fields = property(get_fields)
251-
252-
def get_time_low(self):
255+
@property
256+
def time_low(self):
253257
return self.int >> 96
254258

255-
time_low = property(get_time_low)
256-
257-
def get_time_mid(self):
259+
@property
260+
def time_mid(self):
258261
return (self.int >> 80) & 0xffff
259262

260-
time_mid = property(get_time_mid)
261-
262-
def get_time_hi_version(self):
263+
@property
264+
def time_hi_version(self):
263265
return (self.int >> 64) & 0xffff
264266

265-
time_hi_version = property(get_time_hi_version)
266-
267-
def get_clock_seq_hi_variant(self):
267+
@property
268+
def clock_seq_hi_variant(self):
268269
return (self.int >> 56) & 0xff
269270

270-
clock_seq_hi_variant = property(get_clock_seq_hi_variant)
271-
272-
def get_clock_seq_low(self):
271+
@property
272+
def clock_seq_low(self):
273273
return (self.int >> 48) & 0xff
274274

275-
clock_seq_low = property(get_clock_seq_low)
276-
277-
def get_time(self):
275+
@property
276+
def time(self):
278277
return (((self.time_hi_version & 0x0fff) << 48) |
279278
(self.time_mid << 32) | self.time_low)
280279

281-
time = property(get_time)
282-
283-
def get_clock_seq(self):
280+
@property
281+
def clock_seq(self):
284282
return (((self.clock_seq_hi_variant & 0x3f) << 8) |
285283
self.clock_seq_low)
286284

287-
clock_seq = property(get_clock_seq)
288-
289-
def get_node(self):
285+
@property
286+
def node(self):
290287
return self.int & 0xffffffffffff
291288

292-
node = property(get_node)
293-
294-
def get_hex(self):
289+
@property
290+
def hex(self):
295291
return '%032x' % self.int
296292

297-
hex = property(get_hex)
298-
299-
def get_urn(self):
293+
@property
294+
def urn(self):
300295
return 'urn:uuid:' + str(self)
301296

302-
urn = property(get_urn)
303-
304-
def get_variant(self):
297+
@property
298+
def variant(self):
305299
if not self.int & (0x8000 << 48):
306300
return RESERVED_NCS
307301
elif not self.int & (0x4000 << 48):
@@ -311,15 +305,12 @@ def get_variant(self):
311305
else:
312306
return RESERVED_FUTURE
313307

314-
variant = property(get_variant)
315-
316-
def get_version(self):
308+
@property
309+
def version(self):
317310
# The version bits are only meaningful for RFC 4122 UUIDs.
318311
if self.variant == RFC_4122:
319312
return int((self.int >> 76) & 0xf)
320313

321-
version = property(get_version)
322-
323314
def _find_mac(command, args, hw_identifiers, get_index):
324315
import os
325316
for dir in ['', '/sbin/', '/usr/sbin']:
@@ -536,8 +527,8 @@ def uuid1(node=None, clock_seq=None):
536527
def uuid3(namespace, name):
537528
"""Generate a UUID from the MD5 hash of a namespace UUID and a name."""
538529
from hashlib import md5
539-
hash = md5(namespace.bytes + name).digest()
540-
return UUID(bytes=hash[:16], version=3)
530+
hash = md5(namespace.bytes + bytes(name, "utf-8")).digest()
531+
return UUID(bytes=bytes_(hash[:16]), version=3)
541532

542533
def uuid4():
543534
"""Generate a random UUID."""
@@ -559,8 +550,8 @@ def uuid4():
559550
def uuid5(namespace, name):
560551
"""Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
561552
from hashlib import sha1
562-
hash = sha1(namespace.bytes + name).digest()
563-
return UUID(bytes=hash[:16], version=5)
553+
hash = sha1(namespace.bytes + bytes(name, "utf-8")).digest()
554+
return UUID(bytes=bytes_(hash[:16]), version=5)
564555

565556
# The following standard UUIDs are for use with uuid3() or uuid5().
566557

0 commit comments

Comments
 (0)