-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-112433: Add optional _align_ attribute to ctypes.Structure #113790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
serhiy-storchaka
merged 9 commits into
python:main
from
monkeyman192:add_structure_alignment
Feb 15, 2024
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a6ef3ac
Add optional _align_ attribute to ctypes.Structure
monkeyman192 e2cceec
📜🤖 Added by blurb_it.
blurb-it[bot] 2329fa3
Fix issue with aligned subclasses
monkeyman192 ebaed7e
Improve tests to pass on big endian machines
monkeyman192 6b183b5
Merge branch 'main' into add_structure_alignment
serhiy-storchaka c8b37d4
Further improvements to tests
monkeyman192 68e2d61
Test cleanup and improvements
monkeyman192 f6a93dd
More test improvements
monkeyman192 a7bc0fe
Add extra test and fix documentation typo
monkeyman192 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| from ctypes import Structure, c_char, c_uint32, c_ubyte, alignment, sizeof, Union | ||
| import inspect | ||
| import unittest | ||
|
|
||
|
|
||
| class TestAlignedStructures(unittest.TestCase): | ||
| def test_aligned_string(self): | ||
| data = bytearray( | ||
| b'\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' | ||
| b'\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x21\x00\x00\x00\x00' | ||
| ) | ||
|
monkeyman192 marked this conversation as resolved.
Outdated
|
||
|
|
||
| class Aligned(Structure): | ||
| _align_ = 16 | ||
| _fields_ = [ | ||
| ('value', c_char * 16), | ||
| ] | ||
|
|
||
| class Main(Structure): | ||
| _fields_ = [ | ||
| ('first', c_uint32), | ||
| ('string', Aligned), | ||
| ] | ||
|
|
||
| main = Main.from_buffer(data) | ||
| self.assertEqual(main.first, 7) | ||
| self.assertEqual(main.string.value, b'hello world!') | ||
| self.assertEqual( | ||
| bytes(main.string.__buffer__(inspect.BufferFlags.SIMPLE)), | ||
| b'\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x21\x00\x00\x00\x00' | ||
| ) | ||
|
|
||
| def test_aligned_structures(self): | ||
|
serhiy-storchaka marked this conversation as resolved.
|
||
| data = bytearray( | ||
| b'\x01\x00\x01\x00\x07\x00\x00\x00' | ||
| ) | ||
|
|
||
| class SomeBools(Structure): | ||
| _align_ = 4 | ||
| _fields_ = [ | ||
| ("bool1", c_ubyte), | ||
| ("bool2", c_ubyte), | ||
| ("bool3", c_ubyte), | ||
| ] | ||
| class Main(Structure): | ||
| _fields_ = [ | ||
| ("x", SomeBools), | ||
| ("y", c_uint32), | ||
| ] | ||
|
|
||
| class SomeBoolsTooBig(Structure): | ||
| _align_ = 8 | ||
| _fields_ = [ | ||
| ("bool1", c_ubyte), | ||
| ("bool2", c_ubyte), | ||
| ("bool3", c_ubyte), | ||
| ] | ||
| class MainTooBig(Structure): | ||
| _fields_ = [ | ||
| ("x", SomeBoolsTooBig), | ||
| ("y", c_uint32), | ||
| ] | ||
| main = Main.from_buffer(data) | ||
| self.assertEqual(main.y, 7) | ||
| self.assertEqual(alignment(SomeBools), 4) | ||
| self.assertEqual(main.x.bool1, True) | ||
| self.assertEqual(main.x.bool2, False) | ||
| self.assertEqual(main.x.bool3, True) | ||
|
|
||
| with self.assertRaises(ValueError) as ctx: | ||
| MainTooBig.from_buffer(data) | ||
| self.assertEqual( | ||
| ctx.exception.args[0], | ||
| 'Buffer size too small (4 instead of at least 8 bytes)' | ||
| ) | ||
|
|
||
| def test_aligned_subclasses(self): | ||
| data = bytearray( | ||
| b"\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00" | ||
| ) | ||
|
|
||
| class UnalignedSub(Structure): | ||
| x: c_uint32 | ||
| _fields_ = [ | ||
| ("x", c_uint32), | ||
| ] | ||
|
|
||
| class AlignedStruct(UnalignedSub): | ||
| _align_ = 8 | ||
| y: c_uint32 | ||
| _fields_ = [ | ||
| ("y", c_uint32), | ||
| ] | ||
|
|
||
| class Main(Structure): | ||
| _fields_ = [ | ||
| ("a", c_uint32), | ||
| ("b", AlignedStruct) | ||
| ] | ||
|
|
||
| main = Main.from_buffer(data) | ||
| self.assertEqual(alignment(main.b), 8) | ||
| self.assertEqual(alignment(main), 8) | ||
| self.assertEqual(sizeof(main.b), 8) | ||
| self.assertEqual(sizeof(main), 16) | ||
| self.assertEqual(main.a, 1) | ||
| self.assertEqual(main.b.x, 3) | ||
| self.assertEqual(main.b.y, 4) | ||
|
|
||
| def test_aligned_union(self): | ||
| data = bytearray( | ||
| b"\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00" | ||
| ) | ||
|
|
||
| class AlignedUnion(Union): | ||
| _align_ = 8 | ||
| _fields_ = [ | ||
| ("a", c_uint32), | ||
| ("b", c_ubyte * 8), | ||
| ] | ||
|
|
||
| class Main(Structure): | ||
| _fields_ = [ | ||
| ("first", c_uint32), | ||
| ("union", AlignedUnion), | ||
| ] | ||
|
|
||
| main = Main.from_buffer(data) | ||
| self.assertEqual(main.first, 1) | ||
| self.assertEqual(main.union.a, 3) | ||
| self.assertEqual(bytes(main.union.b), b"\x03\x00\x00\x00\x04\x00\x00\x00") | ||
|
|
||
| def test_aligned_struct_in_union(self): | ||
| data = bytearray( | ||
| b"\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00" | ||
| ) | ||
|
|
||
| class Sub(Structure): | ||
| _align_ = 8 | ||
| _fields_ = [ | ||
| ("x", c_uint32), | ||
| ("y", c_uint32), | ||
| ] | ||
|
|
||
| class MainUnion(Union): | ||
| _fields_ = [ | ||
| ("a", c_uint32), | ||
| ("b", Sub), | ||
| ] | ||
|
|
||
| class Main(Structure): | ||
| _fields_ = [ | ||
| ("first", c_uint32), | ||
| ("union", MainUnion), | ||
| ] | ||
|
|
||
| main = Main.from_buffer(data) | ||
| self.assertEqual(main.first, 1) | ||
| self.assertEqual(main.union.a, 3) | ||
| self.assertEqual(main.union.b.x, 3) | ||
| self.assertEqual(main.union.b.y, 4) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core and Builtins/2024-01-28-02-46-12.gh-issue-112433.FUX-nT.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add ability to force alignment of :mod:`ctypes.Structure` by way of the new ``_align_`` attribute on the class. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.