python / python/cpython

Please add documentation of the bitfield ordering for ctypes.Structure types

Open
#118,434 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs topic-ctypes
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Documentation

The current documentation for ctypes bit fields is very limited and doesn't cover the definition order. AFAICT this is:
Little endian - LSB first -> MSB last
Big endian - MSB first -> LSB last
Native - ??? This is LSB first -> MSB last on x86 but is that just because it's a little endian architecture?

For example:

class BigOnes(BigEndianStructure):
   _pack_ = 1
    _fields_ = (
        ("bit_0", c_uint16, 1),  # MSBit
        ("bit_1", c_uint16, 1),
        ("bit_2", c_uint16, 1),
        ("bit_3", c_uint16, 1),
        ("nibble_1", c_uint16, 4),
        ("nibble_2", c_uint16, 4),
        ("nibble_3", c_uint16, 4),  # LSBit
        ("byte_3", c_uint16, 8),  # MSByte
        ("byte_4", c_uint16, 8),  # LSByte
    )

Versus:

class LittleOnes(LittleEndianStructure):
    _pack_ = 1
    _fields_ = (
        ("bit_0", c_uint16, 1),  # LSBit
        ("bit_1", c_uint16, 1),
        ("bit_2", c_uint16, 1),
        ("bit_3", c_uint16, 1),
        ("nibble_1", c_uint16, 4),
        ("nibble_2", c_uint16, 4),
        ("nibble_3", c_uint16, 4),  # MSBit
        ("byte_3", c_uint16, 8),  # LSByte
        ("byte_4", c_uint16, 8),  # MSByte
    )

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ctypes documentation for Structure, BigEndianStructure, and LittleEndianStructure, using the provided bitfield examples as the cases to explain. Determine and document definition order for little-endian, big-endian, and native structures, including the architecture-dependent behavior, and verify that the resulting text is unambiguous.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.