-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[mypyc] Add bytes index primitive #10950
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bd7c4a3
[mypyc] Add bytes index primitives
97littleleaf11 87218ad
Use CPyTagged
97littleleaf11 7e16789
Discard setitem
97littleleaf11 e75bba9
Add
97littleleaf11 e42f34e
Revert
97littleleaf11 8b31a6a
Fix tests
97littleleaf11 cda86c8
Test
97littleleaf11 4637e1e
Fix
97littleleaf11 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
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 |
|---|---|---|
|
|
@@ -43,16 +43,27 @@ def test_bytes_init() -> None: | |
| pass | ||
|
|
||
| [case testBytesOps] | ||
| from testutil import assertRaises | ||
|
|
||
| def test_indexing() -> None: | ||
| # Use bytes() to avoid constant folding | ||
| b = b'asdf' + bytes() | ||
| assert b[0] == 97 | ||
| assert b[1] == 115 | ||
| assert b[3] == 102 | ||
| assert b[-1] == 102 | ||
| b = b'\xfe\x15' + bytes() | ||
| assert b[0] == 254 | ||
| assert b[1] == 21 | ||
| b = b'\xae\x80\xfe\x15' + bytes() | ||
| assert b[0] == 174 | ||
| assert b[1] == 128 | ||
| assert b[2] == 254 | ||
| assert b[3] == 21 | ||
| assert b[-4] == 174 | ||
| with assertRaises(IndexError, "index out of range"): | ||
| b[4] | ||
| with assertRaises(IndexError, "index out of range"): | ||
| b[-5] | ||
| with assertRaises(IndexError, "index out of range"): | ||
| b[2**26] | ||
|
|
||
| def test_concat() -> None: | ||
| b1 = b'123' + bytes() | ||
|
|
@@ -100,6 +111,7 @@ def test_len() -> None: | |
|
|
||
| [case testBytearrayBasics] | ||
| from typing import Any | ||
| from testutil import assertRaises | ||
|
|
||
| def test_basics() -> None: | ||
| brr1: bytes = bytearray(3) | ||
|
|
@@ -126,6 +138,27 @@ def test_bytearray_passed_into_bytes() -> None: | |
| brr1: Any = bytearray() | ||
| assert f(brr1) | ||
|
|
||
| def test_bytearray_indexing() -> None: | ||
| b: bytes = bytearray(b'\xae\x80\xfe\x15') | ||
| assert b[0] == 174 | ||
| assert b[1] == 128 | ||
| assert b[2] == 254 | ||
| assert b[3] == 21 | ||
| assert b[-4] == 174 | ||
| with assertRaises(IndexError, "index out of range"): | ||
| b[4] | ||
| with assertRaises(IndexError, "index out of range"): | ||
| b[-5] | ||
| b2 = bytearray([175, 255, 128, 22]) | ||
|
Collaborator
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. Use
Collaborator
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. This still needs to be updated. |
||
| assert b2[0] == 175 | ||
| assert b2[1] == 255 | ||
| assert b2[-1] == 22 | ||
| assert b2[2] == 128 | ||
| with assertRaises(ValueError, "byte must be in range(0, 256)"): | ||
| b2[0] = -1 | ||
| with assertRaises(ValueError, "byte must be in range(0, 256)"): | ||
| b2[0] = 256 | ||
|
|
||
| [case testBytesJoin] | ||
| from typing import Any | ||
| from testutil import assertRaises | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.