GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Untangle ctypes bitfield size and expose field information · Issue #128715 · python/cpython

Untangle ctypes bitfield size and expose field information #128715

New issue
Copy link
New issue
Copy link
Open
Open
Untangle ctypes bitfield size and expose field information#128715
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-ctypestype-featureA feature request or enhancementA feature request or enhancement

Description

@encukou
encukou
opened on Jan 10, 2025
Issue body actions

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

  • gh-128715: Expose ctypes.CField, with info attributes #128950
  • gh-128715: CTypeField: Put intness, signedness and pointerness in flags #138541

Activity

  1. encukou
    added
    type-featureA feature request or enhancementA feature request or enhancement
    topic-ctypes
    on Jan 10, 2025
  2. encukou
    added this to Ctypes issueson Jan 10, 2025
  3. encukou
    added a commit that references this issue on Mar 24, 2025

    gh-128715: Expose ctypes.CField, with info attributes (GH-128950)

    0e53038
  4. encukou commented on Mar 24, 2025

    @encukou
    encukou
    on Mar 24, 2025
    MemberAuthor
    More actions

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

  5. picnixz
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Mar 24, 2025
  6. diegorusso
    added a commit that references this issue on Apr 1, 2025

    pythongh-128715: Expose ctypes.CField, with info attributes (pythonGH…

    cb6990c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-ctypestype-featureA feature request or enhancementA feature request or enhancement

    Projects

    • Ctypes issues
      Status
      No status
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.