[New-bugs-announce] [issue46758] Incorrect behaviour creating a Structure with ctypes.c_bool bitfields

SIGSEG V report at bugs.python.org
Tue Feb 15 07:01:56 EST 2022

New submission from SIGSEG V <Noname.genial at alpenjodel.de>:

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.

components: Windows, ctypes
files: mre.py
messages: 413284
nosy: dudenwatschn, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: Incorrect behaviour creating a Structure with ctypes.c_bool bitfields
type: behavior
versions: Python 3.10, Python 3.9
Added file: https://bugs.python.org/file50624/mre.py

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list