WIP: Add signed kwarg to int.to_bytes by projectgus · Pull Request #14275 · micropython/micropython · GitHub
Skip to content

WIP: Add signed kwarg to int.to_bytes#14275

Closed
projectgus wants to merge 2 commits into
micropython:masterfrom
projectgus:feature/int_to_bytes_signed
Closed

WIP: Add signed kwarg to int.to_bytes#14275
projectgus wants to merge 2 commits into
micropython:masterfrom
projectgus:feature/int_to_bytes_signed

Conversation

@projectgus

@projectgus projectgus commented Apr 10, 2024

Copy link
Copy Markdown
Contributor

After doing all the work for #13087 I figured I was 99% of the way to implementing support for the signed kwarg, so decided to try it out. See the second commit for the changes unique to this PR.

TODO

  • Sort out remaining test failures.
  • Add new test cases for converting positive integers with signed=True. (Mostly results are the same, but in some cases an additional byte is needed to ensure a 0 sign bit.)
  • Add new test cases verifying a failure occurs when converting negative integers with signed=False. This will be a breaking change compared to the current behaviour.
  • Check if any of the places in binary.c, etc, need any changes to correctly set as_signed argument.

Code size

Total size of unix "minimal" variant:

(Note: CI measurements are a bit worse than my measurements, see both PRs. I assume because of gcc versions.)

* No longer overflows if byte size is 0 (closes micropython#13041)
* Raises OverflowError in any case where number won't fit into byte length
  (Now matches CPython, previously MicroPython would return a truncated
  bytes object.)
* Document that micropython int.to_bytes() doesn't implement the optional
  signed kwarg, but will behave as if signed=True when the integer is
  negative (this is the current behaviour). Add tests for this also.

Requires changes for small ints, MPZ large ints, and "long long" large
ints.

Adds a new set of unit tests for ints between 32 and 64 bits to increase
coverage of "long long" large ints, which are otherwise untested.

Tested on unix port (64 bit small ints, MPZ long ints) and Zephyr STM32WB
board (32 bit small ints, long long large ints).

Untested on a port whose native format is big endian (don't have one at
hand).

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
@projectgus projectgus added the py-core Relates to py/ directory in source label Apr 10, 2024
Signed-off-by: Angus Gratton <angus@redyak.com.au>
@projectgus projectgus force-pushed the feature/int_to_bytes_signed branch from d968e2a to fd7bc4f Compare April 10, 2024 04:20
@github-actions

Copy link
Copy Markdown

@projectgus projectgus closed this Oct 29, 2024
@projectgus projectgus deleted the feature/int_to_bytes_signed branch October 29, 2024 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

py-core Relates to py/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant