This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Incorrect behaviour creating a Structure with ctypes.c_bool bitfields
Type: behavior Stage:
Components: ctypes, Windows Versions: Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: dudenwatschn, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2022-02-15 12:01 by dudenwatschn, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
mre.py dudenwatschn, 2022-02-15 12:01 Minimal Runnable Example
Messages (1)
msg413284 - (view) Author: SIGSEG V (dudenwatschn) Date: 2022-02-15 12:01
Setting/getting values in a Structure containing multiple c_bool bitfields like:
_fields_ = [
  ('one', c_bool, 1),
  ('two', c_bool, 1),
  ]
results in an unexpected behavior.
Setting any one of these fields to `True` results in ALL of these fields being set to `True` (i.e.: setting `struct.one` to `True` causes both `struct.one` as well as `struct.two` to be set to `True`.
This also results in the binary representation of the struct to be incorrect. The only possible outcomes for `bytes(struct)` are `b'\x00` and `b'\x01'`

Expected behavior should be that when setting `struct.one` only sets the desired field.
This is achievable when defining the same Structure with `c_byte` rather than `c_bool`.
When defining the struct like:
_fields_ = [
  ('one', c_byte, 1),
  ('two', c_byte, 1),
  ]
setting `struct.one` only affects `struct.one` and not `struct.two`.
The binary representation of the structure is also correct. When setting `struct.two` to `True`, `bytes(struct)` returns `b'\x02'` (aka. 0b10).

I've attached a Minimal Runnable Example that hopefully helps outline the issue.
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 90914
2022-02-15 12:01:56dudenwatschncreate