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 — Add ability to force alignment of ctypes.Structure · Issue #112433 · python/cpython

Add ability to force alignment of ctypes.Structure #112433

New issue
Copy link
New issue
Copy link
Closed
Closed
Add ability to force alignment of ctypes.Structure#112433
Copy link
Labels
topic-ctypestype-featureA feature request or enhancementA feature request or enhancement

Description

@monkeyman192
monkeyman192
opened on Nov 27, 2023
Issue body actions

Feature or enhancement

Proposal:

When creating a ctypes.Structure to map data from c/c++ to python, I have been coming up against issues where I have a structure which has an alignment due to a #pragma align.
Currently there is no way to define a ctypes.Structure which can map to an object like this.

I propose we add an _align_ attribute to the ctypes.Structure class which will instruct the code how to align the structure itself in memory.
In the way that the _pack_ attribute indicates the maximum alignment of the fields in the struct, this would indicate the minimum alignment of the struct itself.

An example of such a struct and it's use is as follows:

import ctypes

class IDString(ctypes.Structure):
    _align_ = 0x10
    _fields_ = [
        ("string", ctypes.c_char * 0x10),
    ]

class main(ctypes.Structure):
    _fields_ = [
        ("first", ctypes.c_uint32),
        ("second", ctypes.c_ubyte),
        ("string", IDString),
    ]

data = bytearray(
    b"\x07\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
    b"\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x21\x00\x00\x00\x00"
)
m = main.from_buffer(data)
print(f"first: {m.first}")  # first: 7
print(f"second: {m.second}")  # second: 1
print(f"string: {m.string.string}")  # string: b'hello world!'

Without the _align_ attribute the value of m.string.string would just be an empty bytes object since it would be reading from 8 bytes into the bytearray.

I have already made a (preliminary) implementation of this here.

Because the attribute is optional, I believe there are no potential backward compatibility issues as the default alignment will simply be what it was before (ie. 1).

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/add-ability-to-force-alignment-of-ctypes-structure/39109

Linked PRs

  • gh-112433: Add optional _align_ attribute to ctypes.Structure #113790
  • gh-112433 Docs: Add versionadded value and whatsnew snippet #125087
  • [3.13] gh-112433 add versionadded for ctypes.Structure._align_ (GH-125087) #125113

Activity

  1. monkeyman192
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Nov 27, 2023
  2. AlexWaygood
    added
    topic-ctypes
    on Nov 27, 2023
  3. serhiy-storchaka
    added this to Ctypes issueson Jan 1, 2024
  4. serhiy-storchaka
    added a commit that references this issue on Feb 15, 2024

    gh-112433: Add optional _align_ attribute to ctypes.Structure (GH-113790

    298bcdc
  5. serhiy-storchaka commented on Feb 15, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Feb 15, 2024
    Member
    More actions

    Thank you for your contribution @monkeyman192.

  6. serhiy-storchaka
    closed this as completedon Feb 15, 2024
  7. github-project-automation
    moved this to Done in Ctypes issueson Feb 15, 2024
  8. diegorusso
    added a commit that references this issue on Apr 17, 2024

    pythongh-112433: Add optional _align_ attribute to ctypes.Structure (p…

    d61ab3f
  9. kumaraditya303
    added a commit that references this issue on Oct 8, 2024

    gh-112433 add versionadded for `ctypes.Structure._align_` (#125087)

    5967dd8
  10. JelleZijlstra
    added 2 commits that reference this issue on Oct 8, 2024

    [3.13] pythongh-112433 add versionadded for `ctypes.Structure._align_` (

    f17e5dd

    [3.13] gh-112433 add versionadded for `ctypes.Structure._align_` (GH-…

    089c6d2
  11. efimov-mikhail
    added a commit that references this issue on Oct 9, 2024

    pythongh-112433 add versionadded for `ctypes.Structure._align_` (pyth…

    6b6d27f
  12. LukasWoodtli
    added a commit that references this issue on Jan 22, 2025

    pythongh-112433: Add optional _align_ attribute to ctypes.Structure (p…

    4a5881c
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

    topic-ctypestype-featureA feature request or enhancementA feature request or enhancement

    Projects

    • Ctypes issues
      Status
      Done
      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.