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 — TypeError when using ctypes.BigEndianUnion nested in ctypes.BigEndianStructure on a little endian system (or vice versa) · Issue #105102 · python/cpython

TypeError when using ctypes.BigEndianUnion nested in ctypes.BigEndianStructure on a little endian system (or vice versa) #105102

New issue
Copy link
New issue
Copy link
Closed
Closed
TypeError when using ctypes.BigEndianUnion nested in ctypes.BigEndianStructure on a little endian system (or vice versa)#105102
Copy link
Labels
3.11only security fixesonly security fixes3.12only security fixesonly security fixes3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-ctypestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@Sh3idan
Sh3idan
opened on May 30, 2023
Issue body actions

Bug report

The classes ctypes.BigEndianUnion and ctypes.LittleEndianUnion was added in Python 3.11. In the case of the use of an Union in a Structure class that match the opposite system endianness, the Union class can't be used as shown below:

# coding: utf-8
import sys 
import ctypes as c

assert sys.byteorder == "little"

"""
struct MyStruct {
    unsigned char a;
    unsigned char b:
};
struct MainStruct {
    union {
        struct MyStruct my_struct;
        short field_ab;
    } my_union;
    unsigned char foo;
};
"""
class MyStruct(c.BigEndianStructure):
    _fields_ = [("a", c.c_byte), ("b", c.c_byte)]

    
class MyUnion(c.BigEndianUnion):
    _fields_ = [("my_struct", MyStruct), ("ab", c.c_short)]
    

class MainStruct(c.BigEndianStructure):
    _fields_ = [("my_union", MyUnion), ("foo", c.c_byte)]
    

The following traceback is given by the interpretor:

TypeError                                 Traceback (most recent call last)
Cell In[1], line 28
     24 class MyUnion(c.BigEndianUnion):
     25     _fields_ = [("my_struct", MyStruct), ("ab", c.c_short)]
---> 28 class MainStruct(c.BigEndianStructure):
     29     _fields_ = [("my_union", MyUnion), ("foo", c.c_byte)]

File ~\AppData\Local\Programs\Python\Python311\Lib\ctypes\_endian.py:31, in _swapped_meta.__setattr__(self, attrname, value)
     29         typ = desc[1]
     30         rest = desc[2:]
---> 31         fields.append((name, _other_endian(typ)) + rest)
     32     value = fields
     33 super().__setattr__(attrname, value)

File ~\AppData\Local\Programs\Python\Python311\Lib\ctypes\_endian.py:21, in _other_endian(typ)
     19 if issubclass(typ, Structure):
     20     return typ
---> 21 raise TypeError("This type does not support other endian: %s" % typ)

TypeError: This type does not support other endian: <class '__main__.MyUnion'>

Your environment

  • CPython versions tested on: Interpreter compiled from the main branch in debug mode and also Python 3.11 (coming from the official release)
  • Operating system and architecture: Windows 10 x64

Linked PRs

  • gh-105102: fix nested unions in structures when the system byteorder is the opposite #105106
  • [3.12] gh-105102: Fix nested unions in structures when the system byteorder is the opposite (GH-105106) #114204
  • [3.11] gh-105102: Fix nested unions in structures when the system byteorder is the opposite (GH-105106) #114205

Activity

  1. Sh3idan
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on May 30, 2023
  2. iritkatriel
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Nov 24, 2023
  3. serhiy-storchaka
    added this to Ctypes issueson Jan 1, 2024
  4. serhiy-storchaka
    added
    topic-ctypes
    on Jan 1, 2024
  5. serhiy-storchaka
    added
    3.11only security fixesonly security fixes
    3.12only security fixesonly security fixes
    3.13only security fixesonly security fixes
    on Jan 17, 2024
  6. serhiy-storchaka
    added 4 commits that reference this issue on Jan 17, 2024

    Merge branch 'main' into fix-pythongh-105102

    3165db1

    gh-105102: Fix nested unions in structures when the system byteorder …

    0b541f6

    [3.12] pythongh-105102: Fix nested unions in structures when the syst…

    74fcab3

    [3.11] pythongh-105102: Fix nested unions in structures when the syst…

    482a655
  7. serhiy-storchaka
    closed this as completedon Jan 17, 2024
  8. github-project-automation
    moved this to Done in Ctypes issueson Jan 17, 2024
  9. serhiy-storchaka
    added 2 commits that reference this issue on Jan 17, 2024

    [3.11] gh-105102: Fix nested unions in structures when the system byt…

    2b03259

    [3.12] gh-105102: Fix nested unions in structures when the system byt…

    a735fea
  10. kulikjak
    added a commit that references this issue on Jan 22, 2024

    pythongh-105102: Fix nested unions in structures when the system byte…

    1631772
  11. aisk
    added a commit that references this issue on Feb 11, 2024

    pythongh-105102: Fix nested unions in structures when the system byte…

    893ed6c
  12. Glyphack
    added a commit that references this issue on Sep 2, 2024

    pythongh-105102: Fix nested unions in structures when the system byte…

    d33081d
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.11only security fixesonly security fixes3.12only security fixesonly security fixes3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-ctypestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    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.