python / python/cpython

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

未關閉
#132,177 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

docs topic-ctypes
主要語言
Python
星號
77.2k
分支
36k
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. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 _CData._objects 文件以及其中引用的 Modules/_ctypes/_ctypes.c 中的 CDataType_from_buffer_impl 和 KeepRef 程式碼開始。確認 from_buffer 對 memoryview 的記憶體行為是否為刻意設計,然後更新文件以準確描述該行為;使用提供的 ctypes 範例驗證文件中描述的行為。

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

評估

技術堆疊
c, python
領域
documentation
Issue 類型
文件
難度
3/5
預估耗時
1-2 天
活躍度
冷清
描述清晰度
基本清楚
新手友好度
52/100

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

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