@@ -657,12 +657,13 @@ Nested structures can also be initialized in the constructor in several ways::
657657 >>> r = RECT((1, 2), (3, 4))
658658
659659Field :term: `descriptor `\s can be retrieved from the *class *, they are useful
660- for debugging because they can provide useful information::
660+ for debugging because they can provide useful information.
661+ See :class: `CField `::
661662
662- >>> print( POINT.x)
663- <Field type=c_long , ofs=0, size=4>
664- >>> print( POINT.y)
665- <Field type=c_long , ofs=4, size=4>
663+ >>> POINT.x
664+ <ctypes.CField 'x' type=c_int , ofs=0, size=4>
665+ >>> POINT.y
666+ <ctypes.CField 'y' type=c_int , ofs=4, size=4>
666667 >>>
667668
668669
@@ -2812,6 +2813,98 @@ fields, or any other data types containing pointer type fields.
28122813 present in :attr: `_fields_ `.
28132814
28142815
2816+ .. class :: CField(*args, **kw)
2817+
2818+ Descriptor for fields of a :class: `Structure ` and :class: `Union `.
2819+ For example::
2820+
2821+ >>> class Color(Structure):
2822+ ... _fields_ = (
2823+ ... ('red', c_uint8),
2824+ ... ('green', c_uint8),
2825+ ... ('blue', c_uint8),
2826+ ... ('intense', c_bool, 1),
2827+ ... ('blinking', c_bool, 1),
2828+ ... )
2829+ ...
2830+ >>> Color.red
2831+ <ctypes.CField 'red' type=c_ubyte, ofs=0, size=1>
2832+ >>> Color.green.type
2833+ <class 'ctypes.c_ubyte'>
2834+ >>> Color.blue.byte_offset
2835+ 2
2836+ >>> Color.intense
2837+ <ctypes.CField 'intense' type=c_bool, ofs=3, bit_size=1, bit_offset=0>
2838+ >>> Color.blinking.bit_offset
2839+ 1
2840+
2841+ All attributes are read-only.
2842+
2843+ :class: `!CField ` objects are created via :attr: `~Structure._fields_ `;
2844+ do not instantiate the class directly.
2845+
2846+ .. versionadded :: next
2847+
2848+ Previously, descriptors only had ``offset `` and ``size `` attributes
2849+ and a readable string representation; the :class: `!CField ` class was not
2850+ available directly.
2851+
2852+ .. attribute :: name
2853+
2854+ Name of the field, as a string.
2855+
2856+ .. attribute :: type
2857+
2858+ Type of the field, as a :ref: `ctypes class <ctypes-data-types >`.
2859+
2860+ .. attribute :: offset
2861+ byte_offset
2862+
2863+ Offset of the field, in bytes.
2864+
2865+ For bitfields, this is the offset of the underlying byte-aligned
2866+ *storage unit *; see :attr: `~CField.bit_offset `.
2867+
2868+ .. attribute :: byte_size
2869+
2870+ Size of the field, in bytes.
2871+
2872+ For bitfields, this is the size of the underlying *storage unit *.
2873+ Typically, it has the same size as the bitfield's type.
2874+
2875+ .. attribute :: size
2876+
2877+ For non-bitfields, equivalent to :attr: `~CField.byte_size `.
2878+
2879+ For bitfields, this contains a backwards-compatible bit-packed
2880+ value that combines :attr: `~CField.bit_size ` and
2881+ :attr: `~CField.bit_offset `.
2882+ Prefer using the explicit attributes instead.
2883+
2884+ .. attribute :: is_bitfield
2885+
2886+ True if this is a bitfield.
2887+
2888+ .. attribute :: bit_offset
2889+ bit_size
2890+
2891+ The location of a bitfield within its *storage unit *, that is, within
2892+ :attr: `~CField.byte_size ` bytes of memory starting at
2893+ :attr: `~CField.byte_offset `.
2894+
2895+ To get the field's value, read the storage unit as an integer,
2896+ :ref: `shift left <shifting >` by :attr: `!bit_offset ` and
2897+ take the :attr: `!bit_size ` least significant bits.
2898+
2899+ For non-bitfields, :attr: `!bit_offset ` is zero
2900+ and :attr: `!bit_size ` is equal to ``byte_size * 8 ``.
2901+
2902+ .. attribute :: is_anonymous
2903+
2904+ True if this field is anonymous, that is, it contains nested sub-fields
2905+ that should be be merged into a containing structure or union.
2906+
2907+
28152908.. _ctypes-arrays-pointers :
28162909
28172910Arrays and pointers
0 commit comments