-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
ctypes: bitfield lost data with union on linux platform #95496
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
Comments
I get similar error without unions. Below is a test snippet that passes on Windows but fails on Ubuntu WSL2. import ctypes
for field_width in range(32, 1, -1):
class TestStruct(ctypes.Structure):
_fields_ = [
("Field1", ctypes.c_uint32, field_width),
("Field2", ctypes.c_uint8, 8)
]
cmd = TestStruct()
cmd.Field2 = 1
if cmd.Field2 != 1:
raise RuntimeError(f"{field_width=}, {cmd.Field2=} != 1")
print("All good") I get following output in WSL2 Ubuntu $ python --version
Python 3.8.10
$ python -c 'import ctypes; print(ctypes.__version__)'
1.1.0
$ python ctypes_test.py
Traceback (most recent call last):
File "ctypes_test.py", line 13, in <module>
raise RuntimeError(f"{field_width=}, {cmd.Field2=} != 1")
RuntimeError: field_width=24, cmd.Field2=0 != 1 I tried Python 3.9 on WSL2 Ubuntu too with same result. |
Your E-mail has been received and I will reply as soon as possible
|
This is fixed in #97702 and related fixes. Should be released in Python 3.14; unfortunately the fix is backwards-incompatible so I don't plan to backport it. Note that bitfields use a different layout on Linux (gcc/sysV): the output will be for name in 'abcdefg':
print(name, getattr(a.data, name))
# → zeros for abce, ones for efg To get the Windows behaviour you can add |
Your E-mail has been received and I will reply as soon as possible
|
below example is simple and can be reproduced.
The text was updated successfully, but these errors were encountered: