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 — Silent ctypes.struct layout change from native to Windows when bitfields are used · Issue #131747 · python/cpython

Silent ctypes.struct layout change from native to Windows when bitfields are used #131747

New issue
Copy link
New issue
Copy link
Open
Open
Silent ctypes.struct layout change from native to Windows when bitfields are used#131747
Copy link
Labels
3.14bugs and security fixesbugs and security fixestopic-ctypestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@BCSharp
BCSharp
opened on Mar 25, 2025
Issue body actions

Bug report

Bug description:

Python 3.14 implements a special field _layout_ in ctypes.Structure that allows the programmer to explicitly choose the layout rules for bitfields used by either GCC/Clang or MSVC. The default layout is appropiate for the OS platform (MSVC on Windows, GCC otherwise) which is expected behaviour. It is also possible to use field _pack_ to change the layout to match the compiler. This is currently only implemented for the MSVC layout, not for GCC.

The problem is that using _pack_ on non-Windows systems silently switches the layout rules from GCC to MSVC, which is an unexpected layout by default on non-Windows systems. I propose that this case be handled as an error (NotImplementedError) rather than the silent layout switch. The error message may contain a hint that if the MSVC layout is indeed what is intended, one need to select it explicitly by setting field _layout_.

From the Zen of Python:

Explicit is better than implicit.
Readability counts.
Special cases aren't special enough to break the rules.
Errors should never pass silently.

Tested on 3.14a6

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs

  • gh-131747: ctypes: Deprecate _pack_ implicitly setting _layout_ = 'ms' #133205

Activity

  1. BCSharp
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Mar 25, 2025
  2. ZeroIntensity
    added
    topic-ctypes
    on Mar 25, 2025
  3. ZeroIntensity commented on Mar 25, 2025

    @ZeroIntensity
    ZeroIntensity
    on Mar 25, 2025
    Member
    More actions

    cc @encukou (you messed with bitfields recently, right?)

    If this is valid, we should probably mark this as a blocker.

  4. ZeroIntensity
    added
    3.14bugs and security fixesbugs and security fixes
    on Mar 25, 2025
  5. BCSharp commented on Mar 26, 2025

    @BCSharp
    BCSharp
    on Mar 26, 2025
    ContributorAuthor
    More actions

    @ZeroIntensity I'm glad that you see it as a bug and not a feature request. I discovered it while implementing the same functionality in IronPython, and just couldn't bring myself to follow CPython in this case (normally IronPython follows what CPython does, if possible). It would be great if CPython changes this so that there are no uneecessary differences between IronPython and CPython.

  6. ZeroIntensity commented on Mar 26, 2025

    @ZeroIntensity
    ZeroIntensity
    on Mar 26, 2025
    Member
    More actions

    Yeah, I doubt it's intended behavior. Assuming that's correct (I wasn't part of designing or reviewing _layout_/_pack_, so I'm going to yield to someone else), it would be good to get this fixed before the beta freeze locks it in.

  7. encukou commented on Mar 26, 2025

    @encukou
    encukou
    on Mar 26, 2025
    Member
    More actions

    Hi! I have limited access to the computer & I'm writing from memory. Please verify my claims :)

    Yes, 3.14 has an overhaul of struct layout; look at 3.13 for the previous status quo.
    As far as I remember, _pack_ has always switched to the MSVC layout; changing that would be a breaking change too.

    In both 3.13 and the current 3.14, the meaning of _pack_ is “MSVC #pragma pack”.
    GCC/clangs's __attribute__((packed)) is not implemented/supported at all.
    We could make that more explicit in the docs. (Unfortunately, after such time, people depend on the actual behaviour; it doesn't matter that there could be a better design that would match the docs.)
    Would such a docs change (“_pack_ is “MSVC #pragma pack”) allow you to bring yourself to follow CPython? (If so, could you send a PR?)


    We could deprecate _pack_ without _layout_ on non-Windows, but I fear that it would break existing users unnecssarily.

    Of course, if __attribute__((packed)) is implemented soon (which is not on anyone's roadmap AFAIK), it might be nice to switch the default for “Linux with _pack_” to it.
    But my current vague long-term plan is to make _layout_ a callable that constructs the field list, and gradually move away from configuration strings to something like _layout_ = gcc_sysv(packed=1). And if _pack_ will eventually be (soft-)deprecated, then I want to avoid “churn” for existing users: let them switch at once to a well-designed replacement, whenever it's ready.

  8. BCSharp commented on Mar 26, 2025

    @BCSharp
    BCSharp
    on Mar 26, 2025
    ContributorAuthor
    More actions

    To clarify (or add to the confusion) #pragma pack is also supported by GCC/Clang and is something different than __attribute__((packed)). I understand that _pack_ deals only with the former and not the latter, and that GCC packing is not yet supported in any form. It also makes sense that eventually a new scheme will be needed to support __attribute__((packed)), since it can be applied per field. For backward compatibility I see _pack_ being supported as an alternative to the new scheme, but then it will be even more confusing if it always means MSVC #pragma pack and never GCC #pragma pack.

    If the silent layout change was always in Python, I see it as it was always a bug that went unreported. This is not surprising since till 3.14 there far more serious problems with handling bitfields (overlapping fields, spurious padding bits inserted that did not match the compiler) so I doubt this is widely used. Even if making it an error "breaks" some existing code that just happens to work correctly, there is a trivial remedy by adding _layout_ = 'msvc' (or whatever the codestring for MSVC will be in the final release).

    At the very least, if not an error, please make it a warning that the layout was changed and that in the future this will become GCC pragma pack, so the user better be explicit.

  9. encukou
    added 3 commits that reference this issue on Apr 30, 2025

    pythongh-131747: ctypes: Warn when _pack_ implicitly changes default …

    66b72ba

    pythongh-131747: ctypes: Warn when _pack_ implicitly changes default …

    e9e0b75

    pythongh-131747: ctypes: Warn when _pack_ implicitly changes default …

    ade4c77
  10. encukou commented on May 1, 2025

    @encukou
    encukou
    on May 1, 2025
    Member
    More actions

    Not 100% convinced that breaking the old behaviour is OK, but, we can revert a deprecation warning.

    Does #133205 look OK?

  11. BCSharp commented on May 1, 2025

    @BCSharp
    BCSharp
    on May 1, 2025
    ContributorAuthor
    More actions

    Does #133205 look OK?

    Looks good to me! Thanks!

  12. encukou
    added a commit that references this issue on May 5, 2025

    gh-131747: ctypes: Deprecate _pack_ implicitly setting _layout_ = 'ms' (

    59f78d7
  13. encukou commented on May 17, 2025

    @encukou
    encukou
    on May 17, 2025
    Member
    More actions

    See #133800 for more discussion.

  14. Pranjal095
    added a commit that references this issue on Jul 12, 2025

    pythongh-131747: ctypes: Deprecate _pack_ implicitly setting _layout_…

    254221f
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

    3.14bugs and security fixesbugs and security fixestopic-ctypestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — 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.