Skip to content

array.array() setter incorrectly handle overflows for 'e' type code #156864

Description

@skirpichev

Bug report

Bug description:

Consider this:

>>> import array
>>> a = array.array('e', [123])
>>> a[0] = 123456  # doesn't fit in _Float16 but fits in _Float32
Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    a[0] = 123456
    ~^^^
OverflowError: float too large to pack with e format
>>> a
array('e', [123.0])  
>>> a[0] = 1e300  # doesn't fit neither in _Float32 nor in _Float16
>>> a
array('e', [inf])

That's happened because we use PyArg_Parse() function with 'f' format code to parse floats. Then in the second case PyFloat_Pack2() seen plain infinity and doesn't trigger overflow.

I believe both examples should trigger overflows.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions