I am on ArchLinux with Python 3.10.7. Python 3.11 and main are also affected. I also randomly tried Python 3.6 with the same result. (Python 3.6 is the oldest one that was easy to install.)
More comprehensive test case
Here's how I actually found the problem reported above. Using Hypothesis:
Since I'm looking into bit-fields and ctypes anyway at the moment, I'll plan to work on a fix. But if someone else wants to give it a go, I'm happy to let them.
As far as I can tell the problem might be with PyCField_FromDesc in cfield.c. The code looks both a bit complicated and is not expected to be a hot inner loop (it's only for setting up a Structure from a description.) So perhaps we might want to move most of its logic into Python.
@matthiasgoergens I think this is essentially the same issue as existing issues like #95496, #84039, #73939, #59324, etc; it would be good to try to consolidate some of these so we don't end up with an explosion of issues.
Thank you for the report, and the initial fix!
The layout calculation is now done in Python, and a test suite is in place, so we're in a better place to solve the other struct/union layout issues. I'll close this one.
Activity
pythongh-97588: Some tests to demonstrate the issue
matthiasgoergens commented on Sep 27, 2022
Since I'm looking into bit-fields and ctypes anyway at the moment, I'll plan to work on a fix. But if someone else wants to give it a go, I'm happy to let them.
matthiasgoergens commented on Sep 27, 2022
As far as I can tell the problem might be with PyCField_FromDesc in cfield.c. The code looks both a bit complicated and is not expected to be a hot inner loop (it's only for setting up a Structure from a description.) So perhaps we might want to move most of its logic into Python.
Let's me dig some more.
pythongh-97588: Fix ctypes structs
mdickinson commented on Oct 8, 2022
@matthiasgoergens I think this is essentially the same issue as existing issues like #95496, #84039, #73939, #59324, etc; it would be good to try to consolidate some of these so we don't end up with an explosion of issues.
matthiasgoergens commented on Oct 8, 2022
@mdickinson Good idea. That's actually what I am already doing with the fix I have in the works, but consolidating the issues makes sense, too.
matthiasgoergens commented on Oct 9, 2022
@mdickinson I mentioned the issues that my PR fixes on the PR itself. Is there a way (or necessity) to also consolidate the issues themselves here?
Backport 'pythongh-97588: Fix ctypes structs' to 3.11
Squash 'pythongh-97588: Fix ctypes structs' for backport to 3.10
thesamprice commented on Mar 27, 2023
@matthiasgoergens Could you review pull request !103052
matthiasgoergens commented on Mar 27, 2023
@thesamprice I'll have a look, but keep in mind that I don't have any authority here.
thesamprice commented on Mar 27, 2023
More interested if the one line change passes the tests you wrote .
matthiasgoergens commented on Mar 27, 2023
Oh, ok. Sure, I can run that against my tests.
I guess I should publish my Python Hypothesis tests, too; and not just the tests in my PR. So you can run them yourself.
Give me a bit of time!
Edit: I'll answer over on #103052
matthiasgoergens commented on Mar 27, 2023
@thesamprice I added your tests from #103052 to this PR, too. I hope you don't mind.
gh-97588: Align ctypes struct layout to GCC/MSVC (GH-97702)
pythongh-97588: Align ctypes struct layout to GCC/MSVC (pythonGH-97702)
pythongh-97588: Align ctypes struct layout to GCC/MSVC (pythonGH-97702)
gh-97588: Move ctypes struct/union layout logic to Python (GH-123352)
encukou commented on Sep 9, 2024
Thank you for the report, and the initial fix!
The layout calculation is now done in Python, and a test suite is in place, so we're in a better place to solve the other struct/union layout issues. I'll close this one.
pythongh-97588: remove unused functions in _ctypes/cfield.c
gh-97588: remove unused functions in _ctypes/cfield.c (GH-124010)
pythongh-97588: remove unused functions in _ctypes/cfield.c (pythonGH…