-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
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
Closed
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9038f43
bpo-29729: uuid.UUID now accepts bytes-like object
vstinner c35cb1d
Accept also bytes-like for bytes_le
vstinner 58d013c
Update the NEWS entry.
vstinner d7262ad
Merge branch 'master' into uuid_bytes
vstinner a169e89
Fix UUID(bytes_le=16)
vstinner 583c9fc
Remove a comment
vstinner f437da5
Merge branch 'master' into uuid_bytes
vstinner bf919ce
Repeat that objects must have a length of 16
vstinner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
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') | ||
|
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2017-09-28-16-21-13.bpo-29729.skDkJA.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the comment.