Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a7bc0fe

Browse files
committed
Add extra test and fix documentation typo
1 parent f6a93dd commit a7bc0fe

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

Doc/library/ctypes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2540,7 +2540,7 @@ fields, or any other data types containing pointer type fields.
25402540

25412541
.. attribute:: _align_
25422542

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

Lib/test/test_ctypes/test_aligned_structures.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,42 @@ class Main(SubUnion):
245245
self.assertEqual(main.unsigned, 0xD6)
246246
self.assertEqual(main.signed, -42)
247247

248+
def test_aligned_packed_structures(self):
249+
for sbase, e in (
250+
(LittleEndianStructure, "<"),
251+
(BigEndianStructure, ">"),
252+
):
253+
data = bytearray(struct.pack(f"{e}B2H4xB", 1, 2, 3, 4))
254+
255+
class Inner(sbase):
256+
_align_ = 8
257+
_fields_ = [
258+
("x", c_uint16),
259+
("y", c_uint16),
260+
]
261+
262+
class Main(sbase):
263+
_pack_ = 1
264+
_fields_ = [
265+
("a", c_ubyte),
266+
("b", Inner),
267+
("c", c_ubyte),
268+
]
269+
270+
main = Main.from_buffer(data)
271+
self.assertEqual(sizeof(main), 10)
272+
self.assertEqual(Main.b.offset, 1)
273+
# Alignment == 8 because _pack_ wins out.
274+
self.assertEqual(alignment(main.b), 8)
275+
# Size is still 8 though since inside this Structure, it will have
276+
# effect.
277+
self.assertEqual(sizeof(main.b), 8)
278+
self.assertEqual(Main.c.offset, 9)
279+
self.assertEqual(main.a, 1)
280+
self.assertEqual(main.b.x, 2)
281+
self.assertEqual(main.b.y, 3)
282+
self.assertEqual(main.c, 4)
283+
248284

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

0 commit comments

Comments
 (0)