python / python/cpython

ctypes._CData._b_base_ does not point to root ctypes object

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

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

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

説明

Hi, I've been writing some binary interface "decoder" in python using ctypes, and one thing confuses me a bit (and also ruins some of my expectations)

The docs say:

The _b_base_ read-only member is the root ctypes object that owns the memory block.

So with this code:

import ctypes

bindata = bytearray(b"AAAABBBBCCCC")

SomeArray = ctypes.c_int * 3
class SomeData(ctypes.Structure):
    _fields_ = [
        ("asdf", SomeArray),
    ]

class FinalData(ctypes.Structure):
    _fields_ = [
        ("foo", SomeData),
    ]

x = FinalData.from_buffer(bindata)
print(x)
print(hex(ctypes.addressof(x)))
print(x._b_base_)
print()

foo = x.foo
print(foo)
print(hex(ctypes.addressof(foo)))
print(foo._b_base_)
print()

asdf = foo.asdf
print(asdf)
print(hex(ctypes.addressof(asdf)))
print(asdf._b_base_)
print()

I expect foo._b_base_ and asdf._b_base_ to point to the same FinalData object, meanwhile this is the output:

<__main__.FinalData object at 0x7f3bae3fa5d0>
0x7f3bae6985d0
None

<__main__.SomeData object at 0x7f3bae6597b0>
0x7f3bae6985d0
<__main__.FinalData object at 0x7f3bae3fa5d0>

<__main__.c_int_Array_3 object at 0x7f3bae69e450>
0x7f3bae6985d0
<__main__.SomeData object at 0x7f3bae6597b0>

So instead of root ctypes object, this returns the parent ctypes object. Is this an oversight/bug?

Python version: Python 3.14.0b2
OS: NixOS unstable

Linked PRs
  • gh-152623
  • gh-152706

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

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

はじめの一歩

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

調査の方向性

ctypes._CData.b_base のドキュメントを読み、まず提供された reproducer を実行してください。FinalData、SomeData、SomeArray の親チェーンをたどり、関連付けられた PRs をコンテキストとして使用してください。完了とは、文書化された root-owner の動作と回帰テストのカバレッジが意図されたセマンティクスと一致することを意味します。

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

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

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

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