python / python/cpython

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

Offen
#132,177 1 Kommentar 0 Reaktionen 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

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.

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

Beginne mit der Dokumentation zu _CData._objects und dem darin referenzierten Code CDataType_from_buffer_impl und KeepRef in Modules/_ctypes/_ctypes.c. Prüfe, ob das Speicherverhalten von from_buffer bei memoryview beabsichtigt ist, und aktualisiere anschließend die Dokumentation, um es korrekt zu beschreiben; verifiziere das dokumentierte Verhalten anhand des bereitgestellten ctypes-Beispiels.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
c, python
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
52/100

Neue Issues direkt in Ihr Postfach

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