python / python/cpython

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

Offen
#135,669 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

docs topic-ctypes
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Lesen Sie zuerst die Dokumentation zu ctypes._CData.b_base und führen Sie den bereitgestellten Reproducer aus. Verfolgen Sie die Elternkette für FinalData, SomeData und SomeArray und verwenden Sie die verknüpften PRs als Kontext; die Aufgabe ist abgeschlossen, wenn das dokumentierte Verhalten des Root-Owners und die Regressionsabdeckung der beabsichtigten Semantik entsprechen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
backend
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.