python / python/cpython

`structseq_repr` mislabels unnamed `PyStructSequence` fields (e.g. `os.stat_result` slots 7–9)

未關閉
#154,387 16 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

interpreter-core type-bug
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

structseq_repr() indexes tp_members by visible sequence position, but tp_members is built with every PyStructSequence_UnnamedField entry skipped (packed). As a result, every visible slot at or after the first unnamed field is labeled with the name of a later (hidden) field. For os.stat_result, the unnamed integer time slots 7–9 borrow the names of the hidden st_atime/st_mtime/st_ctime float fields.


Real-world case — a plain os.stat(). The repr prints st_atime=<integer seconds>, but the st_atime attribute is the float with sub-second precision — they are different fields:

>>> import os
>>> st = os.stat(os.curdir)
>>> st
os.stat_result(st_mode=16877, st_ino=1367263, st_dev=16777232, st_nlink=43,
               st_uid=501, st_gid=20, st_size=1376,
               st_atime=1784619363, st_mtime=1784619363, st_ctime=1784619363)
>>> st[7]           # sequence slot 7 — the integer atime
1784619363
>>> st.st_atime     # the .st_atime attribute — a different, hidden float field
1784619363.4590902

Synthetic case, isolating the mechanism with distinct values per field:

>>> # The repr always labels sequence slot 7 "st_atime", printing slot 7's value:
>>> st = os.stat_result(range(22))
>>> st
os.stat_result(st_mode=0, st_ino=1, st_dev=2, st_nlink=3, st_uid=4, st_gid=5,
               st_size=6, st_atime=7, st_mtime=8, st_ctime=9)
>>> st[7]
7

>>> # But the .st_atime ATTRIBUTE is the hidden field at index 10:
>>> st.st_atime  # field 10 supplied  -> 10
10  # surprise! differs from the repr above

>>> os.stat_result(range(11)).st_atime  # field 10 supplied  -> 10
10
>>> os.stat_result(range(10)).st_atime  # field 10 NOT supplied -> falls back to slot 7 -> 7
7  # surprise! # **UPDATE**: this is an intentional behavior in overrided `statresult_new`

>>> os.stat_result.n_sequence_fields, os.stat_result.n_unnamed_fields
(10, 3)
>>> os.stat_result.__match_args__       # correctly excludes the 3 unnamed slots
('st_mode', 'st_ino', 'st_dev', 'st_nlink', 'st_uid', 'st_gid', 'st_size')

The mislabel is easy to miss: when only the 10 sequence fields are supplied (range(10)), the hidden float st_atime (index 10) is unset and derives from the integer slot 7, so the repr and the attribute coincidentally agree. Supplying field 10 (range(11) or more, or a real os.stat()) makes them disagree.

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs
  • gh-154434
  • gh-154684
  • gh-154687

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 structseq_repr 開始,根據 issue 中的 os.stat_result 範例重現不一致,包括提供的隱藏欄位。檢查可見的序列槽是否獨立於未命名欄位進行標記,同時隱藏屬性保留其值,然後根據連結的 PRs 和文件中的範例驗證行為。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。