Buffer requests - `itemsize`, `format`, and `PyBUF_SIMPLE`
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Documentation
I am trying to understand the full interaction between itemsize and format in the buffer requests documentation.
Q: If PyBuf_FORMAT is not set in the request, and shape is not NULL, is the real format unknown (real size specified by itemsize) or required to be "B" format?
To quote some pieces:
From itemsize:
Important exception: If a consumer requests a buffer without the PyBUF_FORMAT flag, format will be set to NULL, but itemsize still has the value for the original format.
If shape is NULL as a result of a PyBUF_SIMPLE or a PyBUF_WRITABLE request, the consumer must disregard itemsize and assume itemsize == 1.
From format:
A NULL terminated string in struct module style syntax describing the contents of a single item. If this is NULL, "B" (unsigned bytes) is assumed.
This seems to imply the following contradictory statements to me:
- From itemsize: If
PyBUF_FORMATis not set in the request, then the exporter must leaveformatasNULL. The original format is unknown to the requester, howeveritemsizemust match the original format's size. - From format: If
PyBUF_FORMATis not set in the request, then the exporter must leaveformatasNULL. The format is assumed to be"B"format.
Reading further, the docs also state
PyBUF_FORMAT must be |’d to any of the flags except PyBUF_SIMPLE, because the latter already implies format B (unsigned bytes). PyBUF_FORMAT cannot be used on its own.
It is unclear to me whether this means:
- All requests other than
PyBUF_SIMPLEorPyBUF_WRITABLEmust also setPyBUF_FORMAT, or PyBUF_FORMAT,PyBUF_SIMPLE | PyBUF_FORMATandPyBUF_WRITABLE | PyBUF_FORMATare disallowed combinations.
If PyBUF_FORMAT must always be set for nontrivial requests, I think a lot of my confusion above goes away, but the fact that the docs seem to allow for NULL format makes me think this is not the case. (Also compound requests such as PyBUF_STRIDED_RO do not set PyBUF_FORMAT.)
On the other hand, I don't see much technical justification for why standalone PyBUF_FORMAT (optionally in combination with PyBUF_SIMPLE or PyBUF_WRITABLE) would be forbidden. It seems reasonably clear to me that this would have to imply the exporter provides a 1D contiguous array where any format is allowed.
I found #123778 which made some changes in this direction, cc @encukou @ZeroIntensity
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先從 C API 緩衝區文件中關於 itemsize、format、PyBUF_SIMPLE 和 PyBUF_FORMAT 的章節開始,然後閱讀 issue #123778 以了解相關背景。完成的標準是:文件一致地說明 itemsize 和 format 的語意,並清楚標示有效和無效的旗標組合。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- documentation
- Issue 類型
- 文件
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100