Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Closed
Labels
3.11only security fixes3.12only security fixes3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-ctypestype-bugAn unexpected behavior, bug, or error
@Sh3idan

Description

@Sh3idan

Bug report

The classesctypes.BigEndianUnion andctypes.LittleEndianUnion was added in Python 3.11. In the case of the use of anUnion in aStructure class that match the opposite system endianness, theUnion class can't be used as shown below:

# coding: utf-8importsysimportctypesascassertsys.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;};"""classMyStruct(c.BigEndianStructure):_fields_= [("a",c.c_byte), ("b",c.c_byte)]classMyUnion(c.BigEndianUnion):_fields_= [("my_struct",MyStruct), ("ab",c.c_short)]classMainStruct(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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixes3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-ctypestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp