python / python/cpython

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

未关闭
#156,865 10 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

extension-modules interpreter-core type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先从 'e'、'f' 和 'Zf' 格式的 array.array setter 开始,然后按照报告中的说明,将它们的溢出行为与 struct.pack 进行比较。在决定预期的一致性之前,先查看链接的 PRs 和相关 issues;完成意味着已为这些格式类型实现并覆盖所选行为。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。