gh-132673: Fix ctypes.Structure with _align_=0 (#132676)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
sobolevn 2025-04-18 17:32:28 +03:00 committed by GitHub
parent 40ae88988c
commit 678b8e1656
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 2 deletions

View file

@ -84,7 +84,7 @@ def get_layout(cls, input_fields, is_struct, base):
raise ValueError('_align_ must be a non-negative integer')
elif align == 0:
# Setting `_align_ = 0` amounts to using the default alignment
align == 1
align = 1
if base:
align = max(ctypes.alignment(base), align)

View file

@ -1,7 +1,7 @@
from ctypes import (
c_char, c_uint32, c_uint16, c_ubyte, c_byte, alignment, sizeof,
BigEndianStructure, LittleEndianStructure,
BigEndianUnion, LittleEndianUnion,
BigEndianUnion, LittleEndianUnion, Structure,
)
import struct
import unittest
@ -69,6 +69,41 @@ class TestAlignedStructures(unittest.TestCase, StructCheckMixin):
self.assertEqual(Main.z.offset, 8)
self.assertEqual(main.z, 7)
def test_negative_align(self):
for base in (Structure, LittleEndianStructure, BigEndianStructure):
with (
self.subTest(base=base),
self.assertRaisesRegex(
ValueError,
'_align_ must be a non-negative integer',
)
):
class MyStructure(base):
_align_ = -1
_fields_ = []
def test_zero_align_no_fields(self):
for base in (Structure, LittleEndianStructure, BigEndianStructure):
with self.subTest(base=base):
class MyStructure(base):
_align_ = 0
_fields_ = []
self.assertEqual(alignment(MyStructure), 1)
self.assertEqual(alignment(MyStructure()), 1)
def test_zero_align_with_fields(self):
for base in (Structure, LittleEndianStructure, BigEndianStructure):
with self.subTest(base=base):
class MyStructure(base):
_align_ = 0
_fields_ = [
("x", c_ubyte),
]
self.assertEqual(alignment(MyStructure), 1)
self.assertEqual(alignment(MyStructure()), 1)
def test_oversized_structure(self):
data = bytearray(b"\0" * 8)
for base in (LittleEndianStructure, BigEndianStructure):

View file

@ -0,0 +1,2 @@
Fix :exc:`AssertionError` raised on :class:`ctypes.Structure` with
``_align_ = 0`` and ``_fields_ = []``.