| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
Sorry, something went wrong.
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead. |
Sorry, something went wrong.
There was a problem hiding this comment.
It is an interesting idea.
But what should be the size and the alignment of B in the following example?
class A(Structure):
x: c_uint32
class B(A):
_align_ = 8
y: c_uint32
Sorry, something went wrong.
Thanks for the review! My intuition would say "8 and 8". |
Sorry, something went wrong.
|
!buildbot s390x |
Sorry, something went wrong.
|
🤖 New build scheduled with the buildbot fleet by @serhiy-storchaka for commit 2329fa3 🤖 The command will test the builders whose names match following regular expression: s390x The builders matched are:
|
Sorry, something went wrong.
|
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
----------------------------------------------------------------------
|
Sorry, something went wrong.
|
In the process of making the tests run with both big and little endian versions of the Structure and Union classes I found something curious... from ctypes import *
class AlignedUnion(BigEndianUnion):
_fields_ = [
("a", c_uint32),
("b", c_int32),
]
class Main(BigEndianStructure):
_fields_ = [
("first", c_uint32),
("union", AlignedUnion),
]
m = Main()If I run this on my machine (little endian), I get the following error: This seems like a bug and is the reason the last few tests look the way they do... |
Sorry, something went wrong.
|
It was fixed not long time ago in #105102. |
Sorry, something went wrong.
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! |
Sorry, something went wrong.
|
Don't rebase, just merge. I just merged main into your branch, you have to pull the changes. |
Sorry, something went wrong.
There was a problem hiding this comment.
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?
Sorry, something went wrong.
| self.assertEqual(main.first, 7) | ||
| self.assertEqual(main.string.value, b'hello world!') | ||
| self.assertEqual( | ||
| bytes(main.string.__buffer__(inspect.BufferFlags.SIMPLE)), |
There was a problem hiding this comment.
Maybe simply bytes(main.string)?
Sorry, something went wrong.
| 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' |
There was a problem hiding this comment.
b'hello world!\0\0\0\0' could be more readable.
Sorry, something went wrong.
| 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") |
There was a problem hiding this comment.
It is simply data[8:].
Sorry, something went wrong.
| class TestAlignedStructures(unittest.TestCase): | ||
| def test_aligned_string(self): | ||
| for base, data in ( | ||
| (LittleEndianStructure, bytearray( |
There was a problem hiding this comment.
You can also use struct.pack() to generate data. It may be more compact and more readable.
Sorry, something went wrong.
|
Thanks for the thorough review! |
Sorry, something went wrong.
There was a problem hiding this comment.
Thank you for your response @monkeyman192. I have yet few suggestions for polishing test.
Sorry, something went wrong.
| ) | ||
| self.assertEqual(bytes(main.string), b'hello world!\0\0\0\0') | ||
| self.assertEqual(Main.string.offset, 16) | ||
| self.assertEqual(Main.string.size, 16) |
There was a problem hiding this comment.
It is not affected by the _align_ setting, because its size was already 16. Maybe make the value field type c_char * 12? Then the _align_ setting will affect also the size of Aligned.
Sorry, something went wrong.
| ("y", c_uint32), | ||
| ("x", c_ubyte), | ||
| ("y", SomeBools), | ||
| ("z", c_uint32), |
There was a problem hiding this comment.
What if make z a byte and make SomeBools containing only 2 fields? Then without the _align_ setting Main 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.
Sorry, something went wrong.
| ("y", SomeBoolsTooBig), | ||
| ("z", c_uint32), | ||
| ] | ||
| with self.assertRaises(ValueError) as ctx: |
There was a problem hiding this comment.
It is still not very informative test. If you want to test that the _align_ setting affects the size of Main, you can do this more directly by checking sizeof(Main). If you want to check also content, use the data of size 12.
Sorry, something went wrong.
There was a problem hiding this comment.
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.
Sorry, something went wrong.
| self.assertEqual(main.unsigned, 0xD6) | ||
| self.assertEqual(main.signed, -42) |
There was a problem hiding this comment.
Oh, it is difficult to test unions with different byte order. But your test will also pass if badding bytes are added before unsigned (i.e. if Main.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.
Sorry, something went wrong.
| 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) |
There was a problem hiding this comment.
Does alignment(Main.string) work?
Sorry, something went wrong.
There was a problem hiding this comment.
It does not since Main.string is a ctypes.CField which has no alignment info.
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
|
Sorry for the ping @serhiy-storchaka just checking to see if there was anything else I needed to address with this. |
Sorry, something went wrong.
There was a problem hiding this comment.
LGTM.
Sorry, something went wrong.
|
!buildbot s390x Debian |
Sorry, something went wrong.
|
🤖 New build scheduled with the buildbot fleet by @serhiy-storchaka for commit a7bc0fe 🤖 The command will test the builders whose names match following regular expression: s390x Debian The builders matched are:
|
Sorry, something went wrong.
|
Ah, I just realised that this was already in the awaiting merge stage but I requested a review from aisk... Sorry about that... |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
This PR adds an _align_ attribute to ctypes.Structure's so that is it possible to set it manually.
📚 Documentation preview 📚: https://cpython-previews--113790.org.readthedocs.build/