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 2 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
25 changes: 19 additions & 6 deletions Lib/test/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,18 @@ def test_exceptions(self):
badvalue(lambda: uuid.UUID('123456781234567812345678z2345678'))

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

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

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

self.assertNotEqual(parent_value, child_value)

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

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

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


class TestInternals(unittest.TestCase):
@unittest.skipUnless(os.name == 'posix', 'requires Posix')
Expand Down
7 changes: 5 additions & 2 deletions Lib/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,18 @@ 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:
if not isinstance(bytes_le, (bytes_, bytearray)):
Copy link
Member

Choose a reason for hiding this comment

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

Add int.

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 don't understand why bytes_le should accept an int? Do you have an example please?

Copy link
Member

Choose a reason for hiding this comment

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

It should not! But accepts. Add a test for bytes_le=16. 😉

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' argument to be an instance of bytes: accept bytes-like types,
Copy link
Member

Choose a reason for hiding this comment

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

... and 'bytes_le'

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, fixed.

bytearray and memoryview for example.