Skip to content
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

Untangle ctypes bitfield size and expose field information #128715

Open
encukou opened this issue Jan 10, 2025 · 1 comment
Open

Untangle ctypes bitfield size and expose field information #128715

encukou opened this issue Jan 10, 2025 · 1 comment
Labels
extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir topic-ctypes type-feature A feature request or enhancement

Comments

@encukou
Copy link
Member

encukou commented Jan 10, 2025

Feature or enhancement

Currently, the internal representation of bitfields in ctypes is a bit-packed number containing the size and offset. This is rather cumbersome to deal with. (As far as I can tell, the reason is that getters/setters take a single argument. But, these are internal so the signature can be changed.)

My plan is to break the CField size/offset information out into more wordy but explicit fields:

  • byte_size & byte_offset, which describe the byte-aligned field within a struct. (This has the same size as the underlying type, and must be fully contained in the struct & readable/writable.)
  • bit_size & bit_offset, which identify the bits within that chunk

Also, I intend to add corresponding attributes an the Python level, and expose _CField publicly as ctypes.CField, mainly for typing purposes. At this point I don't plan to make the type instantiable.

Something like:

>>> class Color(Structure):
...     _fields_ = (
...         ('red', c_uint8),
...         ('green', c_uint8),
...         ('blue', c_uint8),
...         ('intense', c_bool, 1),
...         ('blinking', c_bool, 1),
...    )
...
>>> Color.red
<ctypes.CField 'red' type=c_ubyte, ofs=0, size=1>
>>> Color.green.type
<class 'ctypes.c_ubyte'>
>>> Color.blue.byte_offset
2
>>> Color.intense
<ctypes.CField 'intense' type=c_bool, ofs=3, bit_size=1, bit_offset=0>
>>> Color.blinking.bit_offset
1

Linked PRs

@encukou encukou added type-feature A feature request or enhancement topic-ctypes labels Jan 10, 2025
encukou added a commit that referenced this issue Mar 24, 2025
- Restore max field size to sys.maxsize, as in Python 3.13 & below
- PyCField: Split out bit/byte sizes/offsets.
- Expose CField's size/offset data to Python code
- Add generic checks for all the test structs/unions, using the newly exposed attrs
@encukou
Copy link
Member Author

encukou commented Mar 24, 2025

PR with exposed API is merged, but I'm working on some follow-up refactoring.

@picnixz picnixz added stdlib Python modules in the Lib dir extension-modules C modules in the Modules dir labels Mar 24, 2025
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 1, 2025
…-128950)

- Restore max field size to sys.maxsize, as in Python 3.13 & below
- PyCField: Split out bit/byte sizes/offsets.
- Expose CField's size/offset data to Python code
- Add generic checks for all the test structs/unions, using the newly exposed attrs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir topic-ctypes type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants