Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

monkeyman192
Copy link
Contributor

@monkeyman192monkeyman192 commentedJan 7, 2024
edited by github-actionsbot
Loading

This PR adds an_align_ attribute toctypes.Structure's so that is it possible to set it manually.


📚 Documentation preview 📚:https://cpython-previews--113790.org.readthedocs.build/

@ghost
Copy link

ghost commentedJan 7, 2024
edited by ghost
Loading

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

It is an interesting idea.

But what should be the size and the alignment ofB in the following example?

classA(Structure):x:c_uint32classB(A):_align_=8y:c_uint32

@monkeyman192
Copy link
ContributorAuthor

It is an interesting idea.

But what should be the size and the alignment ofB in the following example?

classA(Structure):x:c_uint32classB(A):_align_=8y:c_uint32

Thanks for the review!

My intuition would say "8 and 8".
Running the code shows that while the size is 8, the alignment ofB is 4, not 8 as specified.
To sanity check I wrote a c++ equivalent of the above and it has size 8, alignment 8.
I'll work on fixing this issue and add some unit tests to cover these cases also, thanks!
I'll also add the news snippet which I have been meaning to do...

@serhiy-storchaka
Copy link
Member

!buildbot s390x

@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by@serhiy-storchaka for commit2329fa3 🤖

The command will test the builders whose names match following regular expression:s390x

The builders matched are:

  • s390x RHEL7 LTO + PGO PR
  • s390x Fedora LTO PR
  • s390x RHEL7 LTO PR
  • s390x SLES PR
  • s390x Fedora Rawhide LTO PR
  • s390x Fedora Clang Installed PR
  • s390x RHEL8 Refleaks PR
  • s390x Fedora Rawhide LTO + PGO PR
  • s390x RHEL8 LTO + PGO PR
  • s390x RHEL8 PR
  • s390x Fedora Rawhide Refleaks PR
  • s390x Fedora Refleaks PR
  • s390x Fedora Clang PR
  • s390x Fedora LTO + PGO PR
  • s390x Fedora Rawhide PR
  • s390x Fedora PR
  • s390x RHEL8 LTO PR
  • s390x Fedora Rawhide Clang PR
  • s390x Debian PR
  • s390x RHEL7 Refleaks PR
  • s390x RHEL7 PR
  • s390x Fedora Rawhide Clang Installed PR

@serhiy-storchaka
Copy link
Member

https://buildbot.python.org/all/#/builders/390/builds/1350/steps/5/logs/stdio

======================================================================FAIL: test_aligned_string (test.test_ctypes.test_aligned_structures.TestAlignedStructures.test_aligned_string)----------------------------------------------------------------------Traceback (most recent call last):  File "/home/dje/cpython-buildarea/pull_request.edelsohn-debian-z/build/Lib/test/test_ctypes/test_aligned_structures.py", line 26, in test_aligned_string    self.assertEqual(main.first, 7)    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^AssertionError: 117440512 != 7======================================================================FAIL: test_aligned_struct_in_union (test.test_ctypes.test_aligned_structures.TestAlignedStructures.test_aligned_struct_in_union)----------------------------------------------------------------------Traceback (most recent call last):  File "/home/dje/cpython-buildarea/pull_request.edelsohn-debian-z/build/Lib/test/test_ctypes/test_aligned_structures.py", line 158, in test_aligned_struct_in_union    self.assertEqual(main.first, 1)    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^AssertionError: 16777216 != 1======================================================================FAIL: test_aligned_structures (test.test_ctypes.test_aligned_structures.TestAlignedStructures.test_aligned_structures)----------------------------------------------------------------------Traceback (most recent call last):  File "/home/dje/cpython-buildarea/pull_request.edelsohn-debian-z/build/Lib/test/test_ctypes/test_aligned_structures.py", line 64, in test_aligned_structures    self.assertEqual(main.y, 7)    ~~~~~~~~~~~~~~~~^^^^^^^^^^^AssertionError: 117440512 != 7======================================================================FAIL: test_aligned_subclasses (test.test_ctypes.test_aligned_structures.TestAlignedStructures.test_aligned_subclasses)----------------------------------------------------------------------Traceback (most recent call last):  File "/home/dje/cpython-buildarea/pull_request.edelsohn-debian-z/build/Lib/test/test_ctypes/test_aligned_structures.py", line 106, in test_aligned_subclasses    self.assertEqual(main.a, 1)    ~~~~~~~~~~~~~~~~^^^^^^^^^^^AssertionError: 16777216 != 1======================================================================FAIL: test_aligned_union (test.test_ctypes.test_aligned_structures.TestAlignedStructures.test_aligned_union)----------------------------------------------------------------------Traceback (most recent call last):  File "/home/dje/cpython-buildarea/pull_request.edelsohn-debian-z/build/Lib/test/test_ctypes/test_aligned_structures.py", line 129, in test_aligned_union    self.assertEqual(main.first, 1)    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^AssertionError: 16777216 != 1----------------------------------------------------------------------

@monkeyman192
Copy link
ContributorAuthor

In the process of making the tests run with both big and little endian versions of theStructure andUnion classes I found something curious...

fromctypesimport*classAlignedUnion(BigEndianUnion):_fields_= [        ("a",c_uint32),        ("b",c_int32),    ]classMain(BigEndianStructure):_fields_= [        ("first",c_uint32),        ("union",AlignedUnion),    ]m=Main()

If I run this on my machine (little endian), I get the following error:
TypeError: This type does not support other endian: <class '__main__.AlignedUnion'>

