-
-
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
30
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 all commits
Commits
Show all changes
30 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 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.
Show resolved
Hide resolved
|
||
assert to_bytes(0, 1, "big") == b'\x00' | ||
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. Maybe also test calling |
||
# 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.
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 think you can avoid allocating a temporary buffer if you call
PyBytes_FromStringAndSize
withNULL
as the first argument to make the object uninitialized. You can then pass a pointer to the contents of thebytes
object to_PyLong_AsByteArray
.