-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[mypyc] feat: new primitive for int.to_bytes
#19674
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
Open
BobTheBuidler
wants to merge
57
commits into
python:master
Choose a base branch
from
BobTheBuidler:to-bytes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 31 commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
2058f81
[mypyc] feat: new primitive for `int.to_bytes`
BobTheBuidler f62bfd2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 813c510
add headers
BobTheBuidler 4be77d6
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into…
BobTheBuidler cb93329
cover all arg combos
BobTheBuidler 6ff1c9b
CPyLong_ToBytes header
BobTheBuidler 166b6f4
;
BobTheBuidler be2d2de
fix name
BobTheBuidler 9ff3a7c
fix _PyLong_AsByteArray compile err
BobTheBuidler 8399619
Update ir.py
BobTheBuidler db7b483
define header
BobTheBuidler ed05c00
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into…
BobTheBuidler a0c147b
fix ir
BobTheBuidler 5bab265
Update ir.py
BobTheBuidler b346bbf
fix ir
BobTheBuidler 8d523a0
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into…
BobTheBuidler 85652a2
fix ir
BobTheBuidler 8e97165
add ir test
BobTheBuidler 3660a01
fix py 3.10
BobTheBuidler 6a8a83c
use _PyLong_AsByteArray on all pythons
BobTheBuidler ec95008
fix: py3.13 and 3.14
BobTheBuidler 95fa8b0
optimize if check
BobTheBuidler 78f4ca1
Merge branch 'master' into to-bytes
BobTheBuidler 326484b
Update CPy.h
BobTheBuidler 245a122
Update int_ops.c
BobTheBuidler d2994e1
Update int_ops.c
BobTheBuidler 1138448
Merge branch 'master' into to-bytes
BobTheBuidler 0715ca2
test long
BobTheBuidler 7cce1e7
add overflow error tests
BobTheBuidler c3aaefa
Merge branch 'master' into to-bytes
BobTheBuidler 0d7cb57
fix: move helper func up in C file
BobTheBuidler db09bc0
Update ir.py
BobTheBuidler a7151dd
Update irbuild-int.test
BobTheBuidler bdd320e
feat: specialize
BobTheBuidler 4518aea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 97873da
missing }
BobTheBuidler d7b0023
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into…
BobTheBuidler 26cd5c6
fix: specialize
BobTheBuidler 30c4a30
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 84acab3
fix
BobTheBuidler b5a6c06
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ef5b1d7
fix
BobTheBuidler 9b629a5
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into…
BobTheBuidler 9b69d58
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6a5de07
fix
BobTheBuidler 251986e
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into…
BobTheBuidler f5f9d30
fix
BobTheBuidler 9a27e5d
Update irbuild-int.test
BobTheBuidler 6c82357
Update run-integers.test
BobTheBuidler c4d9673
Update irbuild-int.test
BobTheBuidler 3ad1e50
maybe fix
BobTheBuidler 77535d3
Update int_ops.py
BobTheBuidler d291fb1
Update irbuild-int.test
BobTheBuidler 66bb191
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into…
BobTheBuidler 420b8e2
Merge branch 'master' into to-bytes
BobTheBuidler 2fc6433
Merge branch 'master' into to-bytes
BobTheBuidler 11a6aea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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 |
---|---|---|
|
@@ -572,3 +572,24 @@ class subc(int): | |
[file userdefinedint.py] | ||
class int: | ||
pass | ||
|
||
[case testIntToBytes] | ||
from testutil import assertRaises | ||
def to_bytes(n: int, length: int, byteorder: str, signed: bool = False) -> bytes: | ||
return n.to_bytes(length, byteorder, signed=signed) | ||
def test_to_bytes() -> None: | ||
assert to_bytes(255, 2, "big") == b'\x00\xff' | ||
assert to_bytes(255, 2, "little") == b'\xff\x00' | ||
assert to_bytes(-1, 2, "big", True) == b'\xff\xff' | ||
BobTheBuidler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
assert to_bytes(0, 1, "big") == b'\x00' | ||
|
||
# test with a value that does not fit in 64 bits | ||
assert to_bytes(10**30, 16, "big") == b'\x00\x00\x00\x0c\x9f,\x9c\xd0Ft\xed\xea@\x00\x00\x00' | ||
# unsigned, too large for 1 byte | ||
with assertRaises(OverflowError): | ||
to_bytes(256, 1, "big") | ||
# signed, too small for 1 byte | ||
with assertRaises(OverflowError): | ||
to_bytes(-129, 1, "big", True) | ||
# signed, too large for 1 byte | ||
with assertRaises(OverflowError): | ||
to_bytes(128, 1, "big", True) |
Oops, something went wrong.
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.