This seems like a bug and is the reason the last few tests look the way they do...

@serhiy-storchaka
Copy link
Member

It was fixed not long time ago in#105102.

monkeyman192 reacted with thumbs up emoji

@monkeyman192
Copy link
ContributorAuthor

monkeyman192 commentedJan 28, 2024
edited
Loading

It was fixed not long time ago in#105102.

Ok great!

I know that as per the requirements for PR's it's required to make each commit separate and to not rebase, but is it fine to rebase the whole branch onto the current head so that I get the fix for this? I haven't contributed before and just want to make sure I'm following the right procedure...

Edit: Oh I see you already merged the code into the branch, thanks!

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commentedJan 28, 2024
edited
Loading

Don't rebase, just merge. I just mergedmain into your branch, you have to pull the changes.

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Some questions and suggestions to tests. Please add more checks for offsets, alignments and sizes.

Interesting, how does setting_align_ in a Union subclass affect it?

self.assertEqual(main.first, 7)
self.assertEqual(main.string.value, b'hello world!')
self.assertEqual(
bytes(main.string.__buffer__(inspect.BufferFlags.SIMPLE)),

Choose a reason for hiding this comment

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

Maybe simplybytes(main.string)?

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'

Choose a reason for hiding this comment

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

b'hello world!\0\0\0\0' could be more readable.

Comment on lines 153 to 156
if ubase == LittleEndianUnion:
self.assertEqual(bytes(main.union.b), b"\3\0\0\0\4\0\0\0")
else:
self.assertEqual(bytes(main.union.b), b"\0\0\0\3\0\0\0\4")

Choose a reason for hiding this comment

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

It is simplydata[8:].

class TestAlignedStructures(unittest.TestCase):
def test_aligned_string(self):
for base, data in (
(LittleEndianStructure, bytearray(

Choose a reason for hiding this comment

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

You can also usestruct.pack() to generate data. It may be more compact and more readable.

@monkeyman192
Copy link
ContributorAuthor

Thanks for the thorough review!
I believe I have addressed all the comments and added some further tests to test the case of a subclassed Union where the subclass has an alignment.
I usedstruct.pack in most places to clean it except for one where the format string was just too ugly looking to make it obvious what the data would be at a glance.

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Thank you for your response@monkeyman192. I have yet few suggestions for polishing test.

monkeyman192 reacted with thumbs up emoji
self.assertEqual(Main.string.offset, 16)
self.assertEqual(Main.string.size, 16)

Choose a reason for hiding this comment

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

It is not affected by the_align_ setting, because its size was already 16. Maybe make thevalue field typec_char * 12? Then the_align_ setting will affect also the size ofAligned.

("y", c_uint32),
("x", c_ubyte),
("y", SomeBools),
("z", c_uint32),

Choose a reason for hiding this comment

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

What if makez a byte and makeSomeBools containing only 2 fields? Then without the_align_ settingMain can be packed in just 4 bytes. The_align_ setting will made larger difference.

Or just set_align_ to 8. This would cover also the next test.

("y", SomeBoolsTooBig),
("z", c_uint32),
]
with self.assertRaises(ValueError) as ctx:

Choose a reason for hiding this comment

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

It is still not very informative test. If you want to test that the_align_ setting affects the size ofMain, you can do this more directly by checkingsizeof(Main). If you want to check also content, use the data of size 12.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I guess with this test I was just wanting to ensure that the appropriate error is raised when you have a Structure/Union with an alignment which will cause the size it expects to be bigger than what it is given. Rather than checking anything about the content itself.

Comment on lines 212 to 213
self.assertEqual(main.unsigned, 0xD6)
self.assertEqual(main.signed, -42)

Choose a reason for hiding this comment

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

Oh, it is difficult to test unions with different byte order. But your test will also pass if badding bytes are added beforeunsigned (i.e. ifMain.unsigned.offset is 7). I would use all different bytes in the input data, and tested:

self.assertEqual(main.unsigned,data[0])self.assertEqual(main.signed,data[0]-256)

Please add also checks for alignments, offsets and sizes.

self.assertEqual(bytes(main.string), b'hello world!\0\0\0\0')
self.assertEqual(Main.string.offset, 16)
self.assertEqual(Main.string.size, 16)
self.assertEqual(alignment(main.string), 16)

Choose a reason for hiding this comment

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

Doesalignment(Main.string) work?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

It does not sinceMain.string is actypes.CField which has no alignment info.

@monkeyman192
Copy link
ContributorAuthor

I realised that I have neither documented nor tested the case of having both_align_ and_pack_ attributes. I'll add that also as I think it's important to understand how it should behave.

@monkeyman192
Copy link
ContributorAuthor

Sorry for the ping@serhiy-storchaka just checking to see if there was anything else I needed to address with this.
I believe I have covered the points you raised previously as well as the typo raised in the documentation.
Let me know if there are any further changes I need to make

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM.

monkeyman192 reacted with hooray emoji
@serhiy-storchaka
Copy link
Member

!buildbot s390x Debian

@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by@serhiy-storchaka for commita7bc0fe 🤖

The command will test the builders whose names match following regular expression:s390x Debian

The builders matched are:

  • s390x Debian PR

@monkeyman192
Copy link
ContributorAuthor

Ah, I just realised that this was already in theawaiting merge stage but I requested a review from aisk... Sorry about that...

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka approved these changes

@aiskaiskAwaiting requested review from aisk

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@monkeyman192@serhiy-storchaka@bedevere-bot@aisk

[8]ページ先頭

©2009-2025 Movatter.jp