Skip to content

[Bug]: missing overflow for (32768).to_bytes(2, 'big', signed=True) #883

@skirpichev

Description

@skirpichev

Describe the bug

On CPython 3.12 this raises:

Python 3.12.13 (tags/v3.12.13:3bb231a6a5d, May 14 2026, 11:06:30) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> (32768).to_bytes(2, 'big', signed=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: int too big to convert

N.B. discovered during testing of the python-gmp package with GraalPy (extension correctly raises an exception for mpz type):

______________________________ test_to_bytes_bulk ______________________________

x = 32768, length = 2, byteorder = 'big', signed = True

    @given(bigints(), integers(min_value=0, max_value=10000),
           sampled_from(["big", "little"]), booleans())
    @example(0, 0, "big", False)
    @example(0, 0, "little", False)
    @example(0, 1, "big", False)
    @example(128, 1, "big", True)
    @example(128, 1, "little", True)
    @example(-129, 1, "big", True)
    @example(-129, 1, "little", True)
    @example(-1, 0, "big", True)
    @example(-2, 0, "big", True)
    @example(-2, 0, "little", True)
    @example(42, 1, "big", False)
    @example(42, 1, "little", False)
    @example(42, 3, "big", False)
    @example(42, 3, "little", False)
    @example(1000, 2, "big", False)
    @example(1000, 4, "big", False)
    @example(-2049, 1, "big", True)
    @example(-65281, 3, "big", True)
    @example(-65281, 3, "little", True)
    @example(128, 1, "big", False)
    @example(-32383289396013590652, 0, "big", True)
    def test_to_bytes_bulk(x, length, byteorder, signed):
        try:
            rx = x.to_bytes(length, byteorder, signed=signed)
        except OverflowError:
            with pytest.raises(OverflowError):
                mpz(x).to_bytes(length, byteorder, signed=signed)
        else:
>           assert rx == mpz(x).to_bytes(length, byteorder, signed=signed)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E           OverflowError: int too big to convert
E           Falsifying example: test_to_bytes_bulk(
E               x=32768,
E               length=2,
E               byteorder='big',  # or any other generated value
E               signed=True,
E           )
E           
E           You can reproduce this example by temporarily adding @reproduce_failure('6.152.11', b'AEEAQwCAAEECQQAB') as a decorator on your test case

tests/test_mpz.py:847: OverflowError

Operating system

Linux

CPU architecture

x86_64

GraalPy version

25.0.3

JDK version

No response

Context configuration

No response

Steps to reproduce

(32768).to_bytes(2, 'big', signed=True)

Expected behavior

OverflowError

Stack trace

Additional context

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions