python / python/cpython

Buffer requests - `itemsize`, `format`, and `PyBUF_SIMPLE`

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

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

docs topic-C-API
主要言語
Python
スター
77.2k
フォーク
35.9k
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_FORMAT is not set in the request, then the exporter must leave format as NULL. The original format is unknown to the requester, however itemsize must match the original format's size.
  • From format: If PyBUF_FORMAT is not set in the request, then the exporter must leave format as NULL. 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_SIMPLE or PyBUF_WRITABLE must also set PyBUF_FORMAT, or
  • PyBUF_FORMAT, PyBUF_SIMPLE | PyBUF_FORMAT and PyBUF_WRITABLE | PyBUF_FORMAT are 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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、itemsize、format、PyBUF_SIMPLE、PyBUF_FORMATに関するC APIバッファードキュメントのセクションを確認し、次に関連するコンテキストについてissue #123778を読んでください。itemsizeとformatのセマンティクスがドキュメント全体で一貫して説明され、有効なフラグの組み合わせと無効なフラグの組み合わせが明確に示されていれば完了です。

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

評価

技術スタック
python
領域
documentation
issue の種類
ドキュメント
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

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

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