Skip to content

Commit 8943bb7

Browse files
authored
gh-136520: Clarify docs for _pack_ & _align_ (GH-137036)
Move docs to the reference section & reduce the “tutorial” part to a quick intro & link. Clarify what values are accepted. Add macro/attribute equivalents. Discourage _align_ values that aren't powers of two.
1 parent 4dd85b3 commit 8943bb7

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

Doc/library/ctypes.rst

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -700,14 +700,10 @@ compiler does it. It is possible to override this behavior entirely by specifyi
700700
:attr:`~Structure._layout_` class attribute in the subclass definition; see
701701
the attribute documentation for details.
702702

703-
It is possible to specify the maximum alignment for the fields by setting
704-
the :attr:`~Structure._pack_` class attribute to a positive integer.
705-
This matches what ``#pragma pack(n)`` does in MSVC.
706-
707-
It is also possible to set a minimum alignment for how the subclass itself is packed in the
708-
same way ``#pragma align(n)`` works in MSVC.
709-
This can be achieved by specifying a :attr:`~Structure._align_` class attribute
710-
in the subclass definition.
703+
It is possible to specify the maximum alignment for the fields and/or for the
704+
structure itself by setting the class attributes :attr:`~Structure._pack_`
705+
and/or :attr:`~Structure._align_`, respectively.
706+
See the attribute documentation for details.
711707

712708
:mod:`ctypes` uses the native byte order for Structures and Unions. To build
713709
structures with non-native byte order, you can use one of the
@@ -2792,11 +2788,18 @@ fields, or any other data types containing pointer type fields.
27922788
.. attribute:: _pack_
27932789

27942790
An optional small integer that allows overriding the alignment of
2795-
structure fields in the instance. :attr:`_pack_` must already be defined
2796-
when :attr:`_fields_` is assigned, otherwise it will have no effect.
2797-
Setting this attribute to 0 is the same as not setting it at all.
2791+
structure fields in the instance.
2792+
2793+
This is only implemented for the MSVC-compatible memory layout
2794+
(see :attr:`_layout_`).
27982795

2799-
This is only implemented for the MSVC-compatible memory layout.
2796+
Setting :attr:`!_pack_` to 0 is the same as not setting it at all.
2797+
Otherwise, the value must be a positive power of two.
2798+
The effect is equivalent to ``#pragma pack(N)`` in C, except
2799+
:mod:`ctypes` may allow larger *n* than what the compiler accepts.
2800+
2801+
:attr:`!_pack_` must already be defined
2802+
when :attr:`_fields_` is assigned, otherwise it will have no effect.
28002803

28012804
.. deprecated-removed:: 3.14 3.19
28022805

@@ -2809,9 +2812,22 @@ fields, or any other data types containing pointer type fields.
28092812

28102813
.. attribute:: _align_
28112814

2812-
An optional small integer that allows overriding the alignment of
2815+
An optional small integer that allows increasing the alignment of
28132816
the structure when being packed or unpacked to/from memory.
2814-
Setting this attribute to 0 is the same as not setting it at all.
2817+
2818+
The value must not be negative.
2819+
The effect is equivalent to ``__attribute__((aligned(N)))`` on GCC
2820+
or ``#pragma align(N)`` on MSVC, except :mod:`ctypes` may allow
2821+
values that the compiler would reject.
2822+
2823+
:attr:`!_align_` can only *increase* a structure's alignment
2824+
requirements. Setting it to 0 or 1 has no effect.
2825+
2826+
Using values that are not powers of two is discouraged and may lead to
2827+
surprising behavior.
2828+
2829+
:attr:`!_align_` must already be defined
2830+
when :attr:`_fields_` is assigned, otherwise it will have no effect.
28152831

28162832
.. versionadded:: 3.13
28172833

0 commit comments

Comments
 (0)