-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Changes from 2 commits
9038f43
c35cb1d
58d013c
d7262ad
a169e89
583c9fc
f437da5
bf919ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)): | ||
bytes_le = bytes_(bytes_le) | ||
if len(bytes_le) != 16: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
And conversely:
|
||
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') | ||
|
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... and 'bytes_le' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, fixed. |
||
bytearray and memoryview for example. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
int
.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
. 😉