Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add extra test and fix documentation typo
  • Loading branch information
monkeyman192 committed Feb 2, 2024
commit a7bc0fef39ea871bd598ffb1fadd10fd7dc17ca7
2 changes: 1 addition & 1 deletion Doc/library/ctypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2540,7 +2540,7 @@ fields, or any other data types containing pointer type fields.

.. attribute:: _align_

An optional small interger that allows overriding the alignment of
An optional small integer that allows overriding the alignment of
the structure when being packed or unpacked to/from memory.
Setting this attribute to 0 is the same as not setting it at all.

Expand Down
36 changes: 36 additions & 0 deletions Lib/test/test_ctypes/test_aligned_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,42 @@ class Main(SubUnion):
self.assertEqual(main.unsigned, 0xD6)
self.assertEqual(main.signed, -42)

def test_aligned_packed_structures(self):
for sbase, e in (
(LittleEndianStructure, "<"),
(BigEndianStructure, ">"),
):
data = bytearray(struct.pack(f"{e}B2H4xB", 1, 2, 3, 4))

class Inner(sbase):
_align_ = 8
_fields_ = [
("x", c_uint16),
("y", c_uint16),
]

class Main(sbase):
_pack_ = 1
_fields_ = [
("a", c_ubyte),
("b", Inner),
("c", c_ubyte),
]

main = Main.from_buffer(data)
self.assertEqual(sizeof(main), 10)
self.assertEqual(Main.b.offset, 1)
# Alignment == 8 because _pack_ wins out.
self.assertEqual(alignment(main.b), 8)
# Size is still 8 though since inside this Structure, it will have
# effect.
self.assertEqual(sizeof(main.b), 8)
self.assertEqual(Main.c.offset, 9)
self.assertEqual(main.a, 1)
self.assertEqual(main.b.x, 2)
self.assertEqual(main.b.y, 3)
self.assertEqual(main.c, 4)


if __name__ == '__main__':
unittest.main()