python / python/cpython

Overflows happened silently in array.array setters for 'e', 'f' and 'Zf' format types

Open
#156,865 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules interpreter-core type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

Consider this:

>>> import array
>>> a = array.array('e', [123])
>>> a[0] = 123456
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    a[0] = 123456
    ~^^^
OverflowError: float too large to pack with e format
>>> a = array.array('f', [123])
>>> a[0] = 1e70  # no OverflowError
>>> a
array('f', [inf])
>>> a = array.array('Zf', [123])
>>> a[0] = 1e70j  # no OverflowError
>>> a
array('Zf', [infj])
>>> import struct
>>> struct.pack('f', 1e70)
Traceback (most recent call last):
  File "<python-input-7>", line 1, in <module>
    struct.pack('f', 1e70)
    ~~~~~~~~~~~^^^^^^^^^^^
OverflowError: float too large to pack with f format
>>> struct.pack('>Zf', 1e70j)
Traceback (most recent call last):
  File "<python-input-13>", line 1, in <module>
    struct.pack('>Zf', 1e70j)
    ~~~~~~~~~~~^^^^^^^^^^^^^^
OverflowError: float too large to pack with f format

(BTW, f_setitem() suffers from same issue as https://github.com/python/cpython/issues/156864.)

Maybe it's a feature and array() should behave differently wrt the struct module. In either case, this behavior looks inconsistent. If we consider cases for 'f'/'Zf' types being correct - we should clear exceptions also for 'e' type code.

See also struct/array issues (merged to this per @vstinner suggestion):

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs
  • gh-156869
  • gh-156868
  • gh-156916
  • gh-156919

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the array.array setters for the 'e', 'f', and 'Zf' formats, then compare their overflow behavior with struct.pack as shown in the report. Review the linked PRs and related issues before deciding the intended consistency; done means the selected behavior is implemented and covered for these format types.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.