Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c35cb1d

Browse filesBrowse files
committed
Accept also bytes-like for bytes_le
* Accept also bytes-like for bytes_le: convert memoryview to bytes to use slices. Check also the length after the conversion to integer to raise a TypeError even if the length is not 16. * Fix unit tests: pass bytes to 'bytes' and 'bytes_le' parameters * Add more tests on Unicode passed to bytes and bytes_le * Document UUID changes
1 parent 9038f43 commit c35cb1d
Copy full SHA for c35cb1d

File tree

Expand file treeCollapse file tree

3 files changed

+23
-7
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+23
-7
lines changed

‎Doc/library/uuid.rst

Copy file name to clipboardExpand all lines: Doc/library/uuid.rst
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ which relays any information about the UUID's safety, using this enumeration:
7575
``12345678-1234-5678-1234-567812345678`` where the 32 hexadecimal digits
7676
represent the UUID.
7777

78+
.. versionchanged:: 3.7
79+
*bytes* and *bytes_le* parameters now accept any bytes-like objects,
80+
bytearray and memoryview for example, not only :class:`bytes`.
81+
7882
:class:`UUID` instances have these read-only attributes:
7983

8084
.. attribute:: UUID.bytes

‎Lib/test/test_uuid.py

Copy file name to clipboardExpand all lines: Lib/test/test_uuid.py
+14-6Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,18 @@ def test_exceptions(self):
213213
badvalue(lambda: uuid.UUID('123456781234567812345678z2345678'))
214214

215215
# Badly formed bytes.
216-
badvalue(lambda: uuid.UUID(bytes='abc'))
217-
badvalue(lambda: uuid.UUID(bytes='\0'*15))
218-
badvalue(lambda: uuid.UUID(bytes='\0'*17))
216+
badtype(lambda: uuid.UUID(bytes='unicode'))
217+
badtype(lambda: uuid.UUID(bytes='u'*16))
218+
badvalue(lambda: uuid.UUID(bytes=b'abc'))
219+
badvalue(lambda: uuid.UUID(bytes=b'\0'*15))
220+
badvalue(lambda: uuid.UUID(bytes=b'\0'*17))
219221

220222
# Badly formed bytes_le.
221-
badvalue(lambda: uuid.UUID(bytes_le='abc'))
222-
badvalue(lambda: uuid.UUID(bytes_le='\0'*15))
223-
badvalue(lambda: uuid.UUID(bytes_le='\0'*17))
223+
badtype(lambda: uuid.UUID(bytes_le='unicode'))
224+
badtype(lambda: uuid.UUID(bytes_le='u'*16))
225+
badvalue(lambda: uuid.UUID(bytes_le=b'abc'))
226+
badvalue(lambda: uuid.UUID(bytes_le=b'\0'*15))
227+
badvalue(lambda: uuid.UUID(bytes_le=b'\0'*17))
224228

225229
# Badly formed fields.
226230
badvalue(lambda: uuid.UUID(fields=(1,)))
@@ -458,9 +462,13 @@ def testIssue8621(self):
458462

459463
def test_bytes_like(self):
460464
u = uuid.uuid4()
465+
461466
self.assertEqual(uuid.UUID(bytes=memoryview(u.bytes)), u)
462467
self.assertEqual(uuid.UUID(bytes=bytearray(u.bytes)), u)
463468

469+
self.assertEqual(uuid.UUID(bytes_le=memoryview(u.bytes_le)), u)
470+
self.assertEqual(uuid.UUID(bytes_le=bytearray(u.bytes_le)), u)
471+
464472

465473
class TestInternals(unittest.TestCase):
466474
@unittest.skipUnless(os.name == 'posix', 'requires Posix')

‎Lib/uuid.py

Copy file name to clipboardExpand all lines: Lib/uuid.py
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,18 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
159159
raise ValueError('badly formed hexadecimal UUID string')
160160
int = int_(hex, 16)
161161
if bytes_le is not None:
162+
if not isinstance(bytes_le, (bytes_, bytearray)):
163+
bytes_le = bytes_(bytes_le)
162164
if len(bytes_le) != 16:
163165
raise ValueError('bytes_le is not a 16-char string')
164166
bytes = (bytes_le[4-1::-1] + bytes_le[6-1:4-1:-1] +
165167
bytes_le[8-1:6-1:-1] + bytes_le[8:])
166168
if bytes is not None:
169+
int = int_.from_bytes(bytes, byteorder='big')
170+
# test length after the conversion to raise a TypeError exception
171+
# if 'bytes' type is str even if 'bytes' length is not 16
167172
if len(bytes) != 16:
168173
raise ValueError('bytes is not a 16-char string')
169-
int = int_.from_bytes(bytes, byteorder='big')
170174
if fields is not None:
171175
if len(fields) != 6:
172176
raise ValueError('fields is not a 6-tuple')

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.