Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
2 changes: 1 addition & 1 deletion Lib/ctypes/_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 24 additions & 1 deletion Lib/test/test_ctypes/test_aligned_structures.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -69,6 +69,29 @@ class Main(base):
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(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_oversized_structure(self):
data = bytearray(b"\0" * 8)
for base in (LittleEndianStructure, BigEndianStructure):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :exc:`AssertionError` raised on :class:`ctypes.Structure` with
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't it a crash, not an AssertionError?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 3.14 it was an AssertionError.

``_align_ = 0`` and ``_fields_ = []``.
Loading