[New-bugs-announce] [issue43073] Adding a ctypes.Union to a ctypes.BigEndianStructure results in an error

Eric Poulsen report at bugs.python.org
Fri Jan 29 23:38:58 EST 2021


New submission from Eric Poulsen <eric at zyxod.com>:

Placing a ctypes.Union inside of a ctypes.BigEndianStructure results in "TypeError: This type does not support other endian".  I believe this is a similar problem to issue #4376 (https://bugs.python.org/issue4376)

Minimum repro test case:

import ctypes as ct

class U(ct.Union):
    _pack_=True
    _fields_=[
        ('a', ct.c_int),
        ('b', ct.c_int),
    ]

class S(ct.BigEndianStructure):
    _pack_=True
    _fields_=[
        ('x', ct.c_int),
        ('y', U),
    ]

I believe the fix is similar to that issue, though I admit I don't know enough about this code to be sure.

diff --git a/Lib/ctypes/_endian.py b/Lib/ctypes/_endian.py
index 37444bd6a7..525c5e58c9 100644
--- a/Lib/ctypes/_endian.py
+++ b/Lib/ctypes/_endian.py
@@ -18,6 +18,9 @@ def _other_endian(typ):
     # if typ is structure
     if issubclass(typ, Structure):
         return typ
+    # if typ is union:
+    if issubclass(typ, Union):
+        return typ
     raise TypeError("This type does not support other endian: %s" % typ)
 
 class _swapped_meta(type(Structure)):

----------
components: ctypes
messages: 385970
nosy: MrSurly
priority: normal
severity: normal
status: open
title: Adding a ctypes.Union to a ctypes.BigEndianStructure results in an error
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43073>
_______________________________________


More information about the New-bugs-announce mailing list