Skip to content

Ctypes unions with bitfield members that do not share memory #83211

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

Closed
OhSoDank mannequin opened this issue Dec 12, 2019 · 2 comments
Closed

Ctypes unions with bitfield members that do not share memory #83211

OhSoDank mannequin opened this issue Dec 12, 2019 · 2 comments
Labels
3.7 (EOL) end of life topic-ctypes type-bug An unexpected behavior, bug, or error

Comments

@OhSoDank
Copy link
Mannequin

OhSoDank mannequin commented Dec 12, 2019

BPO 39030
Nosy @abalkin, @LewisGaul, @OhSoDank
Files
  • bitfield_union.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2019-12-12.11:15:24.265>
    labels = ['ctypes', 'type-bug', '3.7']
    title = 'Ctypes unions with bitfield members that do not share memory'
    updated_at = <Date 2019-12-12.15:41:56.428>
    user = 'https://github.com/OhSoDank'

    bugs.python.org fields:

    activity = <Date 2019-12-12.15:41:56.428>
    actor = 'LewisGaul'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['ctypes']
    creation = <Date 2019-12-12.11:15:24.265>
    creator = 'dankreso'
    dependencies = []
    files = ['48772']
    hgrepos = []
    issue_num = 39030
    keywords = []
    message_count = 1.0
    messages = ['358300']
    nosy_count = 3.0
    nosy_names = ['belopolsky', 'LewisGaul', 'dankreso']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue39030'
    versions = ['Python 3.7']

    @OhSoDank
    Copy link
    Mannequin Author

    OhSoDank mannequin commented Dec 12, 2019

    I've found what looks like a corner case bug. Specifically, the behaviour that looks suspicious is when a ctypes union has bit field members, where the members have bit widths that are smaller than the size types:

    class BitFieldUnion(Union):
        _fields_ = [("a", c_uint32, 16), ("b", c_uint32, 16)]
    
    buff = bytearray(4)
    
    bitfield_union = BitFieldUnion.from_buffer(buff)
    bitfield_union.a = 1
    bitfield_union.b = 2
    
    print("a is {}".format(bitfield_union.a)) # Prints "a is 1"
    print("b is {}".format(bitfield_union.b)) # Prints "b is 2"
    print("Buffer: {}".format(buff)) # Prints "Buffer: b'\x01\x00\x00\x00'".

    (Example of this script can be found at https://rextester.com/XJFGAK37131. I've also tried it on my system which is Ubuntu 16.04.2 LTS with Python 3.6.)

    Here I would expect both 'a' and 'b' to be set to 2, and for the buffer to look like '\x02\x00\x00\x00'. Here's the equivalent code in C which behaves as expected: https://rextester.com/HWDUMB56821.

    If at least one of the bitwidths in the above example are changed from 16 to 32, however, then 'a', 'b', and the buffer look as expected.

    I've also attached some further examples of weird behaviour with unions with bitfield members - online version can be found at https://rextester.com/VZRB77320.

    @OhSoDank OhSoDank mannequin added 3.7 (EOL) end of life topic-ctypes type-bug An unexpected behavior, bug, or error labels Dec 12, 2019
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @encukou
    Copy link
    Member

    encukou commented May 13, 2025

    Fixed in #97702.

    @encukou encukou closed this as completed May 13, 2025
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life topic-ctypes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant