python / python/cpython

`_objects` attribute of ctypes data instances is sometimes not None or a dictionary

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

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

docs topic-ctypes
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Documentation

According to the current ctypes docs, the _objects attribute of ctypes data instances is supposed to be either None or a dictionary:

_objects
This member is either None or a dictionary containing Python objects that need to be kept alive so that the memory block contents is kept valid. This object is only exposed for debugging; never modify the contents of this dictionary.

However, instances created with from_buffer have this attribute set to a memoryview of the source buffer, not to a dictionary:

>>> import ctypes
>>> x = ctypes.c_uint8.from_buffer(bytearray(1), 0)
>>> type(x._objects)
<class 'memoryview'>

This behavior traces to this call of KeepRef inside CDataType_from_buffer_impl. Prior to this call, the result's b_objects is NULL, so this branch in KeepRef is taken, setting b_objects to reference the memoryview.

I'm not familiar with ctypes internals, but to my reading this behavior seems intentional and doesn't appear to cause any problems. Hence I'm filing this as a docs issue.

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

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

はじめの一歩

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

調査の方向性

_CData._objects のドキュメントと、そこで参照されている Modules/_ctypes/_ctypes.c 内の CDataType_from_buffer_impl および KeepRef のコードから始めます。memoryview に対する from_buffer のメモリ動作が意図的なものか確認し、その後、正確に説明するようドキュメントを更新します。提供されている ctypes の例に対して、ドキュメントに記載された動作を検証します。

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

評価

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

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

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