Skip to content

bpo-29729: uuid.UUID now accepts bytes-like object #3801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/uuid.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ which relays any information about the UUID's safety, using this enumeration:
``12345678-1234-5678-1234-567812345678`` where the 32 hexadecimal digits
represent the UUID.

.. versionchanged:: 3.7
*bytes* and *bytes_le* parameters now accept any bytes-like objects,
bytearray and memoryview for example, not only :class:`bytes`.

:class:`UUID` instances have these read-only attributes:

.. attribute:: UUID.bytes
Expand Down
27 changes: 21 additions & 6 deletions Lib/test/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,20 @@ def test_exceptions(self):
badvalue(lambda: self.uuid.UUID('123456781234567812345678z2345678'))

# Badly formed bytes.
badvalue(lambda: self.uuid.UUID(bytes='abc'))
badvalue(lambda: self.uuid.UUID(bytes='\0'*15))
badvalue(lambda: self.uuid.UUID(bytes='\0'*17))
badtype(lambda: self.uuid.UUID(bytes='unicode'))
badtype(lambda: self.uuid.UUID(bytes='u'*16))
badvalue(lambda: self.uuid.UUID(bytes=b'abc'))
badvalue(lambda: self.uuid.UUID(bytes=b'\0'*15))
badvalue(lambda: self.uuid.UUID(bytes=b'\0'*17))
badtype(lambda: self.uuid.UUID(bytes=16))

# Badly formed bytes_le.
badvalue(lambda: self.uuid.UUID(bytes_le='abc'))
badvalue(lambda: self.uuid.UUID(bytes_le='\0'*15))
badvalue(lambda: self.uuid.UUID(bytes_le='\0'*17))
badtype(lambda: self.uuid.UUID(bytes_le='unicode'))
badtype(lambda: self.uuid.UUID(bytes_le='u'*16))
badvalue(lambda: self.uuid.UUID(bytes_le=b'abc'))
badvalue(lambda: self.uuid.UUID(bytes_le=b'\0'*15))
badvalue(lambda: self.uuid.UUID(bytes_le=b'\0'*17))
badtype(lambda: self.uuid.UUID(bytes_le=16))

# Badly formed fields.
badvalue(lambda: self.uuid.UUID(fields=(1,)))
Expand Down Expand Up @@ -476,6 +482,15 @@ def testIssue8621(self):

self.assertNotEqual(parent_value, child_value)

def test_bytes_like(self):
u = self.uuid.uuid4()

self.assertEqual(self.uuid.UUID(bytes=memoryview(u.bytes)), u)
self.assertEqual(self.uuid.UUID(bytes=bytearray(u.bytes)), u)

self.assertEqual(self.uuid.UUID(bytes_le=memoryview(u.bytes_le)), u)
self.assertEqual(self.uuid.UUID(bytes_le=bytearray(u.bytes_le)), u)


class TestUUIDWithoutExtModule(BaseTestUUID, unittest.TestCase):
uuid = py_uuid
Expand Down
8 changes: 6 additions & 2 deletions Lib/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,19 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
raise ValueError('badly formed hexadecimal UUID string')
int = int_(hex, 16)
if bytes_le is not None:
# Don't cast int to bytes to get a TypeError above
if not isinstance(bytes_le, (bytes_, bytearray, int_)):
bytes_le = bytes_(bytes_le)
if len(bytes_le) != 16:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By above in the comment, do you mean this check, which is actually below the comment? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the comment.

raise ValueError('bytes_le is not a 16-char string')
bytes = (bytes_le[4-1::-1] + bytes_le[6-1:4-1:-1] +
bytes_le[8-1:6-1:-1] + bytes_le[8:])
if bytes is not None:
int = int_.from_bytes(bytes, byteorder='big')
# test length after the conversion to raise a TypeError exception
# if 'bytes' type is str even if 'bytes' length is not 16
if len(bytes) != 16:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>>> a = array.array('i', [-1]*4)
>>> x = int.from_bytes(a, byteorder='big')
>>> len(a)
4
>>> x.bit_length()/8
16.0

And conversely:

>>> a = array.array('i', [-1]*16)
>>> x = int.from_bytes(a, byteorder='big')
>>> len(a)
16
>>> x.bit_length()/8
64.0

raise ValueError('bytes is not a 16-char string')
assert isinstance(bytes, bytes_), repr(bytes)
int = int_.from_bytes(bytes, byteorder='big')
if fields is not None:
if len(fields) != 6:
raise ValueError('fields is not a 6-tuple')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
uuid.UUID now accepts bytes-like object. The constructor doesn't require the
'bytes' and 'bytes_le' arguments to be an instance of bytes: accept bytes-like
types, bytearray and memoryview for example.