python / python/cpython

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

オープン
#156,865 コメント 10 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず 'e'、'f'、'Zf' 形式の array.array セッターから始め、次に、レポートに示されているように、それらのオーバーフロー動作を struct.pack と比較してください。意図された一貫性を決定する前に、リンクされている PRs と関連する issues を確認してください。完了とは、選択した動作がこれらの形式タイプに対して実装され、カバーされていることを意味します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。